src/Entity/JournalArticleFileTranslation.php line 31
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM,
Doctrine\Common\Collections\Collection,
Doctrine\Common\Collections\ArrayCollection;
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\Entity\Interface\OrderableInterface;
use App\Enum\Language,
App\Repository\JournalArticleFileTranslationRepository;
#[ApiResource(
security: 'is_granted("' . Journal::class . '")',
operations: [ new Get() ],
extraProperties: ['standard_put' => false],
)]
#[ORM\Entity(repositoryClass: JournalArticleFileTranslationRepository::class)]
class JournalArticleFileTranslation implements TranslationInterface
{
use IdTrait,
UuidTrait,
TimestampableTrait,
TranslationTrait;
#[ApiProperty(description: 'Files')]
#[Groups(['read:' . self::class, 'write'])]
#[Assert\Valid]
#[ORM\OneToMany(
targetEntity: JournalArticleFileTranslationFile::class,
mappedBy: 'parent',
cascade: ['persist', 'remove'],
orphanRemoval: true
)]
#[ORM\OrderBy(['ord' => 'asc'])]
private Collection $files;
public function __construct(JournalArticleFile $parent, Language $lang)
{
$this->setUuid();
$this->parent = $parent;
$this->lang = $lang;
$this->files = new ArrayCollection();
$this->createdAt = new \DateTimeImmutable();
$this->updatedAt = new \DateTimeImmutable();
$parent->addTranslation($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;
}
/**
* @return Collection|JournalArticleFileTranslationFile[]
*/
public function getFiles(): Collection
{
return $this->files;
}
public function addFile(JournalArticleFileTranslationFile $file): self
{
if ($this->files->contains($file)) {
return $this;
}
$this->files[] = $file;
$this->reorderCollection('files');
return $this;
}
public function removeFile(JournalArticleFileTranslationFile $file): self
{
$this->files->removeElement($file);
$this->reorderCollection('files');
return $this;
}
public function getVisibleFiles(): Collection
{
return $this->files->filter(
fn (JournalArticleFileTranslationFile $file) => $file->getMedia() !== null
);
}
public function getHasFiles(): bool
{
foreach($this->files as $file) {
if (! $file->getMedia()) {
continue;
}
return true;
}
return false;
}
}