vendor/roothirsch/tuer24-bundle/src/Entity/Tuer24Category.php line 13

Open in your IDE?
  1. <?php
  2. namespace Roothirsch\Tuer24Bundle\Entity;
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. use Doctrine\Common\Collections\Collection;
  5. use Doctrine\ORM\Mapping as ORM;
  6. use Symfony\Component\Validator\Constraints as Assert;
  7. #[ORM\Entity(repositoryClass: "Roothirsch\Tuer24Bundle\Repository\Tuer24CategoryRepository")]
  8. #[ORM\Table(name: 'roothirsch_tuer24_category')]
  9. #[Assert\Callback(['Roothirsch\Tuer24Bundle\Validator\TranslationValidator', 'validateCategoryTranslations'])]
  10. class Tuer24Category
  11. {
  12. #[ORM\Id]
  13. #[ORM\GeneratedValue]
  14. #[ORM\Column(type: 'integer')]
  15. private $id;
  16. #[ORM\ManyToOne(targetEntity: "Tuer24Category", inversedBy: "children")]
  17. #[ORM\JoinColumn(name: "parent_id", referencedColumnName: "id", onDelete: "SET NULL")]
  18. private $parent;
  19. #[ORM\OneToMany(targetEntity: "Tuer24Category", mappedBy: "parent")]
  20. #[ORM\OrderBy(["position" => "ASC"])]
  21. private $children;
  22. #[ORM\Column(type: 'string', length: 255)]
  23. #[Assert\Length(max: 255)]
  24. private $name = '';
  25. #[ORM\Column(type: 'string', length: 100, unique: true)]
  26. #[Assert\NotBlank]
  27. #[Assert\Length(max: 100)]
  28. private $code;
  29. #[ORM\Column(type: 'text', nullable: true)]
  30. private $description;
  31. #[ORM\Column(type: 'integer')]
  32. private $position = 0;
  33. #[ORM\Column(type: 'boolean')]
  34. private $enabled = true;
  35. #[ORM\Column(type: 'string', length: 255, nullable: true)]
  36. private $imagePath;
  37. #[ORM\OneToMany(targetEntity: "Tuer24CategoryTranslation", mappedBy: "category", cascade: ["persist", "remove"], orphanRemoval: true)]
  38. private $translations;
  39. #[ORM\ManyToMany(targetEntity: "Tuer24Product", mappedBy: "categories")]
  40. private $products;
  41. /**
  42. * Constructor
  43. */
  44. public function __construct()
  45. {
  46. $this->children = new ArrayCollection();
  47. $this->translations = new ArrayCollection();
  48. $this->products = new ArrayCollection();
  49. $this->name = ''; // Initialize with empty string
  50. $this->code = ''; // Initialize with empty string
  51. }
  52. /**
  53. * Get id
  54. *
  55. * @return int|null
  56. */
  57. public function getId(): ?int
  58. {
  59. return $this->id;
  60. }
  61. /**
  62. * Get parent
  63. *
  64. * @return Tuer24Category|null
  65. */
  66. public function getParent(): ?self
  67. {
  68. return $this->parent;
  69. }
  70. /**
  71. * Set parent
  72. *
  73. * @param Tuer24Category|null $parent
  74. * @return $this
  75. */
  76. public function setParent(?self $parent): self
  77. {
  78. $this->parent = $parent;
  79. return $this;
  80. }
  81. /**
  82. * Get children
  83. *
  84. * @return Collection|Tuer24Category[]
  85. */
  86. public function getChildren(): Collection
  87. {
  88. return $this->children;
  89. }
  90. /**
  91. * Add child
  92. *
  93. * @param Tuer24Category $child
  94. * @return $this
  95. */
  96. public function addChild(self $child): self
  97. {
  98. if (!$this->children->contains($child)) {
  99. $this->children[] = $child;
  100. $child->setParent($this);
  101. }
  102. return $this;
  103. }
  104. /**
  105. * Remove child
  106. *
  107. * @param Tuer24Category $child
  108. * @return $this
  109. */
  110. public function removeChild(self $child): self
  111. {
  112. if ($this->children->removeElement($child)) {
  113. // set the owning side to null (unless already changed)
  114. if ($child->getParent() === $this) {
  115. $child->setParent(null);
  116. }
  117. }
  118. return $this;
  119. }
  120. /**
  121. * Get name
  122. *
  123. * @return string|null
  124. */
  125. public function getName(): ?string
  126. {
  127. return $this->name;
  128. }
  129. /**
  130. * Set name
  131. *
  132. * @param string $name
  133. * @return $this
  134. */
  135. public function setName(string $name): self
  136. {
  137. $this->name = $name;
  138. return $this;
  139. }
  140. /**
  141. * Get code
  142. *
  143. * @return string|null
  144. */
  145. public function getCode(): ?string
  146. {
  147. return $this->code;
  148. }
  149. /**
  150. * Set code
  151. *
  152. * @param string $code
  153. * @return $this
  154. */
  155. public function setCode(string $code): self
  156. {
  157. $this->code = $code;
  158. return $this;
  159. }
  160. /**
  161. * Get description
  162. *
  163. * @return string|null
  164. */
  165. public function getDescription(): ?string
  166. {
  167. return $this->description;
  168. }
  169. /**
  170. * Set description
  171. *
  172. * @param string|null $description
  173. * @return $this
  174. */
  175. public function setDescription(?string $description): self
  176. {
  177. $this->description = $description;
  178. return $this;
  179. }
  180. /**
  181. * Get position
  182. *
  183. * @return int|null
  184. */
  185. public function getPosition(): ?int
  186. {
  187. return $this->position;
  188. }
  189. /**
  190. * Set position
  191. *
  192. * @param int $position
  193. * @return $this
  194. */
  195. public function setPosition(int $position): self
  196. {
  197. $this->position = $position;
  198. return $this;
  199. }
  200. /**
  201. * Is enabled
  202. *
  203. * @return bool|null
  204. */
  205. public function isEnabled(): ?bool
  206. {
  207. return $this->enabled;
  208. }
  209. /**
  210. * Set enabled
  211. *
  212. * @param bool $enabled
  213. * @return $this
  214. */
  215. public function setEnabled(bool $enabled): self
  216. {
  217. $this->enabled = $enabled;
  218. return $this;
  219. }
  220. /**
  221. * Get translations
  222. *
  223. * @return Collection|Tuer24CategoryTranslation[]
  224. */
  225. public function getTranslations(): Collection
  226. {
  227. return $this->translations;
  228. }
  229. /**
  230. * Add translation
  231. *
  232. * @param Tuer24CategoryTranslation $translation
  233. * @return $this
  234. */
  235. public function addTranslation(Tuer24CategoryTranslation $translation): self
  236. {
  237. if (!$this->translations->contains($translation)) {
  238. $this->translations[] = $translation;
  239. $translation->setCategory($this);
  240. }
  241. return $this;
  242. }
  243. /**
  244. * Remove translation
  245. *
  246. * @param Tuer24CategoryTranslation $translation
  247. * @return $this
  248. */
  249. public function removeTranslation(Tuer24CategoryTranslation $translation): self
  250. {
  251. if ($this->translations->removeElement($translation)) {
  252. // set the owning side to null (unless already changed)
  253. if ($translation->getCategory() === $this) {
  254. $translation->setCategory(null);
  255. }
  256. }
  257. return $this;
  258. }
  259. /**
  260. * Get products
  261. *
  262. * @return Collection|Tuer24Product[]
  263. */
  264. public function getProducts(): Collection
  265. {
  266. return $this->products;
  267. }
  268. /**
  269. * Add product
  270. *
  271. * @param Tuer24Product $product
  272. * @return $this
  273. */
  274. public function addProduct(Tuer24Product $product): self
  275. {
  276. if (!$this->products->contains($product)) {
  277. $this->products[] = $product;
  278. }
  279. return $this;
  280. }
  281. /**
  282. * Remove product
  283. *
  284. * @param Tuer24Product $product
  285. * @return $this
  286. */
  287. public function removeProduct(Tuer24Product $product): self
  288. {
  289. $this->products->removeElement($product);
  290. return $this;
  291. }
  292. /**
  293. * Get translation by locale
  294. *
  295. * @param string $locale
  296. * @return Tuer24CategoryTranslation|null
  297. */
  298. public function getTranslation(string $locale): ?Tuer24CategoryTranslation
  299. {
  300. foreach ($this->translations as $translation) {
  301. if ($translation->getLocale() === $locale) {
  302. return $translation;
  303. }
  304. }
  305. return null;
  306. }
  307. /**
  308. * Returns a string representation of this category
  309. *
  310. * @return string
  311. */
  312. public function __toString(): string
  313. {
  314. return $this->getName() ?: 'New Category';
  315. }
  316. /**
  317. * Get image path
  318. *
  319. * @return string|null
  320. */
  321. public function getImagePath(): ?string
  322. {
  323. return $this->imagePath;
  324. }
  325. /**
  326. * Set image path
  327. *
  328. * @param string|null $imagePath
  329. * @return $this
  330. */
  331. public function setImagePath(?string $imagePath): self
  332. {
  333. $this->imagePath = $imagePath;
  334. return $this;
  335. }
  336. }