src/Entity/CollectionArticle.php line 48

  1. <?php
  2. namespace App\Entity;
  3. use Doctrine\ORM\Mapping as ORM,
  4.     Doctrine\DBAL\Types\Types;
  5. use Symfony\Component\Serializer\Annotation\Groups,
  6.     Symfony\Component\Validator\Constraints as Assert;
  7. use ApiPlatform\Metadata\ApiResource,
  8.     ApiPlatform\Metadata\ApiProperty,
  9.     ApiPlatform\Metadata\Get,
  10.     ApiPlatform\Metadata\GetCollection,
  11.     ApiPlatform\Metadata\Post,
  12.     ApiPlatform\Metadata\Put,
  13.     ApiPlatform\Metadata\Delete,
  14.     ApiPlatform\Metadata\ApiFilter,
  15.     ApiPlatform\Doctrine\Orm\Filter\OrderFilter;
  16. use App\Entity\Trait\IdTrait,
  17.     App\Entity\Trait\UuidTrait,
  18.     App\Entity\Trait\OrdStatTrait,
  19.     App\Entity\Trait\TimestampableTrait,
  20.     App\Entity\Interface\OrdStatableInterface,
  21.     App\Repository\CollectionArticleRepository;
  22. use App\Filter\IriFilter,
  23.     App\Enum\Language;
  24. #[ApiResource(
  25.     description'Collection articles',
  26.     normalizationContext: ['groups' => ['read''read:' self::class]],
  27.     denormalizationContext: ['groups' => ['write']],
  28.     security'is_granted("' Collection::class . '")',
  29.     order: ['ord' => 'desc'],
  30.     operations: [
  31.         new GetCollection(),
  32.         new Post(denormalizationContext: ['groups' => ['write',  'post']]),
  33.         new Get(),
  34.         new Put(),
  35.         new Delete(),
  36.     ],
  37.     extraProperties: ['standard_put' => false],
  38. )]
  39. #[ApiFilter(IriFilter::class, properties: ['parent'])]
  40. #[ApiFilter(OrderFilter::class, properties: ['ord'])]
  41. #[ORM\Entity(repositoryClassCollectionArticleRepository::class)]
  42. class CollectionArticle implements OrdStatableInterface
  43. {
  44.     use IdTrait,
  45.         UuidTrait,
  46.         OrdStatTrait,
  47.         TimestampableTrait;
  48.     #[ApiProperty(description'Parent'readableLinkfalsewritableLinkfalse)]
  49.     #[Groups(['read''post'])]
  50.     #[Assert\NotNull]
  51.     #[ORM\ManyToOne(targetEntityCollection::class, inversedBy'articles')]
  52.     #[ORM\JoinColumn(onDelete'cascade'nullablefalse)]
  53.     private Collection $parent;
  54.     #[ApiProperty(description'Article'writableLinkfalse)]
  55.     #[Groups(['read''write'])]
  56.     #[Assert\NotNull]
  57.     #[ORM\ManyToOne(targetEntityJournalArticle::class)]
  58.     #[ORM\JoinColumn(onDelete'cascade'nullablefalse)]
  59.     private JournalArticle $article;
  60.     #[ApiProperty(description'Is visible on home')]
  61.     #[Groups(['read:' self::class, 'write'])]
  62.     #[ORM\Column(typeTypes::BOOLEANoptions: ['default' => false])]
  63.     private bool $isVisibleOnHome false;
  64.     public function __construct(Collection $parent)
  65.     {
  66.         $this->setUuid();
  67.         $this->parent $parent;
  68.         $this->createdAt = new \DateTimeImmutable();
  69.         $this->updatedAt = new \DateTimeImmutable();
  70.         $parent->addArticle($this);
  71.     }
  72.     public function getParent(): Collection
  73.     {
  74.         return $this->parent;
  75.     }
  76.     public function getArticle(): JournalArticle
  77.     {
  78.         return $this->article;
  79.     }
  80.     public function setArticle(JournalArticle $article): self
  81.     {
  82.         $this->article $article;
  83.         return $this;
  84.     }
  85.     public function getIsVisibleOnHome(): bool
  86.     {
  87.         return $this->isVisibleOnHome;
  88.     }
  89.     public function setIsVisibleOnHome(bool $isVisibleOnHome): self
  90.     {
  91.         $this->isVisibleOnHome $isVisibleOnHome;
  92.         return $this;
  93.     }
  94.     #[Groups(['read:' self::class])]
  95.     public function getParentNativeLanguage(): Language
  96.     {
  97.         return $this->getParent()->getNativeLanguage();
  98.     }
  99.     #[Groups(['read:' self::class])]
  100.     public function getParentLanguages(): array
  101.     {
  102.         return $this->getParent()->getLanguages();
  103.     }
  104.     #[Groups(['read'])]
  105.     public function getPrintLanguage(): Language
  106.     {
  107.         $languages $this->getParent()->getLanguages();
  108.         if (in_array(Language::PL$languages)) {
  109.             return Language::PL;
  110.         }
  111.         if (in_array(Language::EN$languages)) {
  112.             return Language::EN;
  113.         }
  114.         return $this->getParent()->getNativeLanguage();
  115.     }
  116. }