src/Entity/JournalArticleFileTranslation.php line 31

  1. <?php
  2. namespace App\Entity;
  3. use Doctrine\ORM\Mapping as ORM,
  4.     Doctrine\Common\Collections\Collection,
  5.     Doctrine\Common\Collections\ArrayCollection;
  6. use Symfony\Component\Serializer\Annotation\Groups,
  7.     Symfony\Component\Validator\Constraints as Assert;
  8. use ApiPlatform\Metadata\ApiProperty,
  9.     ApiPlatform\Metadata\ApiResource,
  10.     ApiPlatform\Metadata\Get;
  11. use App\Entity\Trait\IdTrait,
  12.     App\Entity\Trait\UuidTrait,
  13.     App\Entity\Trait\TimestampableTrait,
  14.     App\Entity\Trait\TranslationTrait,
  15.     App\Entity\Interface\TranslationInterface,
  16.     App\Entity\Interface\OrderableInterface;
  17. use App\Enum\Language,
  18.     App\Repository\JournalArticleFileTranslationRepository;
  19. #[ApiResource(
  20.     security'is_granted("' Journal::class . '")',
  21.     operations: [ new Get() ],
  22.     extraProperties: ['standard_put' => false],
  23. )]
  24. #[ORM\Entity(repositoryClassJournalArticleFileTranslationRepository::class)]
  25. class JournalArticleFileTranslation implements TranslationInterface
  26. {
  27.     use IdTrait,
  28.         UuidTrait,
  29.         TimestampableTrait,
  30.         TranslationTrait;
  31.     #[ApiProperty(description'Files')]
  32.     #[Groups(['read:' self::class, 'write'])]
  33.     #[Assert\Valid]
  34.     #[ORM\OneToMany(
  35.         targetEntityJournalArticleFileTranslationFile::class,
  36.         mappedBy'parent',
  37.         cascade: ['persist''remove'],
  38.         orphanRemovaltrue
  39.     )]
  40.     #[ORM\OrderBy(['ord' => 'asc'])]
  41.     private Collection $files;
  42.     public function __construct(JournalArticleFile $parentLanguage $lang)
  43.     {
  44.         $this->setUuid();
  45.         $this->parent $parent;
  46.         $this->lang $lang;
  47.         $this->files = new ArrayCollection();
  48.         $this->createdAt = new \DateTimeImmutable();
  49.         $this->updatedAt = new \DateTimeImmutable();
  50.         $parent->addTranslation($this);
  51.     }
  52.     private function reorderCollection(string $name): void
  53.     {
  54.         $collection $this->{$name}->toArray();
  55.         $this->{$name}->clear();
  56.         uasort($collection, function (OrderableInterface $aOrderableInterface $b): int {
  57.             if ($a->getOrd() === $b->getOrd()) {
  58.                 return 0;
  59.             }
  60.             return $a->getOrd() < $b->getOrd() ? -1;
  61.         });
  62.         foreach ($collection as $item) {
  63.             $this->{$name}[] = $item;
  64.         }
  65.         return;
  66.     }
  67.     /**
  68.      * @return Collection|JournalArticleFileTranslationFile[]
  69.      */
  70.     public function getFiles(): Collection
  71.     {
  72.         return $this->files;
  73.     }
  74.     public function addFile(JournalArticleFileTranslationFile $file): self
  75.     {
  76.         if ($this->files->contains($file)) {
  77.             return $this;
  78.         }
  79.         $this->files[] = $file;
  80.         $this->reorderCollection('files');
  81.         return $this;
  82.     }
  83.     public function removeFile(JournalArticleFileTranslationFile $file): self
  84.     {
  85.         $this->files->removeElement($file);
  86.         $this->reorderCollection('files');
  87.         return $this;
  88.     }
  89.     public function resetFiles(): self
  90.     {
  91.         $this->files = new ArrayCollection();
  92.         return $this;
  93.     }
  94.     public function getVisibleFiles(): Collection
  95.     {
  96.         return $this->files->filter(
  97.             fn (JournalArticleFileTranslationFile $file) => $file->getMedia() !== null
  98.         );
  99.     }
  100.     public function getHasFiles(): bool
  101.     {
  102.         foreach($this->files as $file) {
  103.             if (! $file->getMedia()) {
  104.                 continue;
  105.             }
  106.             return true;
  107.         }
  108.         return false;
  109.     }
  110.     public function clone(JournalArticleFile $parent): self
  111.     {
  112.         $clone = clone $this;
  113.         $clone->id null;
  114.         $clone->setUuid();
  115.         $clone->parent $parent;
  116.         $clone->parent->addTranslation($clone);
  117.         $this->resetFiles();
  118.         $clone->createdAt = new \DateTimeImmutable();
  119.         $clone->updatedAt = new \DateTimeImmutable();
  120.         return $clone;
  121.     }
  122. }