src/Entity/JournalArticleContentBlockTranslation.php line 41
<?php
namespace App\Entity;
use App\Entity\Interface\OrderableInterface;
use App\Entity\Interface\ThumbableInterface;
use App\Entity\Interface\ThumbInterface,
App\Enum\MediaPosition,
App\Validator\Constraints as CustomAssert;
use App\Entity\Trait\Media\ImageThumbableTrait;
use App\Util\ClassUtils;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection,
Doctrine\ORM\Mapping as ORM,
Doctrine\DBAL\Types\Types;
use Symfony\Component\Serializer\Annotation\Groups,
Symfony\Component\Validator\Constraints as Assert;;
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\Interface\TranslationInterface,
App\Repository\JournalArticleContentBlockTranslationRepository;
use App\Attribute\Sanitizeable,
App\Enum\Language,
App\Enum\JournalArticleContentBlockType;
#[ApiResource(
security: 'is_granted("' . Journal::class . '")',
operations: [ new Get() ],
extraProperties: ['standard_put' => false],
)]
#[ORM\Entity(repositoryClass: JournalArticleContentBlockTranslationRepository::class)]
class JournalArticleContentBlockTranslation implements TranslationInterface, ThumbableInterface
{
use IdTrait,
UuidTrait,
TimestampableTrait,
TranslationTrait,
ImageThumbableTrait;
#[ApiProperty(description: 'Title')]
#[Groups(['read', 'write'])]
#[ORM\Column(type: Types::STRING, length: 511, nullable: true)]
private ?string $title = null;
#[ApiProperty(description: 'Text')]
#[Groups([
JournalArticleContentBlockType::TYPE_TEXT,
JournalArticleContentBlockType::TYPE_TEXT_WITH_PHOTO,
JournalArticleContentBlockType::TYPE_TABLE,
JournalArticleContentBlockType::TYPE_FILES,
])]
#[Sanitizeable]
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $text = null;
#[ApiProperty(description: 'Table')]
#[Groups([
JournalArticleContentBlockType::TYPE_TABLE,
])]
#[Sanitizeable]
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $htmlTable = null;
#[ApiProperty(description: 'Alt')]
#[Groups([
JournalArticleContentBlockType::TYPE_TEXT_WITH_PHOTO,
JournalArticleContentBlockType::TYPE_FILES
])]
#[ORM\Column(type: Types::STRING, length: 511, nullable: true)]
protected ?string $alt = null;
#[ApiProperty(description: 'Media position', writableLink: false)]
#[Groups([JournalArticleContentBlockType::TYPE_TEXT_WITH_PHOTO])]
#[ORM\Column(
type: Types::STRING,
enumType: MediaPosition::class,
length: 255,
options: ['default' => MediaPosition::CENTER]
)]
protected MediaPosition $mediaPosition = MediaPosition::CENTER;
#[ApiProperty(description: 'Media', writableLink: false)]
#[Groups([JournalArticleContentBlockType::TYPE_TEXT_WITH_PHOTO])]
#[CustomAssert\ValidMediaMimeType(types: ['image/*'])]
#[ORM\ManyToOne(targetEntity: Media::class, cascade: ['persist', 'remove'])]
#[ORM\JoinColumn(onDelete: 'set null')]
protected ?Media $media = null;
#[ApiProperty(description: 'Thumbs', writableLink: false)]
#[Groups([JournalArticleContentBlockType::TYPE_TEXT_WITH_PHOTO])]
#[Assert\Type(type: Collection::class)]
#[ORM\OneToMany(
targetEntity: ThumbInterface::class,
mappedBy: 'parent',
cascade: ['persist', 'remove'],
orphanRemoval: true
)]
#[ORM\OrderBy(['property' => 'asc', 'name' => 'asc', 'type' => 'asc'])]
protected Collection $thumbs;
#[ApiProperty(description: 'Files')]
#[Groups([
JournalArticleContentBlockType::TYPE_FILES,
])]
#[Assert\Valid]
#[ORM\OneToMany(
targetEntity: JournalArticleContentBlockTranslationFile::class,
mappedBy: 'parent',
cascade: ['persist', 'remove'],
orphanRemoval: true
)]
#[ORM\OrderBy(['ord' => 'asc'])]
protected Collection $files;
public function __construct(JournalArticleContentBlock $parent, Language $lang)
{
$this->setUuid();
$this->parent = $parent;
$this->lang = $lang;
$this->thumbs = new ArrayCollection();
$this->files = 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;
return $this;
}
public function getText(): ?string
{
return $this->text;
}
public function setText(?string $text): self
{
$this->text = $text;
return $this;
}
public function getHtmlTable(): ?string
{
return $this->htmlTable;
}
public function setHtmlTable(?string $htmlTable): self
{
$this->htmlTable = $htmlTable;
return $this;
}
public function getAlt(): ?string
{
return $this->alt;
}
public function setAlt(?string $alt): self
{
$this->alt = $alt;
return $this;
}
public function getMediaPosition(): MediaPosition
{
return $this->mediaPosition;
}
public function setMediaPosition(MediaPosition $mediaPosition): self
{
$this->mediaPosition = $mediaPosition;
return $this;
}
public function getFiles(): Collection
{
return $this->files;
}
public function addFile(JournalArticleContentBlockTranslationFile $file): self
{
if ($this->files->contains($file)) {
return $this;
}
$this->files[] = $file;
$this->reorderCollection('files');
return $this;
}
public function removeFile(JournalArticleContentBlockTranslationFile $file): self
{
$this->files->removeElement($file);
$this->reorderCollection('files');
return $this;
}
public function resetFiles(): self
{
$this->files = new ArrayCollection();
return $this;
}
private function reorderCollection(string $name): void
{
$collection = $this->{$name}->toArray();
$this->{$name}->clear();
uasort($collection, function (OrderableInterface $a, OrderableInterface $b): int {
if ($a->getOrd() === $b->getOrd()) {
return 0;
}
return $a->getOrd() < $b->getOrd() ? -1 : 1;
});
foreach ($collection as $item) {
$this->{$name}[] = $item;
}
return;
}
public function clone(JournalArticleContentBlock $parent): self
{
$clone = clone $this;
$clone->id = null;
$clone->setUuid();
$clone->parent = $parent;
$clone->parent->addTranslation($clone);
ClassUtils::cloneCollection($this, $clone, 'thumbs');
ClassUtils::cloneCollection($this, $clone, 'files');
$clone->createdAt = new \DateTimeImmutable();
$clone->updatedAt = new \DateTimeImmutable();
return $clone;
}
}