src/Entity/BlogTag.php line 17

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\BlogTagRepository;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use Symfony\Component\Validator\Constraints as Assert;
  8. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  9. use Symfony\Component\String\Slugger\SluggerInterface;
  10. #[ORM\Entity(repositoryClassBlogTagRepository::class)]
  11. #[ORM\Table(name'blog_tag')]
  12. #[ORM\HasLifecycleCallbacks]
  13. #[UniqueEntity(fields: ['slug'], message'There is already a tag with this slug')]
  14. class BlogTag
  15. {
  16.     #[ORM\Id]
  17.     #[ORM\GeneratedValue]
  18.     #[ORM\Column]
  19.     private ?int $id null;
  20.     #[ORM\Column(length255)]
  21.     #[Assert\NotBlank]
  22.     private ?string $name null;
  23.     #[ORM\Column(length255uniquetrue)]
  24.     private ?string $slug null;
  25.     #[ORM\Column]
  26.     private ?\DateTimeImmutable $createdAt null;
  27.     #[ORM\ManyToMany(targetEntityBlog::class, mappedBy'tags')]
  28.     private Collection $blogPosts;
  29.     public function __construct()
  30.     {
  31.         $this->blogPosts = new ArrayCollection();
  32.         $this->createdAt = new \DateTimeImmutable();
  33.     }
  34.     public function getId(): ?int
  35.     {
  36.         return $this->id;
  37.     }
  38.     public function getName(): ?string
  39.     {
  40.         return $this->name;
  41.     }
  42.     public function setName(string $name): static
  43.     {
  44.         $this->name $name;
  45.         return $this;
  46.     }
  47.     public function getSlug(): ?string
  48.     {
  49.         return $this->slug;
  50.     }
  51.     public function setSlug(string $slug): static
  52.     {
  53.         $this->slug $slug;
  54.         return $this;
  55.     }
  56.     public function computeSlug(SluggerInterface $slugger): void
  57.     {
  58.         if (!$this->slug || '-' === $this->slug) {
  59.             $this->slug = (string) $slugger->slug((string) $this->name)->lower();
  60.         }
  61.     }
  62.     public function getCreatedAt(): ?\DateTimeImmutable
  63.     {
  64.         return $this->createdAt;
  65.     }
  66.     public function setCreatedAt(\DateTimeImmutable $createdAt): static
  67.     {
  68.         $this->createdAt $createdAt;
  69.         return $this;
  70.     }
  71.     /**
  72.      * @return Collection<int, Blog>
  73.      */
  74.     public function getBlogPosts(): Collection
  75.     {
  76.         return $this->blogPosts;
  77.     }
  78.     public function addBlogPost(Blog $blogPost): static
  79.     {
  80.         if (!$this->blogPosts->contains($blogPost)) {
  81.             $this->blogPosts->add($blogPost);
  82.             $blogPost->addTag($this);
  83.         }
  84.         return $this;
  85.     }
  86.     public function removeBlogPost(Blog $blogPost): static
  87.     {
  88.         if ($this->blogPosts->removeElement($blogPost)) {
  89.             $blogPost->removeTag($this);
  90.         }
  91.         return $this;
  92.     }
  93.     public function __toString(): string
  94.     {
  95.         return $this->name ?? 'New Tag';
  96.     }
  97. }