src/Entity/User.php line 17

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\UserRepository;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  8. use Symfony\Component\Security\Core\User\UserInterface;
  9. /**
  10. * @ORM\Entity(repositoryClass=UserRepository::class)
  11. * @ORM\Table(name="`user`")
  12. */
  13. class User implements UserInterface, PasswordAuthenticatedUserInterface
  14. {
  15. /**
  16. * @ORM\OneToMany(targetEntity=Wishlist::class, mappedBy="user")
  17. */
  18. private $wishlists;
  19. /**
  20. * @ORM\Id
  21. * @ORM\GeneratedValue
  22. * @ORM\Column(type="integer")
  23. */
  24. private $id;
  25. /**
  26. * @ORM\Column(type="string", length=180, unique=true)
  27. */
  28. private $email;
  29. /**
  30. * @ORM\Column(type="json")
  31. */
  32. private $roles = [];
  33. /**
  34. * @var string The hashed password
  35. * @ORM\Column(type="string")
  36. */
  37. private $password;
  38. public function getFullName(): string{
  39. return $this->getFirstname().' '.$this->getLastname();
  40. }
  41. /**
  42. * @ORM\Column(type="string", length=255)
  43. */
  44. private $firstname;
  45. /**
  46. * @ORM\Column(type="string", length=255)
  47. */
  48. private $lastname;
  49. /**
  50. * @ORM\OneToMany(targetEntity=Address::class, mappedBy="user")
  51. */
  52. private $addresses;
  53. /**
  54. * @ORM\OneToMany(targetEntity=Order::class, mappedBy="user")
  55. */
  56. private $orders;
  57. public function __construct()
  58. {
  59. $this->addresses = new ArrayCollection();
  60. $this->orders = new ArrayCollection();
  61. $this->wishlists = new ArrayCollection();
  62. }
  63. public function getId(): ?int
  64. {
  65. return $this->id;
  66. }
  67. public function getEmail(): ?string
  68. {
  69. return $this->email;
  70. }
  71. public function setEmail(string $email): self
  72. {
  73. $this->email = $email;
  74. return $this;
  75. }
  76. /**
  77. * A visual identifier that represents this user.
  78. *
  79. * @see UserInterface
  80. */
  81. public function getUserIdentifier(): string
  82. {
  83. return (string) $this->email;
  84. }
  85. /**
  86. * @deprecated since Symfony 5.3, use getUserIdentifier instead
  87. */
  88. public function getUsername(): string
  89. {
  90. return (string) $this->email;
  91. }
  92. /**
  93. * @see UserInterface
  94. */
  95. public function getRoles(): array
  96. {
  97. $roles = $this->roles;
  98. // guarantee every user at least has ROLE_USER
  99. $roles[] = 'ROLE_USER';
  100. return array_unique($roles);
  101. }
  102. public function setRoles(array $roles): self
  103. {
  104. $this->roles = $roles;
  105. return $this;
  106. }
  107. /**
  108. * @see PasswordAuthenticatedUserInterface
  109. */
  110. public function getPassword(): string
  111. {
  112. return $this->password;
  113. }
  114. public function setPassword(string $password): self
  115. {
  116. $this->password = $password;
  117. return $this;
  118. }
  119. /**
  120. * Returning a salt is only needed, if you are not using a modern
  121. * hashing algorithm (e.g. bcrypt or sodium) in your security.yaml.
  122. *
  123. * @see UserInterface
  124. */
  125. public function getSalt(): ?string
  126. {
  127. return null;
  128. }
  129. /**
  130. * @see UserInterface
  131. */
  132. public function eraseCredentials()
  133. {
  134. // If you store any temporary, sensitive data on the user, clear it here
  135. // $this->plainPassword = null;
  136. }
  137. public function getFirstname(): ?string
  138. {
  139. return $this->firstname;
  140. }
  141. public function setFirstname(string $firstname): self
  142. {
  143. $this->firstname = $firstname;
  144. return $this;
  145. }
  146. public function getLastname(): ?string
  147. {
  148. return $this->lastname;
  149. }
  150. public function setLastname(string $lastname): self
  151. {
  152. $this->lastname = $lastname;
  153. return $this;
  154. }
  155. /**
  156. * @return Collection<int, Address>
  157. */
  158. public function getAddresses(): Collection
  159. {
  160. return $this->addresses;
  161. }
  162. public function addAddress(Address $address): self
  163. {
  164. if (!$this->addresses->contains($address)) {
  165. $this->addresses[] = $address;
  166. $address->setUser($this);
  167. }
  168. return $this;
  169. }
  170. public function removeAddress(Address $address): self
  171. {
  172. if ($this->addresses->removeElement($address)) {
  173. // set the owning side to null (unless already changed)
  174. if ($address->getUser() === $this) {
  175. $address->setUser(null);
  176. }
  177. }
  178. return $this;
  179. }
  180. /**
  181. * @return Collection<int, Order>
  182. */
  183. public function getOrders(): Collection
  184. {
  185. return $this->orders;
  186. }
  187. public function addOrder(Order $order): self
  188. {
  189. if (!$this->orders->contains($order)) {
  190. $this->orders[] = $order;
  191. $order->setUser($this);
  192. }
  193. return $this;
  194. }
  195. public function removeOrder(Order $order): self
  196. {
  197. if ($this->orders->removeElement($order)) {
  198. // set the owning side to null (unless already changed)
  199. if ($order->getUser() === $this) {
  200. $order->setUser(null);
  201. }
  202. }
  203. return $this;
  204. }
  205. }