<?php
namespace App\Entity\Web;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\Mapping as ORM;
use App\Entity\Behaviors\TranslationTrait;
use App\Entity\Behaviors\SluggableTrait;
use Knp\DoctrineBehaviors\Contract\Entity\TranslationInterface;
use Knp\DoctrineBehaviors\Contract\Entity\SluggableInterface;
use Doctrine\ORM\Mapping\Index;
/**
* @ORM\Table(name="web_sites_translation",uniqueConstraints={@ORM\UniqueConstraint(name="sluging", columns={"locale", "slug"})},
* indexes={@Index(name="locale_slug", columns={"locale","slug"})})
* @ORM\Entity(repositoryClass="App\Repository\TranslationRepository")
* @ORM\EntityListeners({"App\Listener\EntityListener"})
* @ORM\HasLifecycleCallbacks()
*/
class SiteTranslation implements TranslationInterface, SluggableInterface
{
use TranslationTrait;
use SluggableTrait;
protected $isEmptyOff = true;
/**
* @ORM\Column(type="string", length=255)
*/
private $title;
/**
* @ORM\Column(type="text")
*/
private $description;
/**
* @ORM\Column(type="string", length=2)
*/
protected $locale;
/**
* @ORM\Column(type="string", length=190)
*/
protected $slug;
public function __construct($locale = null) {
if($locale !== null){
$this->setLocale($locale);
}
}
public function getId(): ?int
{
return $this->id;
}
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(string $title): self
{
$this->title = $title;
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(string $description): self
{
$this->description = $description;
return $this;
}
public function getSluggableFields(): array
{
return ['title'];
}
public function generateSlug(): void { }
/**
* @ORM\PreUpdate()
* @ORM\PrePersist()
*/
public function updateSlug( LifecycleEventArgs $eventArgs )
{
$translatable = $this->getTranslatable();
$oldSlug = $translatable->getSlug();
if ( $this->getRegenerateSlugOnUpdate() || empty( $this->slug ) ) {
$fields = $this->getSluggableFields();
$values = [];
foreach ( $fields as $field ) {
if ( property_exists( $this, $field ) ) {
$val = $this->{$field};
} else {
$methodName = 'get' . ucfirst( $field );
if ( method_exists( $this, $methodName ) ) {
$val = $this->{$methodName}();
} else {
$val = null;
}
}
$values[] = $val;
}
$this->slug = $this->generateSlugValue( $values );
if ( $oldSlug !== $this->slug ) {
$repo = $eventArgs->getEntityManager()->getRepository( get_class( $this ) );
$repo->updateSlug( $this );
}
}
}
}