src/Entity/Blog.php line 18

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\BlogRepository;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\DBAL\Types\Types;
  7. use Doctrine\ORM\Mapping as ORM;
  8. use Symfony\Component\Validator\Constraints as Assert;
  9. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  10. use Symfony\Component\String\Slugger\SluggerInterface;
  11. #[ORM\Entity(repositoryClassBlogRepository::class)]
  12. #[ORM\Table(name'blog')]
  13. #[ORM\HasLifecycleCallbacks]
  14. #[UniqueEntity(fields: ['slug'], message'There is already a blog post with this slug')]
  15. class Blog
  16. {
  17.     public const STATUS_DRAFT 'draft';
  18.     public const STATUS_PUBLISHED 'published';
  19.     #[ORM\Id]
  20.     #[ORM\GeneratedValue]
  21.     #[ORM\Column]
  22.     private ?int $id null;
  23.     #[ORM\Column(length255)]
  24.     #[Assert\NotBlank]
  25.     private ?string $title null;
  26.     #[ORM\Column(length255uniquetrue)]
  27.     private ?string $slug null;
  28.     #[ORM\Column(typeTypes::TEXT)]
  29.     #[Assert\NotBlank]
  30.     private ?string $content null;
  31.     #[ORM\Column(typeTypes::TEXTnullabletrue)]
  32.     private ?string $excerpt null;
  33.     #[ORM\Column(length255nullabletrue)]
  34.     private ?string $featuredImage null;
  35.     #[ORM\Column(length20)]
  36.     private ?string $status self::STATUS_DRAFT;
  37.     #[ORM\Column(nullabletrue)]
  38.     private ?\DateTimeImmutable $publishedAt null;
  39.     #[ORM\Column]
  40.     private ?\DateTimeImmutable $createdAt null;
  41.     #[ORM\Column]
  42.     private ?\DateTimeImmutable $updatedAt null;
  43.     #[ORM\ManyToOne(inversedBy'blogPosts')]
  44.     #[ORM\JoinColumn(nullablefalse)]
  45.     private ?BlogAuthor $author null;
  46.     #[ORM\ManyToMany(targetEntityBlogTag::class, inversedBy'blogPosts')]
  47.     private Collection $tags;
  48.     public function __construct()
  49.     {
  50.         $this->tags = new ArrayCollection();
  51.         $this->createdAt = new \DateTimeImmutable();
  52.         $this->updatedAt = new \DateTimeImmutable();
  53.     }
  54.     #[ORM\PreUpdate]
  55.     public function setUpdatedAtValue(): void
  56.     {
  57.         $this->updatedAt = new \DateTimeImmutable();
  58.     }
  59.     public function getId(): ?int
  60.     {
  61.         return $this->id;
  62.     }
  63.     public function getTitle(): ?string
  64.     {
  65.         return $this->title;
  66.     }
  67.     public function setTitle(string $title): static
  68.     {
  69.         $this->title $title;
  70.         return $this;
  71.     }
  72.     public function getSlug(): ?string
  73.     {
  74.         return $this->slug;
  75.     }
  76.     public function setSlug(string $slug): static
  77.     {
  78.         $this->slug $slug;
  79.         return $this;
  80.     }
  81.     public function computeSlug(SluggerInterface $slugger): void
  82.     {
  83.         if (!$this->slug || '-' === $this->slug) {
  84.             $this->slug = (string) $slugger->slug((string) $this->title)->lower();
  85.         }
  86.     }
  87.     public function getContent(): ?string
  88.     {
  89.         return $this->content;
  90.     }
  91.     public function setContent(string $content): static
  92.     {
  93.         $this->content $content;
  94.         return $this;
  95.     }
  96.     public function getExcerpt(): ?string
  97.     {
  98.         return $this->excerpt;
  99.     }
  100.     public function setExcerpt(?string $excerpt): static
  101.     {
  102.         $this->excerpt $excerpt;
  103.         return $this;
  104.     }
  105.     public function getFeaturedImage(): ?string
  106.     {
  107.         return $this->featuredImage;
  108.     }
  109.     public function setFeaturedImage(?string $featuredImage): static
  110.     {
  111.         $this->featuredImage $featuredImage;
  112.         return $this;
  113.     }
  114.     public function getStatus(): ?string
  115.     {
  116.         return $this->status;
  117.     }
  118.     public function setStatus(string $status): static
  119.     {
  120.         $this->status $status;
  121.         return $this;
  122.     }
  123.     public function isPublished(): bool
  124.     {
  125.         return self::STATUS_PUBLISHED === $this->status;
  126.     }
  127.     public function getPublishedAt(): ?\DateTimeImmutable
  128.     {
  129.         return $this->publishedAt;
  130.     }
  131.     public function setPublishedAt(?\DateTimeImmutable $publishedAt): static
  132.     {
  133.         $this->publishedAt $publishedAt;
  134.         return $this;
  135.     }
  136.     public function getCreatedAt(): ?\DateTimeImmutable
  137.     {
  138.         return $this->createdAt;
  139.     }
  140.     public function setCreatedAt(\DateTimeImmutable $createdAt): static
  141.     {
  142.         $this->createdAt $createdAt;
  143.         return $this;
  144.     }
  145.     public function getUpdatedAt(): ?\DateTimeImmutable
  146.     {
  147.         return $this->updatedAt;
  148.     }
  149.     public function setUpdatedAt(\DateTimeImmutable $updatedAt): static
  150.     {
  151.         $this->updatedAt $updatedAt;
  152.         return $this;
  153.     }
  154.     public function getAuthor(): ?BlogAuthor
  155.     {
  156.         return $this->author;
  157.     }
  158.     public function setAuthor(?BlogAuthor $author): static
  159.     {
  160.         $this->author $author;
  161.         return $this;
  162.     }
  163.     /**
  164.      * @return Collection<int, BlogTag>
  165.      */
  166.     public function getTags(): Collection
  167.     {
  168.         return $this->tags;
  169.     }
  170.     public function addTag(BlogTag $tag): static
  171.     {
  172.         if (!$this->tags->contains($tag)) {
  173.             $this->tags->add($tag);
  174.         }
  175.         return $this;
  176.     }
  177.     public function removeTag(BlogTag $tag): static
  178.     {
  179.         $this->tags->removeElement($tag);
  180.         return $this;
  181.     }
  182.     public function __toString(): string
  183.     {
  184.         return $this->title ?? 'New Blog Post';
  185.     }
  186. }