src/Entity/Regate.php line 23

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\RegateRepository;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use JMS\Serializer\Annotation as Serializer;
  8. use JMS\Serializer\Annotation\Expose;
  9. use JMS\Serializer\Annotation\Groups;
  10. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  11. /**
  12. * @ORM\Entity(repositoryClass=RegateRepository::class)
  13. * @UniqueEntity(
  14. * fields={"affectation"},
  15. * errorPath="slug",
  16. * message="Une régate avec cette affectation existe déjà."
  17. * )
  18. * @Serializer\ExclusionPolicy("ALL")
  19. */
  20. class Regate
  21. {
  22. /**
  23. * @ORM\Id
  24. * @ORM\GeneratedValue
  25. * @ORM\Column(type="integer")
  26. *
  27. * @Expose
  28. * @Groups ({
  29. * "regate:id",
  30. * "regate",
  31. * "regate-list",
  32. * "export_user_datatable",
  33. * "export_regate_datatable"
  34. * })
  35. */
  36. private ?int $id = NULL;
  37. /**
  38. * @ORM\Column(type="string", length=64)
  39. *
  40. * @Expose
  41. * @Groups ({
  42. * "regate:name",
  43. * "regate",
  44. * "user",
  45. * "regate-list",
  46. * "export_regate_datatable",
  47. * })
  48. */
  49. private ?string $name = NULL;
  50. /**
  51. * @ORM\ManyToOne(targetEntity=Regate::class, inversedBy="childs")
  52. * @ORM\JoinColumn(onDelete="CASCADE")
  53. *
  54. * @Expose
  55. * @Groups ({
  56. * "regate",
  57. * "regate-list",
  58. * "export_regate_datatable"
  59. * })
  60. */
  61. private ?Regate $parent = NULL;
  62. /**
  63. * @ORM\OneToMany(targetEntity=Regate::class, mappedBy="parent", cascade={"remove"})
  64. *
  65. * @Expose
  66. * @Groups ({
  67. * "regate:childs",
  68. * "regate",
  69. * "regate-list"
  70. * })
  71. */
  72. private Collection $childs;
  73. /**
  74. * @ORM\Column(type="string", length=255)
  75. *
  76. * @Expose
  77. * @Groups ({
  78. * "regate:affectation",
  79. * "regate",
  80. * "user",
  81. * "regate-list",
  82. * "export_user_datatable",
  83. * "export_regate_datatable",
  84. * })
  85. */
  86. private ?string $affectation = NULL;
  87. /**
  88. * @ORM\Column(type="string", length=64)
  89. *
  90. * @Expose
  91. * @Groups ({
  92. * "regate:level",
  93. * "regate",
  94. * "user",
  95. * "regate-list",
  96. * "export_regate_datatable"})
  97. */
  98. private ?string $level = NULL;
  99. /**
  100. * @ORM\OneToMany(targetEntity=User::class, mappedBy="regate")
  101. *
  102. * @Expose
  103. * @Groups ({
  104. * "regate:members",
  105. * "regate-list"
  106. * })
  107. */
  108. private Collection $members;
  109. /**
  110. * @ORM\OneToOne(targetEntity=User::class, mappedBy="responsableRegate", cascade={"persist", "remove"})
  111. *
  112. * @Expose
  113. * @Groups ({
  114. * "user"
  115. * })
  116. *
  117. * responsable
  118. */
  119. private ?User $responsable = NULL;
  120. public function __construct()
  121. {
  122. $this->childs = new ArrayCollection();
  123. $this->members = new ArrayCollection();
  124. }
  125. public function __toString()
  126. {
  127. return $this->getLevel() . ' : ' . $this->name . ' ' . $this->affectation;
  128. }
  129. public function getLevel(): ?string
  130. {
  131. return $this->level;
  132. }
  133. public function setLevel(string $level): self
  134. {
  135. $this->level = $level;
  136. return $this;
  137. }
  138. /**
  139. * Permet de retourner un array avec les différents lvl attribués à la régate
  140. *
  141. * @return false|string[]
  142. */
  143. public function getLevels()
  144. {
  145. return explode(';', $this->level);
  146. }
  147. public function setLevels(array $levels): self
  148. {
  149. $this->level = implode(';', $levels);
  150. return $this;
  151. }
  152. public function getId(): ?int
  153. {
  154. return $this->id;
  155. }
  156. public function getName(): ?string
  157. {
  158. return $this->name;
  159. }
  160. public function setName(string $name): self
  161. {
  162. $this->name = $name;
  163. return $this;
  164. }
  165. /**
  166. * @return Collection|self[]
  167. */
  168. public function getChilds(): Collection
  169. {
  170. return $this->childs;
  171. }
  172. public function addChild(self $child): self
  173. {
  174. if(!$this->childs->contains($child))
  175. {
  176. $this->childs[] = $child;
  177. $child->setParent($this);
  178. }
  179. return $this;
  180. }
  181. public function removeChild(self $child): self
  182. {
  183. if($this->childs->removeElement($child))
  184. {
  185. // set the owning side to null (unless already changed)
  186. if($child->getParent() === $this)
  187. {
  188. $child->setParent(NULL);
  189. }
  190. }
  191. return $this;
  192. }
  193. public function getParent(): ?self
  194. {
  195. return $this->parent;
  196. }
  197. public function setParent(?self $parent): self
  198. {
  199. $this->parent = $parent;
  200. return $this;
  201. }
  202. public function getAffectation(): ?string
  203. {
  204. return $this->affectation;
  205. }
  206. public function setAffectation(string $affectation): self
  207. {
  208. $this->affectation = $affectation;
  209. return $this;
  210. }
  211. /**
  212. * @return Collection|User[]
  213. */
  214. public function getMembers(): Collection
  215. {
  216. return $this->members;
  217. }
  218. public function addMember(User $member): self
  219. {
  220. if(!$this->members->contains($member))
  221. {
  222. $this->members[] = $member;
  223. $member->setRegate($this);
  224. }
  225. return $this;
  226. }
  227. public function removeMember(User $member): self
  228. {
  229. if($this->members->removeElement($member))
  230. {
  231. // set the owning side to null (unless already changed)
  232. if($member->getRegate() === $this)
  233. {
  234. $member->setRegate(NULL);
  235. }
  236. }
  237. return $this;
  238. }
  239. /**
  240. * @Serializer\VirtualProperty()
  241. * @Serializer\SerializedName("get_first_parent_affectation")
  242. * @return string|null
  243. *
  244. * @Expose()
  245. * @Groups ({
  246. * "regate:get_first_parent_affectation",
  247. * "regate",
  248. * "regate-list",
  249. * "export_regate_datatable"
  250. * })
  251. */
  252. public function getFirstParentAffectation()
  253. {
  254. return $this->parent ? $this->parent->getAffectation() : '';
  255. }
  256. public function getResponsable(): ?User
  257. {
  258. return $this->responsable;
  259. }
  260. public function setResponsable(?User $responsable): self
  261. {
  262. // unset the owning side of the relation if necessary
  263. if($responsable === NULL && $this->responsable !== NULL)
  264. {
  265. $this->responsable->setResponsableRegate(NULL);
  266. }
  267. // set the owning side of the relation if necessary
  268. if($responsable !== NULL && $responsable->getResponsableRegate() !== $this)
  269. {
  270. $responsable->setResponsableRegate($this);
  271. }
  272. $this->responsable = $responsable;
  273. return $this;
  274. }
  275. }