src/Entity/Core/Category.php line 13

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