src/Entity/ProductTranslation.php line 23

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use Doctrine\ORM\Event\LifecycleEventArgs;
  4. use Doctrine\ORM\Mapping as ORM;
  5. use App\Entity\Behaviors\TranslationTrait;
  6. use App\Entity\Behaviors\SluggableTrait;
  7. use Knp\DoctrineBehaviors\Contract\Entity\TranslationInterface;
  8. use Knp\DoctrineBehaviors\Contract\Entity\SluggableInterface;
  9. use Doctrine\ORM\Mapping\Index;
  10. use JMS\Serializer\Annotation as Serializer;
  11. /**
  12. * @ORM\Table(
  13. * uniqueConstraints={@ORM\UniqueConstraint(name="sluging", columns={"locale", "slug"})},
  14. * indexes={@Index(name="locale_slug", columns={"locale","slug"})}
  15. * )
  16. * @ORM\Entity(repositoryClass="App\Repository\TranslationRepository")
  17. * @ORM\HasLifecycleCallbacks()
  18. * @ORM\EntityListeners({"App\Listener\EntityListener"})
  19. */
  20. class ProductTranslation implements TranslationInterface, SluggableInterface
  21. {
  22. use SluggableTrait;
  23. use TranslationTrait;
  24. protected $isEmptyOff = true;
  25. /**
  26. * @Serializer\Groups({"api"})
  27. * @Serializer\SerializedName("name")
  28. * @ORM\Column(type="string", length=190)
  29. */
  30. protected $name;
  31. /**
  32. * @ORM\Column(type="string", length=190)
  33. */
  34. protected $slug;
  35. /**
  36. * @ORM\Column(type="string", length=2)
  37. */
  38. protected $locale;
  39. /**
  40. * @ORM\Column(type="text", nullable=true)
  41. */
  42. private $description;
  43. /**
  44. * @ORM\Column(type="string", length=255, nullable=true)
  45. */
  46. protected $metaDescription;
  47. /**
  48. * @ORM\Column(type="string", length=255, nullable=true)
  49. */
  50. protected $metaKeywords;
  51. /**
  52. * @ORM\Column(type="datetime")
  53. */
  54. private $createts;
  55. /**
  56. * @ORM\ManyToOne(targetEntity="App\Entity\User")
  57. * @ORM\JoinColumn(nullable=true)
  58. */
  59. private $createUser;
  60. /**
  61. * @ORM\Column(type="datetime", nullable=true)
  62. */
  63. private $editts;
  64. /**
  65. * @ORM\ManyToOne(targetEntity="App\Entity\User")
  66. */
  67. private $editUser;
  68. public function getName(): ?string
  69. {
  70. return $this->name;
  71. }
  72. public function setName(string $name): self
  73. {
  74. $this->name = $name;
  75. return $this;
  76. }
  77. public function getDescription(): ?string
  78. {
  79. return $this->description;
  80. }
  81. public function setDescription(?string $description): self
  82. {
  83. $this->description = $description;
  84. return $this;
  85. }
  86. public function getMetaDescription(): ?string
  87. {
  88. return $this->metaDescription;
  89. }
  90. public function setMetaDescription(?string $metaDescription): self
  91. {
  92. $this->metaDescription = $metaDescription;
  93. return $this;
  94. }
  95. public function getMetaKeywords(): ?string
  96. {
  97. return $this->metaKeywords;
  98. }
  99. public function setMetaKeywords(?string $metaKeywords): self
  100. {
  101. $this->metaKeywords = $metaKeywords;
  102. return $this;
  103. }
  104. public function getCreatets(): ?\DateTimeInterface
  105. {
  106. return $this->createts;
  107. }
  108. public function setCreatets(\DateTimeInterface $createts): self
  109. {
  110. $this->createts = $createts;
  111. return $this;
  112. }
  113. public function getCreateUser(): ?User
  114. {
  115. return $this->createUser;
  116. }
  117. public function setCreateUser(?User $createUser): self
  118. {
  119. $this->createUser = $createUser;
  120. return $this;
  121. }
  122. public function getEditts(): ?\DateTimeInterface
  123. {
  124. return $this->editts;
  125. }
  126. public function setEditts(?\DateTimeInterface $editts): self
  127. {
  128. $this->editts = $editts;
  129. return $this;
  130. }
  131. public function getEditUser(): ?User
  132. {
  133. return $this->editUser;
  134. }
  135. public function setEditUser(?User $editUser): self
  136. {
  137. $this->editUser = $editUser;
  138. return $this;
  139. }
  140. public function getCode( )
  141. {
  142. return $this->getTranslatable()->getCode();
  143. }
  144. /**
  145. * Returns an array of the fields used to generate the slug.
  146. *
  147. * @return array
  148. */
  149. public function getSluggableFields(): array
  150. {
  151. return [ 'code','name' ];
  152. }
  153. public function generateSlug(): void { }
  154. /**
  155. * @ORM\PreUpdate()
  156. * @ORM\PrePersist()
  157. */
  158. public function updateSlug( LifecycleEventArgs $eventArgs )
  159. {
  160. $translatable = $this->getTranslatable();
  161. $oldSlug = $translatable->getSlug();
  162. if ( $this->getRegenerateSlugOnUpdate() || empty( $this->slug ) ) {
  163. $fields = $this->getSluggableFields();
  164. $values = [];
  165. foreach ( $fields as $field ) {
  166. if ( property_exists( $this, $field ) ) {
  167. $val = $this->{$field};
  168. } else {
  169. $methodName = 'get' . ucfirst( $field );
  170. if ( method_exists( $this, $methodName ) ) {
  171. $val = $this->{$methodName}();
  172. } else {
  173. $val = null;
  174. }
  175. }
  176. $values[] = $val;
  177. }
  178. $this->slug = $this->generateSlugValue( $values );
  179. if ( $oldSlug !== $this->slug ) {
  180. $repo = $eventArgs->getEntityManager()->getRepository( get_class( $this ) );
  181. $repo->updateSlug( $this );
  182. }
  183. }
  184. }
  185. }