src/Entity/Collection.php line 55

  1. <?php
  2. namespace App\Entity;
  3. use Doctrine\ORM\Mapping as ORM,
  4.     Doctrine\DBAL\Types\Types,
  5.     Doctrine\Common\Collections\Collection as DoctrineCollection,
  6.     Doctrine\Common\Collections\ArrayCollection;
  7. use Symfony\Component\Serializer\Annotation\Groups;
  8. use ApiPlatform\Metadata\ApiResource,
  9.     ApiPlatform\Metadata\ApiProperty,
  10.     ApiPlatform\Metadata\Get,
  11.     ApiPlatform\Metadata\GetCollection,
  12.     ApiPlatform\Metadata\Post,
  13.     ApiPlatform\Metadata\Put,
  14.     ApiPlatform\Metadata\Delete,
  15.     ApiPlatform\Metadata\ApiFilter,
  16.     ApiPlatform\Doctrine\Orm\Filter\OrderFilter;
  17. use App\Entity\Trait\IdTrait,
  18.     App\Entity\Trait\UuidTrait,
  19.     App\Entity\Trait\OrdStatTrait,
  20.     App\Entity\Trait\TimestampableTrait,
  21.     App\Entity\Trait\TranslatableTrait,
  22.     App\Entity\Trait\Languages\SiteLanguagesTrait,
  23.     App\Entity\Interface\TranslatableInterface,
  24.     App\Entity\Interface\OrdStatableInterface,
  25.     App\Entity\Interface\LanguageableInterface,
  26.     App\Repository\CollectionRepository,
  27.     App\Enum\Language;
  28. #[ApiResource(
  29.     description'Collections',
  30.     normalizationContext: ['groups' => [
  31.         'read',
  32.         'read:' self::class,
  33.         'read:' self::class . 'Translation',
  34.         'read:' AbstractThumb::class
  35.     ]],
  36.     denormalizationContext: ['groups' => ['write']],
  37.     security'is_granted("' self::class . '")',
  38.     order: ['ord' => 'desc'],
  39.     operations: [
  40.         new GetCollection(),
  41.         new Post(denormalizationContext: ['groups' => ['write',  'post']]),
  42.         new Get(),
  43.         new Put(),
  44.         new Delete(),
  45.     ],
  46.     extraProperties: ['standard_put' => false],
  47. )]
  48. #[ApiFilter(OrderFilter::class, properties: ['ord'])]
  49. #[ORM\Entity(repositoryClassCollectionRepository::class)]
  50. class Collection implements TranslatableInterfaceOrdStatableInterfaceLanguageableInterface
  51. {
  52.     use IdTrait,
  53.         UuidTrait,
  54.         OrdStatTrait,
  55.         TimestampableTrait,
  56.         TranslatableTrait,
  57.         SiteLanguagesTrait;
  58.     #[ApiProperty(description'Is visible on home')]
  59.     #[Groups(['read:' self::class, 'write'])]
  60.     #[ORM\Column(typeTypes::BOOLEANoptions: ['default' => false])]
  61.     private bool $isVisibleOnHome false;
  62.     #[ApiProperty(description'Is UJ')]
  63.     #[Groups(['read:' self::class, 'write'])]
  64.     #[ORM\Column(typeTypes::BOOLEANoptions: ['default' => false])]
  65.     private bool $isUj false;
  66.     #[ApiProperty(description'Articles')]
  67.     #[ORM\OneToMany(targetEntityCollectionArticle::class, mappedBy'parent'cascade: ['persist''remove'])]
  68.     #[ORM\OrderBy(['ord' => 'desc'])]
  69.     private DoctrineCollection $articles;
  70.     #[ApiProperty(description'Issues')]
  71.     #[ORM\OneToMany(targetEntityCollectionIssue::class, mappedBy'parent'cascade: ['persist''remove'])]
  72.     #[ORM\OrderBy(['ord' => 'desc'])]
  73.     private DoctrineCollection $issues;
  74.     public function __construct(?array $languages)
  75.     {
  76.         $this->setUuid();
  77.         $this->translations = new ArrayCollection();
  78.         $this->articles = new ArrayCollection();
  79.         $this->issues = new ArrayCollection();
  80.         $this->setLanguages($languages ?? Language::DEFAULT_SITE);
  81.         $this->createdAt = new \DateTimeImmutable();
  82.         $this->updatedAt = new \DateTimeImmutable();
  83.     }
  84.     public function getIsVisibleOnHome(): bool
  85.     {
  86.         return $this->isVisibleOnHome;
  87.     }
  88.     public function setIsVisibleOnHome(bool $isVisibleOnHome): self
  89.     {
  90.         $this->isVisibleOnHome $isVisibleOnHome;
  91.         return $this;
  92.     }
  93.     public function getIsUj(): bool
  94.     {
  95.         return $this->isUj;
  96.     }
  97.     public function setIsUj(bool $isUj): self
  98.     {
  99.         $this->isUj $isUj;
  100.         return $this;
  101.     }
  102.     /**
  103.      * @return DoctrineCollection|CollectionArticle[]
  104.      */
  105.     public function getArticles(): DoctrineCollection
  106.     {
  107.         return $this->articles;
  108.     }
  109.     public function addArticle(CollectionArticle $article): self
  110.     {
  111.         if ($this->articles->contains($article)) {
  112.             return $this;
  113.         }
  114.         $this->articles[] = $article;
  115.         return $this;
  116.     }
  117.     public function removeArticle(CollectionArticle $article): self
  118.     {
  119.         $this->articles->removeElement($article);
  120.         return $this;
  121.     }
  122.     public function getArticlesCount(): int
  123.     {
  124.         return $this->articles->count();
  125.     }
  126.     /**
  127.      * @return DoctrineCollection|CollectionIssue[]
  128.      */
  129.     public function getIssues(): DoctrineCollection
  130.     {
  131.         return $this->issues;
  132.     }
  133.     public function addIssue(CollectionIssue $issue): self
  134.     {
  135.         if ($this->issues->contains($issue)) {
  136.             return $this;
  137.         }
  138.         $this->issues[] = $issue;
  139.         return $this;
  140.     }
  141.     public function removeIssue(CollectionIssue $issue): self
  142.     {
  143.         $this->issues->removeElement($issue);
  144.         return $this;
  145.     }
  146.     public function getIssuesCount(): int
  147.     {
  148.         return $this->issues->count();
  149.     }
  150.     public function getElementsCount(): int
  151.     {
  152.         return $this->getIssuesCount() + $this->getArticlesCount();
  153.     }
  154. }