src/Entity/JournalArticleMetadataTranslation.php line 30
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM,
Doctrine\DBAL\Types\Types,
Doctrine\Common\Collections\Collection,
Doctrine\Common\Collections\ArrayCollection;
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;
use App\Enum\Language,
App\Attribute\Sanitizeable,
App\Attribute\DenormalizationPriority,
App\Repository\JournalArticleMetadataTranslationRepository;
#[ApiResource(
security: 'is_granted("' . Journal::class . '")',
operations: [ new Get() ],
extraProperties: ['standard_put' => false],
)]
#[ORM\Entity(repositoryClass: JournalArticleMetadataTranslationRepository::class)]
class JournalArticleMetadataTranslation
{
use IdTrait,
UuidTrait,
TimestampableTrait;
#[ApiProperty(description: 'Parent')]
#[ORM\ManyToOne(targetEntity: JournalArticle::class, inversedBy: 'metadataTranslations')]
#[ORM\JoinColumn(onDelete: 'cascade', nullable: false)]
private JournalArticle $parent;
#[ApiProperty(description: 'Title')]
#[Groups(['read', 'write'])]
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $title = null;
#[ApiProperty(description: 'Abstract')]
#[Groups(['read:' . self::class, 'write'])]
#[Sanitizeable]
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $abstract = null;
#[ApiProperty(description: 'Translation language')]
#[ORM\Column(
type: Types::STRING,
enumType: Language::class,
length: 7,
options: ['default' => Language::EN]
)]
private Language $lang = Language::EN;
#[ApiProperty(description: 'Is keywords import reseting previous')]
#[Groups(['read:' . self::class, 'write'])]
#[DenormalizationPriority(DenormalizationPriority::TOP)]
private bool $isKeywordsImportResetingPrevious = false;
#[ApiProperty(description: 'Keywords')]
#[ORM\OneToMany(
targetEntity: JournalArticleMetadataTranslationKeyword::class,
mappedBy: 'parent',
cascade: ['persist', 'remove'],
orphanRemoval: true
)]
#[ORM\OrderBy(['ord' => 'desc'])]
private Collection $keywords;
public function __construct(JournalArticle $parent, Language $lang)
{
$this->setUuid();
$this->parent = $parent;
$this->lang = $lang;
$this->keywords = new ArrayCollection();
$this->createdAt = new \DateTimeImmutable();
$this->updatedAt = new \DateTimeImmutable();
$parent->addMetadataTranslation($this);
}
public function getParent(): JournalArticle
{
return $this->parent;
}
public function getTitle(): ?string
{
return $this->title;
}
public function getRawTitle(): ?string
{
return strip_tags($this->title);
}
public function setTitle(?string $title): self
{
$this->title = $title;
/** @var JournalArticle $parent*/
$parent = $this->parent;
if ($parent->getSlugTitleLanguage() !== null) {
$nativeTitleLang = $parent->getSlugTitleLanguage();
} else {
$nativeTitleLang = $parent->getNativeMetadataLanguage();
}
if ($this->lang === $nativeTitleLang) {
$parent
->setNativeTitle($title)
->setNativeTitleRaw(strip_tags($title));
}
return $this;
}
public function getAbstract(): ?string
{
return $this->abstract;
}
public function setAbstract(?string $abstract): self
{
$this->abstract = $abstract;
return $this;
}
public function getLang(): Language
{
return $this->lang;
}
public function setIsKeywordsImportResetingPrevious(bool $isKeywordsImportResetingPrevious)
{
$this->isKeywordsImportResetingPrevious = $isKeywordsImportResetingPrevious;
return $this;
}
public function getIsKeywordsImportResetingPrevious(): bool
{
return true;
}
#[Groups(['write'])]
public function setImportedKeywords(?string $importedKeywords): self
{
if (! $importedKeywords) {
return $this;
}
$importedKeywords = str_replace(';', ',', $importedKeywords);
/** @var JournalArticleTranslation|null */
$adjacentTranslation = $this->parent->getTranslation($this->lang);
if ($this->isKeywordsImportResetingPrevious) {
$adjacentTranslation?->setMetaKeywords($importedKeywords);
$this->resetKeywords();
} elseif (! empty($adjacentTranslation?->getMetaKeywords())) {
$adjacentTranslation?->setMetaKeywords("{$adjacentTranslation->getMetaKeywords()}, {$importedKeywords}");
} else {
$adjacentTranslation?->setMetaKeywords($importedKeywords);
}
$keywords = array_map(
fn(string $keyword) => trim($keyword),
explode(',', $importedKeywords)
);
$keywords = array_filter(
$keywords,
fn(string $keyword) => ! empty($keyword)
);
foreach($keywords as $keyword) {
new JournalArticleMetadataTranslationKeyword($this, $keyword);
}
$ord = count($this->keywords);
foreach($this->keywords as $keyword) {
$keyword->setOrd($ord--);
}
return $this;
}
/**
* @return Collection|JournalArticleMetadataTranslationKeyword[]
*/
public function getKeywords(): Collection
{
return $this->keywords;
}
public function addKeyword(JournalArticleMetadataTranslationKeyword $keyword): self
{
if ($this->keywords->contains($keyword)) {
return $this;
}
$this->keywords[] = $keyword;
return $this;
}
public function removeKeyword(JournalArticleMetadataTranslationKeyword $keyword): self
{
$this->keywords->removeElement($keyword);
return $this;
}
public function resetKeywords(): self
{
$this->keywords = new ArrayCollection();
return $this;
}
public function clone(JournalArticle $parent): self
{
$clone = clone $this;
$clone->id = null;
$clone->setUuid();
$clone->parent = $parent;
$clone->parent->addMetadataTranslation($clone);
$clone->resetKeywords();
$clone->createdAt = new \DateTimeImmutable();
$clone->updatedAt = new \DateTimeImmutable();
return $clone;
}
}