src/Entity/User.php line 21

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use Assert\Email;
  4. use Assert\Length;
  5. use Assert\NotBlank;
  6. use Doctrine\DBAL\Types\Types;
  7. use Doctrine\ORM\Mapping as ORM;
  8. use App\Repository\UserRepository;
  9. use Doctrine\Common\Collections\Collection;
  10. use Doctrine\Common\Collections\ArrayCollection;
  11. use Symfony\Component\Serializer\Annotation\Groups;
  12. use Symfony\Component\Validator\Constraints as Assert;
  13. use Symfony\Component\Security\Core\User\UserInterface;
  14. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  15. #[ORM\Entity(repositoryClass: UserRepository::class)]
  16. class User implements UserInterface, PasswordAuthenticatedUserInterface
  17. {
  18.     #[Groups(['user:read'])]
  19.     #[ORM\Id]
  20.     #[ORM\GeneratedValue]
  21.     #[ORM\Column]
  22.     private ?int $id = null;
  23.     #[Groups(['user:read'])]
  24.     #[ORM\Column(length: 60, unique: true)]
  25.     #[NotBlank(message: "L'adresse e-mail est requise.")]
  26.     #[Email(message: "L'adresse e-mail n'est pas valide.")]
  27.     #[Length(max: 60, maxMessage: "L'adresse e-mail ne doit pas dépasser {{ limit }} caractères.")]
  28.     private ?string $email = null;
  29.     #[Groups(['user:read'])]
  30.     #[ORM\Column]
  31.     private array $roles = [];
  32.     /**
  33.      * @var string The hashed password
  34.      */
  35.     #[Groups(['user:read'])]
  36.     #[ORM\Column]
  37.     private ?string $password = null;
  38.     #[Groups(['user:read'])]
  39.     #[ORM\Column(length: 50)]
  40.     #[Assert\NotBlank(message: "Le nom complet est requis.")]
  41.     #[Assert\Length(max: 50, maxMessage: "Le nom complet ne doit pas dépasser {{ limit }} caractères.")]
  42.     private ?string $fullName = null;
  43.     #[Groups(['user:read'])]
  44.     #[ORM\OneToMany(mappedBy: 'user', targetEntity: Purchase::class, cascade: ["persist"])]
  45.     private Collection $purchases;
  46. //
  47.     #[Groups(['user:read'])]
  48.     #[ORM\Column(type: 'string', length: 100, nullable: true)]
  49.     private $resetToken;
  50.     #[Groups(['user:read'])]
  51.     #[ORM\Column(length: 255)]
  52.     #[Assert\NotBlank(message: "Le numéro de téléphone est requis.")]
  53.     #[Assert\Regex(
  54.         pattern: '/^\+?\d+$/',
  55.         message: 'Veuillez entrer un numéro de téléphone valide (chiffres et optionnellement le signe +).'
  56.     )]
  57.     #[Assert\Length(max: 20, maxMessage: "Le numéro de téléphone ne doit pas dépasser {{ limit }} caractères.")]
  58.     private ?string $phone = null;
  59.   
  60.     public function __construct()
  61.     {
  62.         $this->purchases = new ArrayCollection();
  63.     }
  64.     public function getId(): ?int
  65.     {
  66.         return $this->id;
  67.     }
  68.     public function getEmail(): ?string
  69.     {
  70.         return $this->email;
  71.     }
  72.     public function setEmail(?string $email): self
  73.     {
  74.         $this->email = $email;
  75.         return $this;
  76.     }
  77.     /**
  78.      * A visual identifier that represents this user.
  79.      *
  80.      * @see UserInterface
  81.      */
  82.     public function getUserIdentifier(): string
  83.     {
  84.         return (string) $this->email;
  85.     }
  86.     /**
  87.      * @deprecated since Symfony 5.3, use getUserIdentifier instead
  88.      */
  89.     public function getUsername(): string
  90.     {
  91.         return (string) $this->email;
  92.     }
  93.     /**
  94.      * @see UserInterface
  95.      */
  96.     public function getRoles(): array
  97.     {
  98.         $roles = $this->roles;
  99.         // guarantee every user at least has ROLE_USER
  100.         $roles[] = 'ROLE_USER';
  101.         return array_unique($roles);
  102.     }
  103.     public function setRoles(array $roles): self
  104.     {
  105.         $this->roles = $roles;
  106.         return $this;
  107.     }
  108.     /**
  109.      * @see PasswordAuthenticatedUserInterface
  110.      */
  111.     public function getPassword(): string
  112.     {
  113.         return $this->password;
  114.     }
  115.     public function setPassword(string $password): self
  116.     {
  117.         $this->password = $password;
  118.         return $this;
  119.     }
  120.     /**
  121.      * Returning a salt is only needed, if you are not using a modern
  122.      * hashing algorithm (e.g. bcrypt or sodium) in your security.yaml.
  123.      *
  124.      * @see UserInterface
  125.      */
  126.     public function getSalt(): ?string
  127.     {
  128.         return null;
  129.     }
  130.     /**
  131.      * @see UserInterface
  132.      */
  133.     public function eraseCredentials()
  134.     {
  135.         // If you store any temporary, sensitive data on the user, clear it here
  136.         // $this->plainPassword = null;
  137.     }
  138.     public function getFullName(): ?string
  139.     {
  140.         return $this->fullName;
  141.     }
  142.     public function setFullName(?string $fullName): self
  143.     {
  144.         $this->fullName = $fullName;
  145.         return $this;
  146.     }
  147.     /**
  148.      * @return Collection<int, Purchase>
  149.      */
  150.     public function getPurchases(): Collection
  151.     {
  152.         return $this->purchases;
  153.     }
  154.     public function addPurchase(Purchase $purchase): self
  155.     {
  156.         if (!$this->purchases->contains($purchase)) {
  157.             $this->purchases->add($purchase);
  158.             $purchase->setUser($this);
  159.         }
  160.         return $this;
  161.     }
  162.     public function removePurchase(Purchase $purchase): self
  163.     {
  164.         if ($this->purchases->removeElement($purchase)) {
  165.             // set the owning side to null (unless already changed)
  166.             if ($purchase->getUser() === $this) {
  167.                 $purchase->setUser(null);
  168.             }
  169.         }
  170.         return $this;
  171.     }
  172.     public function getResetToken(): ?string
  173.     {
  174.         return $this->resetToken;
  175.     }
  176.     public function setResetToken(?string $resetToken): self
  177.     {
  178.         $this->resetToken = $resetToken;
  179.         return $this;
  180.     }
  181.     public function getPhone(): ?string
  182.     {
  183.         return $this->phone;
  184.     }
  185.     public function setPhone(?string $phone): self
  186.     {
  187.         $this->phone = $phone;
  188.         return $this;
  189.     }
  190. }