src/Entity/AffiliationTranslation.php line 37
<?php
namespace App\Entity;
use App\Entity\Interface\TranslatableInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM,
Doctrine\DBAL\Types\Types;
use Symfony\Component\Serializer\Annotation\Groups;
use ApiPlatform\Metadata\ApiProperty,
ApiPlatform\Metadata\ApiResource,
ApiPlatform\Metadata\Get;
use App\Entity\Trait\IdTrait,
App\Entity\Trait\UuidTrait,
App\Entity\Trait\TimestampableTrait,
App\Entity\Trait\TranslationTrait,
App\Entity\Trait\Media\ImageThumbableTrait,
App\Entity\Trait\SlugTrait,
App\Entity\Interface\TranslationInterface,
App\Entity\Interface\ThumbableInterface,
App\Enum\Language,
App\Repository\AffiliationTranslationRepository,
App\Validator\Constraints as CustomAssert;
/**
* @method Affiliation getParent()
*/
#[ApiResource(
security: 'is_granted("' . Affiliation::class . '")',
operations: [ new Get() ],
extraProperties: ['standard_put' => false],
)]
#[ORM\Entity(repositoryClass: AffiliationTranslationRepository::class)]
class AffiliationTranslation implements TranslationInterface, ThumbableInterface
{
use IdTrait,
UuidTrait,
TimestampableTrait,
TranslationTrait,
SlugTrait,
ImageThumbableTrait;
#[ApiProperty(description: 'Title')]
#[Groups(['read', 'read:stats', 'write'])]
#[ORM\Column(type: Types::STRING, length: 1023, nullable: true)]
private ?string $title = null;
#[ApiProperty(description: 'Text')]
#[Groups(['read:' . self::class, 'write'])]
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $text = null;
#[ApiProperty(description: 'Alt')]
#[Groups(['read:' . self::class, 'write'])]
#[ORM\Column(type: Types::STRING, length: 1023, nullable: true)]
private ?string $alt = null;
#[ApiProperty(description: 'Alt')]
#[Groups(['read:' . self::class, 'write'])]
#[ORM\Column(type: Types::STRING, length: 1023, nullable: true)]
private ?string $alt2 = null;
#[ApiProperty(description: 'Media', writableLink: false)]
#[Groups(['read:' . self::class, 'write'])]
#[CustomAssert\ValidMediaMimeType(types: ['image/*'])]
#[ORM\ManyToOne(targetEntity: Media::class, cascade: ['persist', 'remove'])]
#[ORM\JoinColumn(onDelete: 'set null')]
protected ?Media $media2 = null;
public function __construct(Affiliation $parent, Language $lang)
{
$this->setUuid();
$this->parent = $parent;
$this->lang = $lang;
$this->thumbs = new ArrayCollection();
$this->createdAt = new \DateTimeImmutable();
$this->updatedAt = new \DateTimeImmutable();
$parent->addTranslation($this);
}
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(?string $title): self
{
$this->title = $title;
/** @var Affiliation */
$parent = $this->parent;
if ($this->lang === $parent->getNativeLanguage()) {
$parent->setNativeTitle($title);
}
return $this;
}
public function getText(): ?string
{
return $this->text;
}
public function setText(?string $text): self
{
$this->text = $text;
return $this;
}
public function getAlt(): ?string
{
return $this->alt;
}
public function setAlt(?string $alt): self
{
$this->alt = $alt;
return $this;
}
public function getMedia2(): ?Media
{
return $this->media2;
}
public function setMedia2(?Media $media2): self
{
$this->setImageThumbable('media2', $media2);
return $this;
}
#[Groups(['read'])]
public function getFullAffiliationTitle(): ?string
{
return $this->getParentAffiliationTitle($this->getParent(), $this->lang) . $this->title;
}
private function getParentAffiliationTitle(Affiliation $affiliation, Language $lang): ?string
{
if (! $affiliation->getParent()) {
return null;
}
/** @var AffiliationTranslation */
$langTranslation = $affiliation->getTranslations()[$lang->value];
return $this->getParentAffiliationTitle($affiliation->getParent(), $lang)
. $langTranslation?->getTitle()
. ' - ';
}
#[Groups(['read'])]
public function getFullAffiliationTitleWithRoot(): ?string
{
if (!$this->getParent()->getRoot() || $this->getParent()->getRoot() === $this->getParent()) {
return $this->getTitle();
}
return sprintf('%s (%s)', $this->getTitle(), $this->getParent()->getRoot()->getTranslation($this->getLang())->getTitle());
}
public function getAlt2(): ?string
{
return $this->alt2;
}
public function setAlt2(?string $alt2): void
{
$this->alt2 = $alt2;
}
}