src/Entity/AffiliationTranslation.php line 37

  1. <?php
  2. namespace App\Entity;
  3. use App\Entity\Interface\TranslatableInterface;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\ORM\Mapping as ORM,
  6.     Doctrine\DBAL\Types\Types;
  7. use Symfony\Component\Serializer\Annotation\Groups;
  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\Trait\Media\ImageThumbableTrait,
  16.     App\Entity\Trait\SlugTrait,
  17.     App\Entity\Interface\TranslationInterface,
  18.     App\Entity\Interface\ThumbableInterface,
  19.     App\Enum\Language,
  20.     App\Repository\AffiliationTranslationRepository,
  21.     App\Validator\Constraints as CustomAssert;
  22. /**
  23.  * @method Affiliation getParent()
  24.  */
  25. #[ApiResource(
  26.     security'is_granted("' Affiliation::class . '")',
  27.     operations: [ new Get() ],
  28.     extraProperties: ['standard_put' => false],
  29. )]
  30. #[ORM\Entity(repositoryClassAffiliationTranslationRepository::class)]
  31. class AffiliationTranslation implements TranslationInterfaceThumbableInterface
  32. {
  33.     use IdTrait,
  34.         UuidTrait,
  35.         TimestampableTrait,
  36.         TranslationTrait,
  37.         SlugTrait,
  38.         ImageThumbableTrait;
  39.     #[ApiProperty(description'Title')]
  40.     #[Groups(['read''read:stats''write'])]
  41.     #[ORM\Column(typeTypes::STRINGlength1023nullabletrue)]
  42.     private ?string $title null;
  43.     #[ApiProperty(description'Text')]
  44.     #[Groups(['read:' self::class, 'write'])]
  45.     #[ORM\Column(typeTypes::TEXTnullabletrue)]
  46.     private ?string $text null;
  47.     #[ApiProperty(description'Alt')]
  48.     #[Groups(['read:' self::class, 'write'])]
  49.     #[ORM\Column(typeTypes::STRINGlength1023nullabletrue)]
  50.     private ?string $alt null;
  51.     #[ApiProperty(description'Alt')]
  52.     #[Groups(['read:' self::class, 'write'])]
  53.     #[ORM\Column(typeTypes::STRINGlength1023nullabletrue)]
  54.     private ?string $alt2 null;
  55.     #[ApiProperty(description'Media'writableLinkfalse)]
  56.     #[Groups(['read:' self::class, 'write'])]
  57.     #[CustomAssert\ValidMediaMimeType(types: ['image/*'])]
  58.     #[ORM\ManyToOne(targetEntityMedia::class, cascade: ['persist''remove'])]
  59.     #[ORM\JoinColumn(onDelete'set null')]
  60.     protected ?Media $media2 null;
  61.     public function __construct(Affiliation $parentLanguage $lang)
  62.     {
  63.         $this->setUuid();
  64.         $this->parent $parent;
  65.         $this->lang $lang;
  66.         $this->thumbs = new ArrayCollection();
  67.         $this->createdAt = new \DateTimeImmutable();
  68.         $this->updatedAt = new \DateTimeImmutable();
  69.         $parent->addTranslation($this);
  70.     }
  71.     public function getTitle(): ?string
  72.     {
  73.         return $this->title;
  74.     }
  75.     public function setTitle(?string $title): self
  76.     {
  77.         $this->title $title;
  78.         /** @var Affiliation */
  79.         $parent $this->parent;
  80.         if ($this->lang === $parent->getNativeLanguage()) {
  81.             $parent->setNativeTitle($title);
  82.         }
  83.         return $this;
  84.     }
  85.     public function getText(): ?string
  86.     {
  87.         return $this->text;
  88.     }
  89.     public function setText(?string $text): self
  90.     {
  91.         $this->text $text;
  92.         return $this;
  93.     }
  94.     public function getAlt(): ?string
  95.     {
  96.         return $this->alt;
  97.     }
  98.     public function setAlt(?string $alt): self
  99.     {
  100.         $this->alt $alt;
  101.         return $this;
  102.     }
  103.     public function getMedia2(): ?Media
  104.     {
  105.         return $this->media2;
  106.     }
  107.     public function setMedia2(?Media $media2): self
  108.     {
  109.         $this->setImageThumbable('media2'$media2);
  110.         return $this;
  111.     }
  112.     #[Groups(['read'])]
  113.     public function getFullAffiliationTitle(): ?string
  114.     {
  115.         return $this->getParentAffiliationTitle($this->getParent(), $this->lang) . $this->title;
  116.     }
  117.     private function getParentAffiliationTitle(Affiliation $affiliationLanguage $lang): ?string
  118.     {
  119.         if (! $affiliation->getParent()) {
  120.             return null;
  121.         }
  122.         /** @var AffiliationTranslation */
  123.         $langTranslation $affiliation->getTranslations()[$lang->value];
  124.         return $this->getParentAffiliationTitle($affiliation->getParent(), $lang)
  125.             . $langTranslation?->getTitle()
  126.             . ' - ';
  127.     }
  128.     #[Groups(['read'])]
  129.     public function getFullAffiliationTitleWithRoot(): ?string
  130.     {
  131.         if (!$this->getParent()->getRoot() || $this->getParent()->getRoot() === $this->getParent()) {
  132.             return $this->getTitle();
  133.         }
  134.         return sprintf('%s (%s)'$this->getTitle(), $this->getParent()->getRoot()->getTranslation($this->getLang())->getTitle());
  135.     }
  136.     public function getAlt2(): ?string
  137.     {
  138.         return $this->alt2;
  139.     }
  140.     public function setAlt2(?string $alt2): void
  141.     {
  142.         $this->alt2 $alt2;
  143.     }
  144. }