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. $this->childs[] = $child;
  176. $child->setParent($this);
  177. }
  178. return $this;
  179. }
  180. public function removeChild(self $child): self
  181. {
  182. if ($this->childs->removeElement($child)) {
  183. // set the owning side to null (unless already changed)
  184. if ($child->getParent() === $this) {
  185. $child->setParent(NULL);
  186. }
  187. }
  188. return $this;
  189. }
  190. public function getParent(): ?self
  191. {
  192. return $this->parent;
  193. }
  194. public function setParent(?self $parent): self
  195. {
  196. $this->parent = $parent;
  197. return $this;
  198. }
  199. public function getAffectation(): ?string
  200. {
  201. return $this->affectation;
  202. }
  203. public function setAffectation(string $affectation): self
  204. {
  205. $this->affectation = $affectation;
  206. return $this;
  207. }
  208. /**
  209. * @return Collection|User[]
  210. */
  211. public function getMembers(): Collection
  212. {
  213. return $this->members;
  214. }
  215. public function addMember(User $member): self
  216. {
  217. if (!$this->members->contains($member)) {
  218. $this->members[] = $member;
  219. $member->setRegate($this);
  220. }
  221. return $this;
  222. }
  223. public function removeMember(User $member): self
  224. {
  225. if ($this->members->removeElement($member)) {
  226. // set the owning side to null (unless already changed)
  227. if ($member->getRegate() === $this) {
  228. $member->setRegate(NULL);
  229. }
  230. }
  231. return $this;
  232. }
  233. /**
  234. * @Serializer\VirtualProperty()
  235. * @Serializer\SerializedName("get_first_parent_affectation")
  236. * @return string|null
  237. *
  238. * @Expose()
  239. * @Groups ({
  240. * "regate:get_first_parent_affectation",
  241. * "regate",
  242. * "regate-list",
  243. * "export_regate_datatable"
  244. * })
  245. */
  246. public function getFirstParentAffectation()
  247. {
  248. return $this->parent ? $this->parent->getAffectation() : '';
  249. }
  250. public function getResponsable(): ?User
  251. {
  252. return $this->responsable;
  253. }
  254. public function setResponsable(?User $responsable): self
  255. {
  256. // unset the owning side of the relation if necessary
  257. if ($responsable === NULL && $this->responsable !== NULL) {
  258. $this->responsable->setResponsableRegate(NULL);
  259. }
  260. // set the owning side of the relation if necessary
  261. if ($responsable !== NULL && $responsable->getResponsableRegate() !== $this) {
  262. $responsable->setResponsableRegate($this);
  263. }
  264. $this->responsable = $responsable;
  265. return $this;
  266. }
  267. }