src/Entity/JournalArticleContentBlockTranslation.php line 41

  1. <?php
  2. namespace App\Entity;
  3. use App\Entity\Interface\OrderableInterface;
  4. use App\Entity\Interface\ThumbableInterface;
  5. use App\Entity\Interface\ThumbInterface,
  6.     App\Enum\MediaPosition,
  7.     App\Validator\Constraints as CustomAssert;
  8. use App\Entity\Trait\Media\ImageThumbableTrait;
  9. use App\Util\ClassUtils;
  10. use Doctrine\Common\Collections\ArrayCollection;
  11. use Doctrine\Common\Collections\Collection,
  12.     Doctrine\ORM\Mapping as ORM,
  13.     Doctrine\DBAL\Types\Types;
  14. use Symfony\Component\Serializer\Annotation\Groups,
  15.     Symfony\Component\Validator\Constraints as Assert;;
  16. use ApiPlatform\Metadata\ApiProperty,
  17.     ApiPlatform\Metadata\ApiResource,
  18.     ApiPlatform\Metadata\Get;
  19. use App\Entity\Trait\IdTrait,
  20.     App\Entity\Trait\UuidTrait,
  21.     App\Entity\Trait\TimestampableTrait,
  22.     App\Entity\Trait\TranslationTrait,
  23.     App\Entity\Interface\TranslationInterface,
  24.     App\Repository\JournalArticleContentBlockTranslationRepository;
  25. use App\Attribute\Sanitizeable,
  26.     App\Enum\Language,
  27.     App\Enum\JournalArticleContentBlockType;
  28. #[ApiResource(
  29.     security'is_granted("' Journal::class . '")',
  30.     operations: [ new Get() ],
  31.     extraProperties: ['standard_put' => false],
  32. )]
  33. #[ORM\Entity(repositoryClassJournalArticleContentBlockTranslationRepository::class)]
  34. class JournalArticleContentBlockTranslation implements TranslationInterfaceThumbableInterface
  35. {
  36.     use IdTrait,
  37.         UuidTrait,
  38.         TimestampableTrait,
  39.         TranslationTrait,
  40.         ImageThumbableTrait;
  41.     #[ApiProperty(description'Title')]
  42.     #[Groups(['read''write'])]
  43.     #[ORM\Column(typeTypes::STRINGlength511nullabletrue)]
  44.     private ?string $title null;
  45.     #[ApiProperty(description'Text')]
  46.     #[Groups([
  47.         JournalArticleContentBlockType::TYPE_TEXT,
  48.         JournalArticleContentBlockType::TYPE_TEXT_WITH_PHOTO,
  49.         JournalArticleContentBlockType::TYPE_TABLE,
  50.         JournalArticleContentBlockType::TYPE_FILES,
  51.     ])]
  52.     #[Sanitizeable]
  53.     #[ORM\Column(typeTypes::TEXTnullabletrue)]
  54.     private ?string $text null;
  55.     #[ApiProperty(description'Table')]
  56.     #[Groups([
  57.         JournalArticleContentBlockType::TYPE_TABLE,
  58.     ])]
  59.     #[Sanitizeable]
  60.     #[ORM\Column(typeTypes::TEXTnullabletrue)]
  61.     private ?string $htmlTable null;
  62.     #[ApiProperty(description'Alt')]
  63.     #[Groups([
  64.         JournalArticleContentBlockType::TYPE_TEXT_WITH_PHOTO,
  65.         JournalArticleContentBlockType::TYPE_FILES
  66.     ])]
  67.     #[ORM\Column(typeTypes::STRINGlength511nullabletrue)]
  68.     protected ?string $alt null;
  69.     #[ApiProperty(description'Media position'writableLinkfalse)]
  70.     #[Groups([JournalArticleContentBlockType::TYPE_TEXT_WITH_PHOTO])]
  71.     #[ORM\Column(
  72.         typeTypes::STRING,
  73.         enumTypeMediaPosition::class,
  74.         length255,
  75.         options: ['default' => MediaPosition::CENTER]
  76.     )]
  77.     protected MediaPosition $mediaPosition MediaPosition::CENTER;
  78.     #[ApiProperty(description'Media'writableLinkfalse)]
  79.     #[Groups([JournalArticleContentBlockType::TYPE_TEXT_WITH_PHOTO])]
  80.     #[CustomAssert\ValidMediaMimeType(types: ['image/*'])]
  81.     #[ORM\ManyToOne(targetEntityMedia::class, cascade: ['persist''remove'])]
  82.     #[ORM\JoinColumn(onDelete'set null')]
  83.     protected ?Media $media null;
  84.     #[ApiProperty(description'Thumbs'writableLinkfalse)]
  85.     #[Groups([JournalArticleContentBlockType::TYPE_TEXT_WITH_PHOTO])]
  86.     #[Assert\Type(typeCollection::class)]
  87.     #[ORM\OneToMany(
  88.         targetEntityThumbInterface::class,
  89.         mappedBy'parent',
  90.         cascade: ['persist''remove'],
  91.         orphanRemovaltrue
  92.     )]
  93.     #[ORM\OrderBy(['property' => 'asc''name' => 'asc''type' => 'asc'])]
  94.     protected Collection $thumbs;
  95.     #[ApiProperty(description'Files')]
  96.     #[Groups([
  97.         JournalArticleContentBlockType::TYPE_FILES,
  98.     ])]
  99.     #[Assert\Valid]
  100.     #[ORM\OneToMany(
  101.         targetEntityJournalArticleContentBlockTranslationFile::class,
  102.         mappedBy'parent',
  103.         cascade: ['persist''remove'],
  104.         orphanRemovaltrue
  105.     )]
  106.     #[ORM\OrderBy(['ord' => 'asc'])]
  107.     protected Collection $files;
  108.     public function __construct(JournalArticleContentBlock $parentLanguage $lang)
  109.     {
  110.         $this->setUuid();
  111.         $this->parent $parent;
  112.         $this->lang $lang;
  113.         $this->thumbs = new ArrayCollection();
  114.         $this->files = new ArrayCollection();
  115.         $this->createdAt = new \DateTimeImmutable();
  116.         $this->updatedAt = new \DateTimeImmutable();
  117.         $parent->addTranslation($this);
  118.     }
  119.     public function getTitle(): ?string
  120.     {
  121.         return $this->title;
  122.     }
  123.     public function setTitle(?string $title): self
  124.     {
  125.         $this->title $title;
  126.         return $this;
  127.     }
  128.     public function getText(): ?string
  129.     {
  130.         return $this->text;
  131.     }
  132.     public function setText(?string $text): self
  133.     {
  134.         $this->text $text;
  135.         return $this;
  136.     }
  137.     public function getHtmlTable(): ?string
  138.     {
  139.         return $this->htmlTable;
  140.     }
  141.     public function setHtmlTable(?string $htmlTable): self
  142.     {
  143.         $this->htmlTable $htmlTable;
  144.         return $this;
  145.     }
  146.     public function getAlt(): ?string
  147.     {
  148.         return $this->alt;
  149.     }
  150.     public function setAlt(?string $alt): self
  151.     {
  152.         $this->alt $alt;
  153.         return $this;
  154.     }
  155.     public function getMediaPosition(): MediaPosition
  156.     {
  157.         return $this->mediaPosition;
  158.     }
  159.     public function setMediaPosition(MediaPosition $mediaPosition): self
  160.     {
  161.         $this->mediaPosition $mediaPosition;
  162.         return $this;
  163.     }
  164.     public function getFiles(): Collection
  165.     {
  166.         return $this->files;
  167.     }
  168.     public function addFile(JournalArticleContentBlockTranslationFile $file): self
  169.     {
  170.         if ($this->files->contains($file)) {
  171.             return $this;
  172.         }
  173.         $this->files[] = $file;
  174.         $this->reorderCollection('files');
  175.         return $this;
  176.     }
  177.     public function removeFile(JournalArticleContentBlockTranslationFile $file): self
  178.     {
  179.         $this->files->removeElement($file);
  180.         $this->reorderCollection('files');
  181.         return $this;
  182.     }
  183.     public function resetFiles(): self
  184.     {
  185.         $this->files = new ArrayCollection();
  186.         return $this;
  187.     }
  188.     private function reorderCollection(string $name): void
  189.     {
  190.         $collection $this->{$name}->toArray();
  191.         $this->{$name}->clear();
  192.         uasort($collection, function (OrderableInterface $aOrderableInterface $b): int {
  193.             if ($a->getOrd() === $b->getOrd()) {
  194.                 return 0;
  195.             }
  196.             return $a->getOrd() < $b->getOrd() ? -1;
  197.         });
  198.         foreach ($collection as $item) {
  199.             $this->{$name}[] = $item;
  200.         }
  201.         return;
  202.     }
  203.     public function clone(JournalArticleContentBlock $parent): self
  204.     {
  205.         $clone = clone $this;
  206.         $clone->id null;
  207.         $clone->setUuid();
  208.         $clone->parent $parent;
  209.         $clone->parent->addTranslation($clone);
  210.         ClassUtils::cloneCollection($this$clone'thumbs');
  211.         ClassUtils::cloneCollection($this$clone'files');
  212.         $clone->createdAt = new \DateTimeImmutable();
  213.         $clone->updatedAt = new \DateTimeImmutable();
  214.         return $clone;
  215.     }
  216. }