src/CoreBundle/Entity/Category.php line 14

Open in your IDE?
  1. <?php
  2. namespace CoreBundle\Entity;
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. use Doctrine\ORM\Mapping as ORM;
  5. use JsonSerializable;
  6. use DateTime;
  7. /**
  8. * @ORM\Table("category")
  9. * @ORM\Entity(repositoryClass="CoreBundle\Entity\CategoryRepository")
  10. */
  11. class Category implements JsonSerializable
  12. {
  13. /**
  14. * @var int
  15. *
  16. * @ORM\Column(name="id", type="integer")
  17. * @ORM\Id
  18. * @ORM\GeneratedValue(strategy="AUTO")
  19. */
  20. private $id;
  21. /**
  22. * @var string
  23. *
  24. * @ORM\Column(name="name", type="string", length=255, unique=true)
  25. */
  26. private $name;
  27. /**
  28. * @var string
  29. *
  30. * @ORM\Column(name="display_name", type="string", length=255, nullable=true)
  31. */
  32. private $displayName;
  33. /**
  34. * Type of a category;
  35. * 0 - normal
  36. * 1 - screening
  37. *
  38. * @var int
  39. *
  40. * @ORM\Column(name="type", type="integer", nullable=true, options={"default"=0})
  41. */
  42. private $type = 0;
  43. /**
  44. * @var Topic[]
  45. *
  46. * @ORM\OneToMany(targetEntity="Topic", mappedBy="category", cascade={"persist"})
  47. * @ORM\OrderBy({"sortOrder" = "ASC"})
  48. */
  49. private $topics;
  50. /**
  51. * @var bool $enabled
  52. *
  53. * @ORM\Column(name="enabled", type="boolean", unique=false, options={"default" : true})
  54. */
  55. private $enabled = true;
  56. /**
  57. * @var bool $show_in_self_study
  58. *
  59. * @ORM\Column(name="show_in_self_study", type="boolean", unique=false, options={"default" : true})
  60. */
  61. private $showInSelfStudy = false;
  62. /**
  63. * @var bool $show_in_self_study
  64. *
  65. * @ORM\Column(name="show_in_main_system", type="boolean", unique=false, options={"default" : true})
  66. */
  67. private $showInMainSystem = true;
  68. /**
  69. * @var bool $show_in_self_study
  70. *
  71. * @ORM\Column(name="show_only_to_demo_user", type="boolean", unique=false, options={"default" : false})
  72. */
  73. private $showOnlyToDemoUser = false;
  74. /**
  75. * @var int
  76. *
  77. * @ORM\Column(name="sort_order", type="integer", nullable=true, options={"default"=0})
  78. */
  79. private $sortOrder;
  80. /**
  81. * @var ?DateTime
  82. *
  83. * @ORM\Column(name="deleted_at", type="datetime", nullable=true)
  84. */
  85. private ?DateTime $deletedAt;
  86. public function __construct()
  87. {
  88. $this->topics = new ArrayCollection();
  89. }
  90. /**
  91. * Get id.
  92. *
  93. * @return int
  94. */
  95. public function getId()
  96. {
  97. return $this->id;
  98. }
  99. /**
  100. * Set name.
  101. *
  102. * @param string $name
  103. *
  104. * @return Category
  105. */
  106. public function setName($name)
  107. {
  108. $this->name = $name;
  109. return $this;
  110. }
  111. /**
  112. * Get name.
  113. *
  114. * @return string
  115. */
  116. public function getName()
  117. {
  118. return $this->name;
  119. }
  120. /**
  121. * @return Topic[]
  122. */
  123. public function getTopics()
  124. {
  125. return $this->topics;
  126. }
  127. /**
  128. * @return Topic[]
  129. */
  130. public function getEnabledTopics()
  131. {
  132. return $this->topics->filter(function (Topic $topic) {
  133. return $topic->isEnabled();
  134. });
  135. }
  136. /**
  137. * @param Topic $topic
  138. *
  139. * @return Category
  140. */
  141. public function addTopic(Topic $topic)
  142. {
  143. $topic->setCategory($this);
  144. $this->topics->add($topic);
  145. return $this;
  146. }
  147. /**
  148. * @param Topic $topic
  149. *
  150. * @return bool
  151. */
  152. public function removeTopic(Topic $topic)
  153. {
  154. $topic->setCategory(null);
  155. return $this->topics->removeElement($topic);
  156. }
  157. /**
  158. * @return int
  159. */
  160. public function getType()
  161. {
  162. return $this->type;
  163. }
  164. /**
  165. * @param int $type
  166. */
  167. public function setType($type)
  168. {
  169. $this->type = $type;
  170. }
  171. /**
  172. * @return bool
  173. */
  174. public function isEnabled(): bool
  175. {
  176. return $this->enabled;
  177. }
  178. /**
  179. * @param bool $enabled
  180. */
  181. public function setEnabled(bool $enabled): void
  182. {
  183. $this->enabled = $enabled;
  184. }
  185. /**
  186. * @return int
  187. */
  188. public function getSortOrder()
  189. {
  190. return $this->sortOrder;
  191. }
  192. /**
  193. * @param int $sortOrder
  194. */
  195. public function setSortOrder($sortOrder)
  196. {
  197. $this->sortOrder = $sortOrder;
  198. }
  199. public function getTopicNames()
  200. {
  201. $topic_obj_array = $this->getTopics()->toArray();
  202. $topics = array_map(fn($e) => $e->getName(), $topic_obj_array);
  203. return implode(', ', $topics);
  204. }
  205. public function isShownInMainSystem(): bool
  206. {
  207. return $this->showInMainSystem;
  208. }
  209. public function setShowInMainSystem(bool $showInMainSystem): void
  210. {
  211. $this->showInMainSystem = $showInMainSystem;
  212. }
  213. public function getDisplayName(): string
  214. {
  215. if ($this->displayName)
  216. {
  217. return $this->displayName;
  218. }
  219. return $this->name;
  220. }
  221. public function setDisplayName(?string $displayName): void
  222. {
  223. $this->displayName = $displayName;
  224. }
  225. /**
  226. * @return DateTime|null
  227. */
  228. public function getDeletedAt(): ?DateTime
  229. {
  230. return $this->deletedAt;
  231. }
  232. /**
  233. * @param DateTime|null $deletedAt
  234. * @return void
  235. */
  236. public function setDeletedAt(?DateTime $deletedAt): void
  237. {
  238. $this->deletedAt = $deletedAt;
  239. }
  240. public function JsonSerialize(): mixed
  241. {
  242. return [
  243. "id" => $this->getId(),
  244. "name" => $this->getName(),
  245. "type" => $this->getType(),
  246. ];
  247. }
  248. public function __toString(): string
  249. {
  250. return $this->getName();
  251. }
  252. }