src/Entity/User.php line 16

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\Id
  17. * @ORM\GeneratedValue
  18. * @ORM\Column(type="integer")
  19. */
  20. private $id;
  21. /**
  22. * @ORM\Column(type="string", length=180, unique=true)
  23. */
  24. private $email;
  25. /**
  26. * @ORM\Column(type="json")
  27. */
  28. private $roles = [];
  29. /**
  30. * @var string The hashed password
  31. * @ORM\Column(type="string")
  32. */
  33. private $password;
  34. /**
  35. * @ORM\Column(type="string", length=255)
  36. */
  37. private $firstname;
  38. /**
  39. * @ORM\Column(type="string", length=255)
  40. */
  41. private $lastname;
  42. /**
  43. * @ORM\OneToMany(targetEntity=Address::class, mappedBy="user")
  44. */
  45. private $addresses;
  46. /**
  47. * @ORM\OneToMany(targetEntity=Order::class, mappedBy="user")
  48. */
  49. private $orders;
  50. /**
  51. * @ORM\ManyToMany(targetEntity=Product::class)
  52. */
  53. private $wishlists;
  54. /**
  55. * @ORM\Column(type="string", length=255, nullable=true)
  56. */
  57. private $token;
  58. /**
  59. * @ORM\Column(type="datetime", nullable=true)
  60. */
  61. private $tokenExpireAt;
  62. /**
  63. * @ORM\Column(type="datetime", nullable=true)
  64. */
  65. private $lastLoginAt;
  66. public function __construct()
  67. {
  68. $this->addresses = new ArrayCollection();
  69. $this->orders = new ArrayCollection();
  70. $this->wishlists = new ArrayCollection();
  71. }
  72. public function __toString()
  73. {
  74. return $this->getFirstname().' '.$this->getLastname();
  75. }
  76. public function getId(): ?int
  77. {
  78. return $this->id;
  79. }
  80. public function getEmail(): ?string
  81. {
  82. return $this->email;
  83. }
  84. public function setEmail(string $email): self
  85. {
  86. $this->email = $email;
  87. return $this;
  88. }
  89. /**
  90. * A visual identifier that represents this user.
  91. *
  92. * @see UserInterface
  93. */
  94. public function getUserIdentifier(): string
  95. {
  96. return (string) $this->email;
  97. }
  98. /**
  99. * @deprecated since Symfony 5.3, use getUserIdentifier instead
  100. */
  101. public function getUsername(): string
  102. {
  103. return (string) $this->email;
  104. }
  105. /**
  106. * @see UserInterface
  107. */
  108. public function getRoles(): array
  109. {
  110. $roles = $this->roles;
  111. // guarantee every user at least has ROLE_USER
  112. $roles[] = 'ROLE_USER';
  113. return array_unique($roles);
  114. }
  115. public function setRoles(array $roles): self
  116. {
  117. $this->roles = $roles;
  118. return $this;
  119. }
  120. /**
  121. * @see PasswordAuthenticatedUserInterface
  122. */
  123. public function getPassword(): string
  124. {
  125. return $this->password;
  126. }
  127. public function setPassword(string $password): self
  128. {
  129. $this->password = $password;
  130. return $this;
  131. }
  132. /**
  133. * Returning a salt is only needed, if you are not using a modern
  134. * hashing algorithm (e.g. bcrypt or sodium) in your security.yaml.
  135. *
  136. * @see UserInterface
  137. */
  138. public function getSalt(): ?string
  139. {
  140. return null;
  141. }
  142. /**
  143. * @see UserInterface
  144. */
  145. public function eraseCredentials()
  146. {
  147. // If you store any temporary, sensitive data on the user, clear it here
  148. // $this->plainPassword = null;
  149. }
  150. public function getFirstname(): ?string
  151. {
  152. return $this->firstname;
  153. }
  154. public function setFirstname(string $firstname): self
  155. {
  156. $this->firstname = $firstname;
  157. return $this;
  158. }
  159. public function getLastname(): ?string
  160. {
  161. return $this->lastname;
  162. }
  163. public function setLastname(string $lastname): self
  164. {
  165. $this->lastname = $lastname;
  166. return $this;
  167. }
  168. /**
  169. * @return Collection<int, Address>
  170. */
  171. public function getAddresses(): Collection
  172. {
  173. return $this->addresses;
  174. }
  175. public function addAddress(Address $address): self
  176. {
  177. if (!$this->addresses->contains($address)) {
  178. $this->addresses[] = $address;
  179. $address->setUser($this);
  180. }
  181. return $this;
  182. }
  183. public function removeAddress(Address $address): self
  184. {
  185. if ($this->addresses->removeElement($address)) {
  186. // set the owning side to null (unless already changed)
  187. if ($address->getUser() === $this) {
  188. $address->setUser(null);
  189. }
  190. }
  191. return $this;
  192. }
  193. /**
  194. * @return Collection<int, Order>
  195. */
  196. public function getOrders(): Collection
  197. {
  198. return $this->orders;
  199. }
  200. public function addOrder(Order $order): self
  201. {
  202. if (!$this->orders->contains($order)) {
  203. $this->orders[] = $order;
  204. $order->setUser($this);
  205. }
  206. return $this;
  207. }
  208. public function removeOrder(Order $order): self
  209. {
  210. if ($this->orders->removeElement($order)) {
  211. // set the owning side to null (unless already changed)
  212. if ($order->getUser() === $this) {
  213. $order->setUser(null);
  214. }
  215. }
  216. return $this;
  217. }
  218. /**
  219. * @return Collection<int, Product>
  220. */
  221. public function getWishlists(): Collection
  222. {
  223. return $this->wishlists;
  224. }
  225. public function addWishlist(Product $wishlist): self
  226. {
  227. if (!$this->wishlists->contains($wishlist)) {
  228. $this->wishlists[] = $wishlist;
  229. }
  230. return $this;
  231. }
  232. public function removeWishlist(Product $wishlist): self
  233. {
  234. $this->wishlists->removeElement($wishlist);
  235. return $this;
  236. }
  237. public function getToken(): ?string
  238. {
  239. return $this->token;
  240. }
  241. public function setToken(?string $token): self
  242. {
  243. $this->token = $token;
  244. return $this;
  245. }
  246. public function getTokenExpireAt(): ?\DateTimeInterface
  247. {
  248. return $this->tokenExpireAt;
  249. }
  250. public function setTokenExpireAt(?\DateTimeInterface $tokenExpireAt): self
  251. {
  252. $this->tokenExpireAt = $tokenExpireAt;
  253. return $this;
  254. }
  255. public function getLastLoginAt(): ?\DateTimeInterface
  256. {
  257. return $this->lastLoginAt;
  258. }
  259. public function setLastLoginAt(?\DateTimeInterface $lastLoginAt): self
  260. {
  261. $this->lastLoginAt = $lastLoginAt;
  262. return $this;
  263. }
  264. }