Person.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Entity\Person;
  4. use ApiPlatform\Core\Annotation\ApiResource;
  5. use App\Entity\Core\ContactPoint;
  6. use App\Entity\Core\File;
  7. use App\Repository\Person\PersonRepository;
  8. use Doctrine\Common\Collections\ArrayCollection;
  9. use Doctrine\Common\Collections\Collection;
  10. use Doctrine\ORM\Mapping as ORM;
  11. use JetBrains\PhpStorm\Pure;
  12. use Symfony\Component\Security\Core\User\UserInterface;
  13. use Symfony\Component\Validator\Constraints as Assert;
  14. use Symfony\Component\Serializer\Annotation\Groups;
  15. /**
  16. * Personne physique ou morale
  17. */
  18. #[ApiResource(
  19. collectionOperations: [],
  20. itemOperations: [
  21. "get" => ["security" => "is_granted('ROLE_USERS_VIEW')"],
  22. ]
  23. )]
  24. #[ORM\Entity(repositoryClass: PersonRepository::class)]
  25. class Person implements UserInterface
  26. {
  27. #[ORM\Id]
  28. #[ORM\Column]
  29. #[ORM\GeneratedValue]
  30. private ?int $id = null;
  31. #[ORM\Column(length: 180, unique: true, nullable: true)]
  32. private ?string $username = null;
  33. private array $roles = [];
  34. #[ORM\Column(nullable: true)]
  35. private ?string $password = null;
  36. #[ORM\Column(length: 255, nullable: true)]
  37. #[Groups(["access_people_ref", "access_address"])]
  38. private ?string $name = null;
  39. #[ORM\Column(length: 255, nullable: true)]
  40. #[Groups(["access_people_ref", "access_address"])]
  41. private ?string $givenName = null;
  42. #[ORM\ManyToMany(targetEntity: ContactPoint::class, mappedBy: 'person')]
  43. private Collection $contactPoints;
  44. #[ORM\OneToMany( mappedBy: 'person', targetEntity: PersonAddressPostal::class, orphanRemoval: true)]
  45. #[Groups("access_address")]
  46. private Collection $personAddressPostal;
  47. #[ORM\Column(nullable: true)]
  48. #[Assert\Choice(callback: ['\App\Enum\Person\GenderEnum', 'toArray'], message: 'invalid-gender')]
  49. private ?string $gender = null;
  50. #[ORM\ManyToOne(inversedBy: 'personImages')]
  51. #[ORM\JoinColumn(referencedColumnName: 'id', nullable: true, onDelete: 'SET NULL')]
  52. private ?File $image = null;
  53. #[ORM\Column(options: ['default' => true])]
  54. private bool $isPhysical = true;
  55. #[Pure] public function __construct()
  56. {
  57. $this->contactPoints = new ArrayCollection();
  58. $this->personAddressPostal = new ArrayCollection();
  59. }
  60. public function getId(): ?int
  61. {
  62. return $this->id;
  63. }
  64. public function getUsername(): ?string
  65. {
  66. return (string) $this->username;
  67. }
  68. public function getUserIdentifier(): ?string
  69. {
  70. return (string) $this->username;
  71. }
  72. public function setUsername(?string $username): self
  73. {
  74. $this->username = $username;
  75. return $this;
  76. }
  77. public function getRoles(): array
  78. {
  79. $roles = $this->roles;
  80. // guarantee every user at least has ROLE_USER
  81. $roles[] = 'ROLE_USER';
  82. return array_unique($roles);
  83. }
  84. public function setRoles(array $roles): self
  85. {
  86. $this->roles = $roles;
  87. return $this;
  88. }
  89. public function getPassword(): ?string
  90. {
  91. return (string) $this->password;
  92. }
  93. public function setPassword(?string $password): self
  94. {
  95. $this->password = $password;
  96. return $this;
  97. }
  98. public function getSalt(): ?string
  99. {
  100. return null;
  101. // not needed when using the "bcrypt" algorithm in security.yaml
  102. }
  103. public function eraseCredentials()
  104. {
  105. // If you store any temporary, sensitive data on the user, clear it here
  106. // $this->plainPassword = null;
  107. }
  108. public function getName(): ?string
  109. {
  110. return $this->name;
  111. }
  112. public function setName(?string $name): self
  113. {
  114. $this->name = $name;
  115. return $this;
  116. }
  117. public function getGivenName(): ?string
  118. {
  119. return $this->givenName;
  120. }
  121. public function setGivenName(?string $givenName): self
  122. {
  123. $this->givenName = $givenName;
  124. return $this;
  125. }
  126. public function setGender(?string $gender): self
  127. {
  128. $this->gender = $gender;
  129. return $this;
  130. }
  131. public function getGender(): ?string
  132. {
  133. return $this->gender;
  134. }
  135. /**
  136. * @return Collection|ContactPoint[]
  137. */
  138. public function getContactPoints(): Collection
  139. {
  140. return $this->contactPoints;
  141. }
  142. public function addContactPoint(ContactPoint $contactPoint): self
  143. {
  144. if (!$this->contactPoints->contains($contactPoint)) {
  145. $this->contactPoints[] = $contactPoint;
  146. $contactPoint->addPerson($this);
  147. }
  148. return $this;
  149. }
  150. public function removeContactPoint(ContactPoint $contactPoint): self
  151. {
  152. if ($this->contactPoints->removeElement($contactPoint)) {
  153. $contactPoint->removePerson($this);
  154. }
  155. return $this;
  156. }
  157. public function setImage(?File $image):self
  158. {
  159. $this->image = $image;
  160. return $this;
  161. }
  162. public function getImage(): ?File
  163. {
  164. return $this->image;
  165. }
  166. public function getPersonAddressPostal(): Collection
  167. {
  168. return $this->personAddressPostal;
  169. }
  170. public function addPersonAddressPostal(PersonAddressPostal $personAddressPostal): self
  171. {
  172. if (!$this->personAddressPostal->contains($personAddressPostal)) {
  173. $this->personAddressPostal[] = $personAddressPostal;
  174. $personAddressPostal->setPerson($this);
  175. }
  176. return $this;
  177. }
  178. public function removePersonAddressPostal(PersonAddressPostal $personAddressPostal): self
  179. {
  180. if ($this->personAddressPostal->removeElement($personAddressPostal)) {
  181. // set the owning side to null (unless already changed)
  182. if ($personAddressPostal->getPerson() === $this) {
  183. $personAddressPostal->setPerson(null);
  184. }
  185. }
  186. return $this;
  187. }
  188. public function getIsPhysical(): ?bool
  189. {
  190. return $this->isPhysical;
  191. }
  192. public function setAdminAccess(bool $isPhysical): self
  193. {
  194. $this->isPhysical = $isPhysical;
  195. return $this;
  196. }
  197. }