src/Entity/User.php line 21

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. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  10. use Symfony\Component\Validator\Constraints as Assert;
  11. /**
  12.  * @ORM\Entity(repositoryClass=UserRepository::class)
  13.  * @UniqueEntity(
  14.  *     fields={"email"},
  15.  *     message="Este correo ya está registrado."
  16.  * )
  17.  */
  18. class User implements UserInterfacePasswordAuthenticatedUserInterface
  19. {
  20.     public function __construct()
  21.     {
  22.         $this->roles = ["ROLE_CLIENT"];
  23.         $this->patients = new ArrayCollection();
  24.         $this->createdAt = new \DateTimeImmutable();
  25.         $this->useTemporaryEmail false;
  26.         $this->notifyClinica false;
  27.     }
  28.     /**
  29.      * @ORM\Id
  30.      * @ORM\GeneratedValue
  31.      * @ORM\Column(type="integer")
  32.      */
  33.     private $id;
  34.     /**
  35.      * @ORM\Column(type="string", length=180, unique=true)
  36.      * @Assert\NotBlank(message="El correo es obligatorio.")
  37.      * @Assert\Email(
  38.      *     message="Introduce un correo válido (ej: usuario@dominio.com).",
  39.      *     mode="strict"
  40.      * )
  41.      */
  42.     private $email;
  43.     /**
  44.      * @ORM\Column(type="json")
  45.      */
  46.     private $roles = [];
  47.     /**
  48.      * @ORM\Column(type="string")
  49.      */
  50.     private $password;
  51.     /**
  52.      * @Assert\NotBlank(message="La contraseña es obligatoria.", groups={"patients"})
  53.      * @Assert\Length(
  54.      *     min = 8,
  55.      *     minMessage = "La contraseña debe tener al menos {{ limit }} caracteres."
  56.      * )
  57.      * @Assert\Regex(pattern="/[A-Z]/", message="La contraseña debe contener al menos una letra mayúscula.")
  58.      * @Assert\Regex(pattern="/[a-z]/", message="La contraseña debe contener al menos una letra minúscula.")
  59.      * @Assert\Regex(pattern="/\d/", message="La contraseña debe contener al menos un número.")
  60.      * @Assert\Regex(pattern="/[\W_]/", message="La contraseña debe contener al menos un carácter especial.")
  61.      */
  62.     private $plainPassword;
  63.     /**
  64.      * @ORM\Column(type="string", length=255)
  65.      * @Assert\NotBlank(message="El nombre es obligatorio.", groups={"Default", "profile", "provider"})
  66.      * @Assert\Length(min=3, minMessage="El nombre debe tener al menos 3 caracteres.", groups={"Default", "profile", "provider"})
  67.      */
  68.     private $name;
  69.     /**
  70.      * @ORM\Column(name="lastname", type="string", length=255)
  71.      * @Assert\NotBlank(message="El apellido es obligatorio.", groups={"Default", "profile"})
  72.      * @Assert\Length(min=3, minMessage="El apellido debe tener al menos 3 caracteres.", groups={"Default", "profile"})
  73.      */
  74.     private $lastName;
  75.     /**
  76.      * @ORM\Column(type="string", length=20, nullable=true)
  77.      * @Assert\Length(min=8, minMessage="El teléfono debe tener al menos 8 dígitos.", groups={"Default", "profile", "provider"})
  78.      * @Assert\Regex(pattern="/^\+?[0-9]+$/", message="El teléfono solo puede contener números y el signo + al inicio.", groups={"Default", "profile", "provider"})
  79.      */
  80.     private $phone;
  81.     /**
  82.      * @ORM\Column(type="string", length=255, nullable=true)
  83.      */
  84.     private $address;
  85.     /**
  86.      * @ORM\Column(type="datetime_immutable")
  87.      */
  88.     private $createdAt;
  89.     /**
  90.      * @ORM\Column(type="date", nullable=true)
  91.      * @Assert\NotNull(
  92.      *     message="La fecha de nacimiento es obligatoria.",
  93.      *     groups={"Default", "profile"}
  94.      * )
  95.      * @Assert\LessThan(
  96.      *     "today",
  97.      *     message="La fecha de nacimiento debe ser anterior a hoy."
  98.      * )
  99.      */
  100.     private $birthDate;
  101.     /**
  102.      * Relación ManyToMany: un proveedor puede tener varios pacientes asignados.
  103.      * Esta relación es opcional y solo se usa para ROLE_PROVIDER.
  104.      *
  105.      * @ORM\ManyToMany(targetEntity="App\Entity\User")
  106.      * @ORM\JoinTable(
  107.      *     name="provider_patient",
  108.      *     joinColumns={@ORM\JoinColumn(name="provider_id", referencedColumnName="id")},
  109.      *     inverseJoinColumns={@ORM\JoinColumn(name="patient_id", referencedColumnName="id")}
  110.      * )
  111.      */
  112.     private $patients;
  113.     /**
  114.      * @ORM\Column(type="string", length=20, options={"default": "persona"})
  115.      */
  116.     private $entityType 'persona';
  117.     
  118.     /**
  119.      * @ORM\Column(type="string", length=100, nullable=true)
  120.      */
  121.     private $resetToken;
  122.     /**
  123.      * @ORM\Column(type="datetime_immutable", nullable=true)
  124.      */
  125.     private $tokenExpiresAt;
  126.     
  127.     /**
  128.      * @ORM\Column(type="boolean", options={"default": 0})
  129.     */
  130.     private $useTemporaryEmail false;
  131.     /**
  132.      * @ORM\Column(type="boolean", options={"default": 0})
  133.      */
  134.     private $notifyClinica false;
  135.     /**
  136.      * @ORM\Column(type="string", length=255, nullable=true)
  137.      */
  138.     private $clinicNotificationEmail;
  139.     public function getId(): ?int
  140.     {
  141.         return $this->id;
  142.     }
  143.     public function getEmail(): ?string
  144.     {
  145.         return $this->email;
  146.     }
  147.     public function setEmail(string $email): self
  148.     {
  149.         $this->email $email;
  150.         return $this;
  151.     }
  152.     public function getUserIdentifier(): string
  153.     {
  154.         return (string) $this->email;
  155.     }
  156.     /** @deprecated since Symfony 5.3 */
  157.     public function getUsername(): string
  158.     {
  159.         return (string) $this->email;
  160.     }
  161.     public function getRoles(): array
  162.     {
  163.         return $this->roles ?? [];
  164.     }
  165.     public function setRoles($roles): self
  166.     {
  167.         if (!is_array($roles)) {
  168.             $roles = [$roles];
  169.         }
  170.         $this->roles $roles;
  171.         return $this;
  172.     }
  173.     public function getPassword(): string
  174.     {
  175.         return $this->password;
  176.     }
  177.     public function setPassword(string $password): self
  178.     {
  179.         $this->password $password;
  180.         return $this;
  181.     }
  182.     public function getPlainPassword(): ?string
  183.     {
  184.         return $this->plainPassword;
  185.     }
  186.     public function setPlainPassword(?string $plainPassword): self
  187.     {
  188.         $this->plainPassword $plainPassword;
  189.         return $this;
  190.     }
  191.     public function getSalt(): ?string
  192.     {
  193.         return null;
  194.     }
  195.     public function eraseCredentials()
  196.     {
  197.         $this->plainPassword null;
  198.     }
  199.     public function getName(): ?string
  200.     {
  201.         return $this->name;
  202.     }
  203.     public function setName(string $name): self
  204.     {
  205.         $this->name $name;
  206.         return $this;
  207.     }
  208.     public function getLastName(): ?string
  209.     {
  210.         return $this->lastName;
  211.     }
  212.     public function setLastName(string $lastName): self
  213.     {
  214.         $this->lastName $lastName;
  215.         return $this;
  216.     }
  217.     public function getPhone(): ?string
  218.     {
  219.         return $this->phone;
  220.     }
  221.     public function setPhone(?string $phone): self
  222.     {
  223.         $this->phone $phone;
  224.         return $this;
  225.     }
  226.     public function getAddress(): ?string
  227.     {
  228.         return $this->address;
  229.     }
  230.     public function setAddress(?string $address): self
  231.     {
  232.         $this->address $address;
  233.         return $this;
  234.     }
  235.     /**
  236.      * @return Collection|User[]
  237.      */
  238.     public function getPatients(): Collection
  239.     {
  240.         return $this->patients;
  241.     }
  242.     public function addPatient(User $patient): self
  243.     {
  244.         if (!$this->patients->contains($patient)) {
  245.             $this->patients[] = $patient;
  246.         }
  247.         return $this;
  248.     }
  249.     public function removePatient(User $patient): self
  250.     {
  251.         $this->patients->removeElement($patient);
  252.         return $this;
  253.     }
  254.     public function getCreatedAt(): ?\DateTimeImmutable
  255.     {
  256.         return $this->createdAt;
  257.     }
  258.     public function setCreatedAt(\DateTimeImmutable $createdAt): self
  259.     {
  260.         $this->createdAt $createdAt;
  261.         return $this;
  262.     }
  263.     public function getEntityType(): string
  264.     {
  265.         return $this->entityType;
  266.     }
  267.     public function setEntityType(string $entityType): self
  268.     {
  269.         $this->entityType $entityType;
  270.         return $this;
  271.     }
  272.     
  273.     public function getResetToken(): ?string
  274.     {
  275.         return $this->resetToken;
  276.     }
  277.     public function setResetToken(?string $resetToken): self
  278.     {
  279.         $this->resetToken $resetToken;
  280.         return $this;
  281.     }
  282.     public function getTokenExpiresAt(): ?\DateTimeImmutable
  283.     {
  284.         return $this->tokenExpiresAt;
  285.     }
  286.     public function setTokenExpiresAt(?\DateTimeImmutable $tokenExpiresAt): self
  287.     {
  288.         $this->tokenExpiresAt $tokenExpiresAt;
  289.         return $this;
  290.     }
  291.     
  292.     public function getUseTemporaryEmail(): bool
  293.     {
  294.         return $this->useTemporaryEmail;
  295.     }
  296.     public function setUseTemporaryEmail(bool $useTemporaryEmail): self
  297.     {
  298.         $this->useTemporaryEmail $useTemporaryEmail;
  299.         return $this;
  300.     }
  301.     public function getNotifyClinica(): bool
  302.     {
  303.         return $this->notifyClinica;
  304.     }
  305.     public function setNotifyClinica(bool $notifyClinica): self
  306.     {
  307.         $this->notifyClinica $notifyClinica;
  308.         return $this;
  309.     }
  310.     public function getClinicNotificationEmail(): ?string
  311.     {
  312.         return $this->clinicNotificationEmail;
  313.     }
  314.     public function setClinicNotificationEmail(?string $clinicNotificationEmail): self
  315.     {
  316.         $this->clinicNotificationEmail $clinicNotificationEmail;
  317.         return $this;
  318.     }
  319.     
  320.     public function getBirthDate(): ?\DateTimeImmutable
  321.     {
  322.         if ($this->birthDate instanceof \DateTime) {
  323.         // Convertir mutable a immutable
  324.         $this->birthDate = \DateTimeImmutable::createFromMutable($this->birthDate);
  325.         }
  326.     
  327.         return $this->birthDate;
  328.     }
  329.     
  330.     public function setBirthDate(?\DateTimeImmutable $birthDate): self
  331.     {
  332.         $this->birthDate $birthDate;
  333.         return $this;
  334.     }
  335.     
  336.     public function getAge(): ?int
  337.     {
  338.         if (!$this->birthDate) {
  339.             return null;
  340.         }
  341.     
  342.         $today = new \DateTimeImmutable();
  343.         return $this->birthDate->diff($today)->y;
  344.     }
  345. }