<?php
namespace App\Entity;
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;
use JMS\Serializer\Annotation as Serializer;
/**
* @ORM\Table(
* uniqueConstraints={@ORM\UniqueConstraint(name="sluging", columns={"locale", "slug"})},
* indexes={@Index(name="locale_slug", columns={"locale","slug"})}
* )
* @ORM\Entity(repositoryClass="App\Repository\TranslationRepository")
* @ORM\HasLifecycleCallbacks()
* @ORM\EntityListeners({"App\Listener\EntityListener"})
*/
class ProductTranslation implements TranslationInterface, SluggableInterface
{
use SluggableTrait;
use TranslationTrait;
protected $isEmptyOff = true;
/**
* @Serializer\Groups({"api"})
* @Serializer\SerializedName("name")
* @ORM\Column(type="string", length=190)
*/
protected $name;
/**
* @ORM\Column(type="string", length=190)
*/
protected $slug;
/**
* @ORM\Column(type="string", length=2)
*/
protected $locale;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $description;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
protected $metaDescription;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
protected $metaKeywords;
/**
* @ORM\Column(type="datetime")
*/
private $createts;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\User")
* @ORM\JoinColumn(nullable=true)
*/
private $createUser;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $editts;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\User")
*/
private $editUser;
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(?string $description): self
{
$this->description = $description;
return $this;
}
public function getMetaDescription(): ?string
{
return $this->metaDescription;
}
public function setMetaDescription(?string $metaDescription): self
{
$this->metaDescription = $metaDescription;
return $this;
}
public function getMetaKeywords(): ?string
{
return $this->metaKeywords;
}
public function setMetaKeywords(?string $metaKeywords): self
{
$this->metaKeywords = $metaKeywords;
return $this;
}
public function getCreatets(): ?\DateTimeInterface
{
return $this->createts;
}
public function setCreatets(\DateTimeInterface $createts): self
{
$this->createts = $createts;
return $this;
}
public function getCreateUser(): ?User
{
return $this->createUser;
}
public function setCreateUser(?User $createUser): self
{
$this->createUser = $createUser;
return $this;
}
public function getEditts(): ?\DateTimeInterface
{
return $this->editts;
}
public function setEditts(?\DateTimeInterface $editts): self
{
$this->editts = $editts;
return $this;
}
public function getEditUser(): ?User
{
return $this->editUser;
}
public function setEditUser(?User $editUser): self
{
$this->editUser = $editUser;
return $this;
}
public function getCode( )
{
return $this->getTranslatable()->getCode();
}
/**
* Returns an array of the fields used to generate the slug.
*
* @return array
*/
public function getSluggableFields(): array
{
return [ 'code','name' ];
}
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 );
}
}
}
}