src/Entity/User.php line 19

  1. <?php
  2. namespace App\Entity;
  3. use App\Entity\Trait\DateTrait;
  4. use App\Entity\Trait\IdTrait;
  5. use App\Repository\UserRepository;
  6. use Doctrine\Common\Collections\ArrayCollection;
  7. use Doctrine\Common\Collections\Collection;
  8. use Doctrine\ORM\Mapping as ORM;
  9. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  10. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  11. use Symfony\Component\Security\Core\User\UserInterface;
  12. #[ORM\Table(name'`user`')]
  13. #[ORM\Entity(repositoryClassUserRepository::class)]
  14. #[ORM\HasLifecycleCallbacks]
  15. #[UniqueEntity(fields: ['email'], message'Cette adresse email est déjà utilisée')]
  16. class User implements UserInterfacePasswordAuthenticatedUserInterface
  17. {
  18.     use IdTrait;
  19.     use DateTrait;
  20.     const ROLE_DEFAULT 'ROLE_USER';
  21.     #[ORM\Column(length180uniquetrue)]
  22.     private ?string $email null;
  23.     #[ORM\Column(length255nullabletrue)]
  24.     private ?string $firstName null;
  25.     #[ORM\Column(length255nullabletrue)]
  26.     private ?string $lastName null;
  27.     #[ORM\Column(length255nullabletrue)]
  28.     private ?string $phoneNumber null;
  29.     #[ORM\Column]
  30.     private array $roles = [];
  31.     /**
  32.      * @var string The hashed password
  33.      */
  34.     #[ORM\Column]
  35.     private ?string $password null;
  36.     #[ORM\Column(options: ['default' => false])]
  37.     private ?bool $isVerified null;
  38.     #[ORM\OneToMany(mappedBy'owner'targetEntityImage::class)]
  39.     private Collection $ownedImages;
  40.     public function __construct()
  41.     {
  42.         $this->isVerified false;
  43.         $this->ownedImages = new ArrayCollection();
  44.     }
  45.     /**
  46.      * A visual identifier that represents this user.
  47.      *
  48.      * @see UserInterface
  49.      */
  50.     public function getUserIdentifier(): string
  51.     {
  52.         return (string) $this->email;
  53.     }
  54.     public function getEmail(): ?string
  55.     {
  56.         return $this->email;
  57.     }
  58.     public function setEmail(?string $email): self
  59.     {
  60.         $this->email $email;
  61.         return $this;
  62.     }
  63.     public function getFirstName(): ?string
  64.     {
  65.         return $this->firstName;
  66.     }
  67.     public function setFirstName(?string $firstName): self
  68.     {
  69.         $this->firstName $firstName;
  70.         return $this;
  71.     }
  72.     public function getLastName(): ?string
  73.     {
  74.         return $this->lastName;
  75.     }
  76.     public function setLastName(?string $lastName): self
  77.     {
  78.         $this->lastName $lastName;
  79.         return $this;
  80.     }
  81.     public function getFullName(): ?string
  82.     {
  83.         $tab = [];
  84.         if ($this->lastName !== null) {
  85.             $tab[] = $this->firstName;
  86.         }
  87.         if ($this->lastName !== null) {
  88.             $tab[] = $this->lastName;
  89.         }
  90.         if (count($tab)) {
  91.             return implode(' '$tab);
  92.         }
  93.         return null;
  94.     }
  95.     public function getPhoneNumber(): ?string
  96.     {
  97.         return $this->phoneNumber;
  98.     }
  99.     public function setPhoneNumber(?string $phoneNumber): self
  100.     {
  101.         $this->phoneNumber $phoneNumber;
  102.         return $this;
  103.     }
  104.     /**
  105.      * @see UserInterface
  106.      */
  107.     public function getRoles(): array
  108.     {
  109.         $roles $this->roles;
  110.         // guarantee every user at least has ROLE_USER
  111.         $roles[] = self::ROLE_DEFAULT;
  112.         return array_unique($roles);
  113.     }
  114.     public function setRoles(array $roles): self
  115.     {
  116.         $this->roles $roles;
  117.         return $this;
  118.     }
  119.     public function addRole(string $role): self
  120.     {
  121.         $this->roles[] = $role;
  122.         $this->roles array_unique($this->roles);
  123.         return $this;
  124.     }
  125.     /**
  126.      * @see PasswordAuthenticatedUserInterface
  127.      */
  128.     public function getPassword(): string
  129.     {
  130.         return $this->password;
  131.     }
  132.     public function setPassword(?string $password): self
  133.     {
  134.         $this->password $password;
  135.         return $this;
  136.     }
  137.     /**
  138.      * @see UserInterface
  139.      */
  140.     public function eraseCredentials()
  141.     {
  142.         // If you store any temporary, sensitive data on the user, clear it here
  143.         // $this->plainPassword = null;
  144.     }
  145.     public function isVerified(): ?bool
  146.     {
  147.         return $this->isVerified;
  148.     }
  149.     public function setIsVerified(?bool $isVerified): self
  150.     {
  151.         $this->isVerified $isVerified;
  152.         return $this;
  153.     }
  154.     /**
  155.      * @return Collection<int, Image>
  156.      */
  157.     public function getOwnedImages(): Collection
  158.     {
  159.         return $this->ownedImages;
  160.     }
  161.     public function addOwnedImage(Image $ownedImage): self
  162.     {
  163.         if (!$this->ownedImages->contains($ownedImage)) {
  164.             $this->ownedImages->add($ownedImage);
  165.             $ownedImage->setOwner($this);
  166.         }
  167.         return $this;
  168.     }
  169.     public function removeOwnedImage(Image $ownedImage): self
  170.     {
  171.         if ($this->ownedImages->removeElement($ownedImage)) {
  172.             // set the owning side to null (unless already changed)
  173.             if ($ownedImage->getOwner() === $this) {
  174.                 $ownedImage->setOwner(null);
  175.             }
  176.         }
  177.         return $this;
  178.     }
  179. }