<?php
namespace App\Entity;
use App\Repository\BlogRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\String\Slugger\SluggerInterface;
#[ORM\Entity(repositoryClass: BlogRepository::class)]
#[ORM\Table(name: 'blog')]
#[ORM\HasLifecycleCallbacks]
#[UniqueEntity(fields: ['slug'], message: 'There is already a blog post with this slug')]
class Blog
{
public const STATUS_DRAFT = 'draft';
public const STATUS_PUBLISHED = 'published';
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(length: 255)]
#[Assert\NotBlank]
private ?string $title = null;
#[ORM\Column(length: 255, unique: true)]
private ?string $slug = null;
#[ORM\Column(type: Types::TEXT)]
#[Assert\NotBlank]
private ?string $content = null;
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $excerpt = null;
#[ORM\Column(length: 255, nullable: true)]
private ?string $featuredImage = null;
#[ORM\Column(length: 20)]
private ?string $status = self::STATUS_DRAFT;
#[ORM\Column(nullable: true)]
private ?\DateTimeImmutable $publishedAt = null;
#[ORM\Column]
private ?\DateTimeImmutable $createdAt = null;
#[ORM\Column]
private ?\DateTimeImmutable $updatedAt = null;
#[ORM\ManyToOne(inversedBy: 'blogPosts')]
#[ORM\JoinColumn(nullable: false)]
private ?BlogAuthor $author = null;
#[ORM\ManyToMany(targetEntity: BlogTag::class, inversedBy: 'blogPosts')]
private Collection $tags;
public function __construct()
{
$this->tags = new ArrayCollection();
$this->createdAt = new \DateTimeImmutable();
$this->updatedAt = new \DateTimeImmutable();
}
#[ORM\PreUpdate]
public function setUpdatedAtValue(): void
{
$this->updatedAt = new \DateTimeImmutable();
}
public function getId(): ?int
{
return $this->id;
}
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(string $title): static
{
$this->title = $title;
return $this;
}
public function getSlug(): ?string
{
return $this->slug;
}
public function setSlug(string $slug): static
{
$this->slug = $slug;
return $this;
}
public function computeSlug(SluggerInterface $slugger): void
{
if (!$this->slug || '-' === $this->slug) {
$this->slug = (string) $slugger->slug((string) $this->title)->lower();
}
}
public function getContent(): ?string
{
return $this->content;
}
public function setContent(string $content): static
{
$this->content = $content;
return $this;
}
public function getExcerpt(): ?string
{
return $this->excerpt;
}
public function setExcerpt(?string $excerpt): static
{
$this->excerpt = $excerpt;
return $this;
}
public function getFeaturedImage(): ?string
{
return $this->featuredImage;
}
public function setFeaturedImage(?string $featuredImage): static
{
$this->featuredImage = $featuredImage;
return $this;
}
public function getStatus(): ?string
{
return $this->status;
}
public function setStatus(string $status): static
{
$this->status = $status;
return $this;
}
public function isPublished(): bool
{
return self::STATUS_PUBLISHED === $this->status;
}
public function getPublishedAt(): ?\DateTimeImmutable
{
return $this->publishedAt;
}
public function setPublishedAt(?\DateTimeImmutable $publishedAt): static
{
$this->publishedAt = $publishedAt;
return $this;
}
public function getCreatedAt(): ?\DateTimeImmutable
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeImmutable $createdAt): static
{
$this->createdAt = $createdAt;
return $this;
}
public function getUpdatedAt(): ?\DateTimeImmutable
{
return $this->updatedAt;
}
public function setUpdatedAt(\DateTimeImmutable $updatedAt): static
{
$this->updatedAt = $updatedAt;
return $this;
}
public function getAuthor(): ?BlogAuthor
{
return $this->author;
}
public function setAuthor(?BlogAuthor $author): static
{
$this->author = $author;
return $this;
}
/**
* @return Collection<int, BlogTag>
*/
public function getTags(): Collection
{
return $this->tags;
}
public function addTag(BlogTag $tag): static
{
if (!$this->tags->contains($tag)) {
$this->tags->add($tag);
}
return $this;
}
public function removeTag(BlogTag $tag): static
{
$this->tags->removeElement($tag);
return $this;
}
public function __toString(): string
{
return $this->title ?? 'New Blog Post';
}
}