src/ProductBundle/Entity/Attribute.php line 22

Open in your IDE?
  1. <?php
  2. namespace App\ProductBundle\Entity;
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. use Doctrine\Common\Collections\Collection;
  5. use Doctrine\Common\Collections\Criteria;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use App\ProductBundle\Entity\Translation\AttributeTranslation;
  8. use App\ServiceBundle\Entity\Locale;
  9. use PN\LocaleBundle\Model\LocaleTrait;
  10. use PN\ServiceBundle\Interfaces\DateTimeInterface;
  11. use PN\ServiceBundle\Model\DateTimeTrait;
  12. use PN\ServiceBundle\Model\VirtualDeleteTrait;
  13. use PN\LocaleBundle\Model\Translatable;
  14. /**
  15. * @ORM\HasLifecycleCallbacks
  16. * @ORM\Table(name="product_attribute")
  17. * @ORM\Entity(repositoryClass="App\ProductBundle\Repository\AttributeRepository")
  18. */
  19. class Attribute implements Translatable, DateTimeInterface
  20. {
  21. use VirtualDeleteTrait,
  22. DateTimeTrait,
  23. LocaleTrait;
  24. const TYPE_NUMBER = "number";
  25. const TYPE_TEXT = "text";
  26. const TYPE_DROPDOWN = "dropdown";
  27. const TYPE_CHECKBOX = "checkbox";
  28. public static $types = [
  29. 'Number' => self::TYPE_NUMBER,
  30. 'Text' => self::TYPE_TEXT,
  31. 'Single Choice' => self::TYPE_DROPDOWN,
  32. 'Multiple Choices' => self::TYPE_CHECKBOX,
  33. ];
  34. /**
  35. * @var int
  36. *
  37. * @ORM\Column(name="id", type="integer")
  38. * @ORM\Id
  39. * @ORM\GeneratedValue(strategy="AUTO")
  40. */
  41. private $id;
  42. /**
  43. * @ORM\ManyToOne(targetEntity="Category", inversedBy="attributes",cascade={"persist"})
  44. */
  45. private $category;
  46. /**
  47. * @var string
  48. *
  49. * @ORM\Column(name="title", type="string", length=100)
  50. */
  51. private $title;
  52. /**
  53. * @var int
  54. *
  55. * @ORM\Column(name="type", type="string", length=45)
  56. */
  57. private $type;
  58. /**
  59. * @var string
  60. * @ORM\Column(name="tarteb", type="smallint", nullable=true)
  61. */
  62. private $tarteb;
  63. /**
  64. * @ORM\Column(name="search", type="boolean")
  65. */
  66. private $search = false;
  67. /**
  68. * @ORM\Column(name="mandatory", type="boolean")
  69. */
  70. private $mandatory = false;
  71. /**
  72. * @ORM\OneToMany(targetEntity="App\ProductBundle\Entity\Translation\AttributeTranslation", mappedBy="translatable", cascade={"ALL"}, orphanRemoval=true)
  73. */
  74. private $translations;
  75. /**
  76. * @ORM\OneToMany(targetEntity="SubAttribute", mappedBy="attribute", cascade={"persist"})
  77. */
  78. private $subAttributes;
  79. /**
  80. * @ORM\OneToMany(targetEntity="ProductHasAttribute", mappedBy="attribute", cascade={"persist"})
  81. */
  82. private $productHasAttributes;
  83. public function __construct()
  84. {
  85. $this->translations = new ArrayCollection();
  86. $this->subAttributes = new ArrayCollection();
  87. $this->productHasAttributes = new ArrayCollection();
  88. }
  89. /**
  90. * Now we tell doctrine that before we persist or update we call the updatedTimestamps() function.
  91. *
  92. * @ORM\PrePersist
  93. */
  94. public function updatedTimestamps()
  95. {
  96. if ($this->getCreated() == null) {
  97. $this->setCreated(new \DateTime(date('Y-m-d H:i:s')));
  98. }
  99. }
  100. public function getTypeName()
  101. {
  102. return array_search($this->getType(), self::$types);
  103. }
  104. /**
  105. * @return Collection|SubAttribute[]
  106. */
  107. public function getSubAttributes($deleted=null): Collection
  108. {
  109. $criteria = Criteria::create();
  110. if ($deleted == null) {
  111. $criteria->where(Criteria::expr()->eq('deleted', null));
  112. }
  113. return $this->subAttributes->matching($criteria);
  114. }
  115. public function getObj()
  116. {
  117. return [
  118. "id" => (int)$this->getId(),
  119. "title" => (string)$this->getTitle(),
  120. ];
  121. }
  122. public function __toString()
  123. {
  124. return $this->getTitle();
  125. }
  126. public function getId(): ?int
  127. {
  128. return $this->id;
  129. }
  130. public function getTitle(): ?string
  131. {
  132. return !$this->currentTranslation ? $this->title : $this->currentTranslation->getTitle();
  133. }
  134. public function setTitle(string $title): self
  135. {
  136. $this->title = $title;
  137. return $this;
  138. }
  139. public function getType(): ?string
  140. {
  141. return $this->type;
  142. }
  143. public function setType(string $type): self
  144. {
  145. $this->type = $type;
  146. return $this;
  147. }
  148. public function getTarteb(): ?int
  149. {
  150. return $this->tarteb;
  151. }
  152. public function setTarteb(?int $tarteb): self
  153. {
  154. $this->tarteb = $tarteb;
  155. return $this;
  156. }
  157. public function getSearch(): ?bool
  158. {
  159. return $this->search;
  160. }
  161. public function setSearch(bool $search): self
  162. {
  163. $this->search = $search;
  164. return $this;
  165. }
  166. public function getMandatory(): ?bool
  167. {
  168. return $this->mandatory;
  169. }
  170. public function setMandatory(bool $mandatory): self
  171. {
  172. $this->mandatory = $mandatory;
  173. return $this;
  174. }
  175. public function getCategory(): ?Category
  176. {
  177. return $this->category;
  178. }
  179. public function setCategory(?Category $category): self
  180. {
  181. $this->category = $category;
  182. return $this;
  183. }
  184. /**
  185. * @return Collection|AttributeTranslation[]
  186. */
  187. public function getTranslations(): Collection
  188. {
  189. return $this->translations;
  190. }
  191. public function addTranslation(AttributeTranslation $translation): self
  192. {
  193. if (!$this->translations->contains($translation)) {
  194. $this->translations[] = $translation;
  195. $translation->setTranslatable($this);
  196. }
  197. return $this;
  198. }
  199. public function removeTranslation(AttributeTranslation $translation): self
  200. {
  201. if ($this->translations->removeElement($translation)) {
  202. // set the owning side to null (unless already changed)
  203. if ($translation->getTranslatable() === $this) {
  204. $translation->setTranslatable(null);
  205. }
  206. }
  207. return $this;
  208. }
  209. public function addSubAttribute(SubAttribute $subAttribute): self
  210. {
  211. if (!$this->subAttributes->contains($subAttribute)) {
  212. $this->subAttributes[] = $subAttribute;
  213. $subAttribute->setAttribute($this);
  214. }
  215. return $this;
  216. }
  217. public function removeSubAttribute(SubAttribute $subAttribute): self
  218. {
  219. if ($this->subAttributes->removeElement($subAttribute)) {
  220. // set the owning side to null (unless already changed)
  221. if ($subAttribute->getAttribute() === $this) {
  222. $subAttribute->setAttribute(null);
  223. }
  224. }
  225. return $this;
  226. }
  227. /**
  228. * @return Collection|ProductHasAttribute[]
  229. */
  230. public function getProductHasAttributes(): Collection
  231. {
  232. return $this->productHasAttributes;
  233. }
  234. public function addProductHasAttribute(ProductHasAttribute $productHasAttribute): self
  235. {
  236. if (!$this->productHasAttributes->contains($productHasAttribute)) {
  237. $this->productHasAttributes[] = $productHasAttribute;
  238. $productHasAttribute->setAttribute($this);
  239. }
  240. return $this;
  241. }
  242. public function removeProductHasAttribute(ProductHasAttribute $productHasAttribute): self
  243. {
  244. if ($this->productHasAttributes->removeElement($productHasAttribute)) {
  245. // set the owning side to null (unless already changed)
  246. if ($productHasAttribute->getAttribute() === $this) {
  247. $productHasAttribute->setAttribute(null);
  248. }
  249. }
  250. return $this;
  251. }
  252. }