src/Entity/Collections.php line 26

  1. <?php
  2. namespace App\Entity;
  3. use ApiPlatform\Metadata\ApiResource;
  4. use ApiPlatform\Metadata\GetCollection;
  5. use App\Repository\CollectionsRepository;
  6. use App\Trait\ActivableTrait;
  7. use App\Trait\DateTrait;
  8. use App\Trait\DeletableTrait;
  9. use DateTimeImmutable;
  10. use Doctrine\Common\Collections\ArrayCollection;
  11. use Doctrine\Common\Collections\Collection;
  12. use Doctrine\DBAL\Types\Types;
  13. use Doctrine\ORM\Mapping as ORM;
  14. use Symfony\Component\Serializer\Annotation\Groups;
  15. #[ORM\Entity(repositoryClass: CollectionsRepository::class)]
  16. #[ApiResource(
  17.     operations: [
  18.         new GetCollection(
  19.             normalizationContext: ['groups' => ['collections:list']]
  20.         )
  21.     ]
  22. )]
  23. class Collections
  24. {
  25.     use DateTrait;
  26.     use ActivableTrait;
  27.     use DeletableTrait;
  28.     #[ORM\Id]
  29.     #[ORM\GeneratedValue]
  30.     #[ORM\Column]
  31.     #[Groups(['collections:list', 'article:view'])]
  32.     private ?int $id = null;
  33.     #[ORM\Column(length: 255)]
  34.     #[Groups(['collections:list', 'article:view'])]
  35.     private ?string $name = null;
  36.     #[ORM\Column(type: Types::TEXT, nullable: true)]
  37.     private ?string $content = null;
  38.     #[ORM\ManyToOne(cascade: ["persist"])]
  39.     private ?Attachment $thumbnail = null;
  40.     #[ORM\ManyToMany(targetEntity: Article::class, mappedBy: 'collections')]
  41.     private Collection $articles;
  42.     #[ORM\ManyToOne(inversedBy: 'collections')]
  43.     #[ORM\JoinColumn(nullable: false)]
  44.     private ?Establishment $establishment = null;
  45.     #[ORM\ManyToOne(targetEntity: self::class, inversedBy: 'collections')]
  46.     private ?self $parent = null;
  47.     #[ORM\OneToMany(mappedBy: 'parent', targetEntity: self::class)]
  48.     private Collection $collections;
  49.     #[ORM\Column(length: 50, nullable: true)]
  50.     private ?string $icon = null;
  51.     public function __construct()
  52.     {
  53.         $this->isActive = 0;
  54.         if (is_null($this->createdAt)) $this->createdAt = new DateTimeImmutable();
  55.         $this->updatedAt = new DateTimeImmutable();
  56.         $this->articles = new ArrayCollection();
  57.         $this->collections = new ArrayCollection();
  58.     }
  59.     public function getId(): ?int
  60.     {
  61.         return $this->id;
  62.     }
  63.     public function getName(): ?string
  64.     {
  65.         return $this->name;
  66.     }
  67.     public function setName(string $name): self
  68.     {
  69.         $this->name = $name;
  70.         return $this;
  71.     }
  72.     public function getContent(): ?string
  73.     {
  74.         return $this->content;
  75.     }
  76.     public function setContent(string $content): self
  77.     {
  78.         $this->content = $content;
  79.         return $this;
  80.     }
  81.     public function getThumbnail(): ?Attachment
  82.     {
  83.         return $this->thumbnail;
  84.     }
  85.     public function setThumbnail(?Attachment $thumbnail): self
  86.     {
  87.         $this->thumbnail = $thumbnail;
  88.         return $this;
  89.     }
  90.     /**
  91.      * @return Collection<int, Article>
  92.      */
  93.     public function getArticles(): Collection
  94.     {
  95.         return $this->articles;
  96.     }
  97.     public function addArticle(Article $article): self
  98.     {
  99.         if (!$this->articles->contains($article)) {
  100.             $this->articles->add($article);
  101.             $article->addCollection($this);
  102.         }
  103.         return $this;
  104.     }
  105.     public function removeArticle(Article $article): self
  106.     {
  107.         if ($this->articles->removeElement($article)) {
  108.             $article->removeCollection($this);
  109.         }
  110.         return $this;
  111.     }
  112.     public function __toString(): string
  113.     {
  114.         return $this->name;
  115.     }
  116.     public function getEstablishment(): ?Establishment
  117.     {
  118.         return $this->establishment;
  119.     }
  120.     public function setEstablishment(?Establishment $establishment): self
  121.     {
  122.         $this->establishment = $establishment;
  123.         return $this;
  124.     }
  125.     public function getParent(): ?self
  126.     {
  127.         return $this->parent;
  128.     }
  129.     public function setParent(?self $parent): self
  130.     {
  131.         $this->parent = $parent;
  132.         return $this;
  133.     }
  134.     /**
  135.      * @return Collection<int, self>
  136.      */
  137.     public function getCollections(): Collection
  138.     {
  139.         return $this->collections;
  140.     }
  141.     public function addCollection(self $collection): self
  142.     {
  143.         if (!$this->collections->contains($collection)) {
  144.             $this->collections->add($collection);
  145.             $collection->setParent($this);
  146.         }
  147.         return $this;
  148.     }
  149.     public function removeCollection(self $collection): self
  150.     {
  151.         if ($this->collections->removeElement($collection)) {
  152.             // set the owning side to null (unless already changed)
  153.             if ($collection->getParent() === $this) {
  154.                 $collection->setParent(null);
  155.             }
  156.         }
  157.         return $this;
  158.     }
  159.     public function getIcon(): ?string
  160.     {
  161.         return $this->icon;
  162.     }
  163.     public function setIcon(?string $icon): static
  164.     {
  165.         $this->icon = $icon;
  166.         return $this;
  167.     }
  168. }