src/Entity/Direccion.php line 28

  1. <?php
  2. namespace App\Entity;
  3. use ApiPlatform\Metadata\ApiResource;
  4. use App\Repository\DireccionRepository;
  5. use Doctrine\DBAL\Types\Types;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use ApiPlatform\Metadata\Get;
  8. use ApiPlatform\Metadata\Post;
  9. use ApiPlatform\Metadata\Put;
  10. use ApiPlatform\Metadata\Delete;
  11. use ApiPlatform\Metadata\Patch;
  12. use ApiPlatform\Metadata\GetCollection;
  13. use ApiPlatform\Metadata\ApiFilter;
  14. #https://api-platform.com/docs/core/serialization/#plain-identifiers
  15. // use ApiPlatform\Doctrine\Orm\Filter\DateFilter;
  16. use ApiPlatform\Doctrine\Orm\Filter\SearchFilter;
  17. use ApiPlatform\Doctrine\Orm\Filter\BooleanFilter;
  18. use ApiPlatform\Doctrine\Orm\Filter\NumericFilter;
  19. use Symfony\Component\Validator\Constraints as Assert;
  20. use App\Validator\Constraints as CustomAssert;
  21. use Symfony\Component\Serializer\Annotation\Groups;
  22. use Symfony\Component\Serializer\Annotation\SerializedName;
  23. #[ORM\Entity(repositoryClassDireccionRepository::class)]
  24. #[ApiResource(
  25.     order: ['id' => 'DESC'],
  26.     description'Direcciones de Herdasa',
  27.     shortName'Direcciones',
  28.     //Fichero para ver posibles opciones vendor/api-platform/core/src/Metadata/ApiResource.php
  29.     operations:[
  30.         new Get(security'is_granted("' ApiToken::SCOPE_USER_READ '")'),
  31.         new GetCollection(security'is_granted("' ApiToken::SCOPE_USER_READ '")'),
  32.         new Post(security'is_granted("' ApiToken::SCOPE_USER_WRITE_DIST '")'),
  33.         new Put(security'is_granted("' ApiToken::SCOPE_USER_WRITE_DIST '")'),
  34.         new Delete(security'is_granted("' ApiToken::SCOPE_USER_WRITE_DIST '")'),
  35.         new Patch(security'is_granted("' ApiToken::SCOPE_USER_WRITE_DIST '")'),
  36.     ],
  37.     // normalizationContext: ['groups' => ['direccion:read']],
  38.     // denormalizationContext: ['groups' => ['direccion:write']],
  39. )]
  40. #[ApiFilter(SearchFilter::class, properties: ['id' => 'exact''usuario' => 'exact'])]
  41. #[ApiFilter(BooleanFilter::class, properties: ['primario'])]
  42. #[ApiFilter(NumericFilter::class, properties: ['tipo'])]
  43. // #[Get(normalizationContext: ['groups' => ['direccion:read', 'direccion:write']])]
  44. class Direccion
  45. {
  46.     #[ORM\Id]
  47.     #[ORM\GeneratedValue]
  48.     #[ORM\Column]
  49.     #[Groups(['direccion:read''direccion:write'])]
  50.     private ?int $id null;
  51.     #[ORM\Column(length100)]
  52.     #[Assert\NotBlank(message"El nombre es requerido.")]
  53.     #[Assert\Length(
  54.         max100,
  55.         maxMessage"El nombre no puede tener más de {{ limit }} caracteres."
  56.     )]
  57.     #[Groups(['direccion:read''direccion:write'])]
  58.     private ?string $nombre null;
  59.     #[ORM\Column(length150)]
  60.     #[Assert\NotBlank(message"Los apellidos son requeridos.")]
  61.     #[Assert\Length(
  62.         max150,
  63.         maxMessage"Los apellidos no pueden tener más de {{ limit }} caracteres."
  64.     )]
  65.     #[Groups(['direccion:read''direccion:write'])]
  66.     private ?string $apellidos null;
  67.     #[ORM\Column(length255)]
  68.     #[Assert\Length(
  69.         max255,
  70.         maxMessage"La calle_1 no puede tener más de {{ limit }} caracteres."
  71.     )]
  72.     #[Assert\NotBlank(message"La primera línea de la calle es requerida.")]
  73.     #[Groups(['direccion:read''direccion:write'])]
  74.     #[SerializedName('calle1')]
  75.     private ?string $calle_1 null;
  76.     #[ORM\Column(length255nullabletrue)]
  77.     #[Assert\Length(
  78.         max255,
  79.         maxMessage"La calle_2 no puede tener más de {{ limit }} caracteres."
  80.     )]
  81.     #[Groups(['direccion:read''direccion:write'])]
  82.     #[SerializedName('calle2')]
  83.     private ?string $calle_2 null;
  84.     #[ORM\Column(length80)]
  85.     #[Assert\Length(
  86.         max80,
  87.         maxMessage"El código postal no puede tener más de {{ limit }} caracteres."
  88.     )]
  89.     #[Assert\NotBlank(message"La ciudad es requerida.")]
  90.     #[Groups(['direccion:read''direccion:write'])]
  91.     private ?string $ciudad null;
  92.     #[ORM\Column(length80)]
  93.     #[Assert\Length(
  94.         max80,
  95.         maxMessage"La provincia no puede tener más de {{ limit }} caracteres."
  96.     )]
  97.     #[Assert\NotBlank(message"La provincia es requerida.")]
  98.     #[Groups(['direccion:read''direccion:write'])]
  99.     private ?string $provincia null;
  100.     #[ORM\Column(length5)]
  101.     #[Assert\Length(
  102.         min5,
  103.         max5,
  104.         exactMessage"El Código postal debe tener exactamente {{ limit }} caracteres."
  105.     )]
  106.     #[Assert\Regex(
  107.         pattern"/^[0-9]*$/",
  108.         message"El código postal solo puede contener números."
  109.     )]
  110.     #[Assert\NotBlank(message"El código postal es requerido.")]
  111.     #[Groups(['direccion:read''direccion:write'])]
  112.     #[SerializedName('codigoPostal')]
  113.     private ?string $codigo_postal null;
  114.     #[ORM\Column(length9)]
  115.     #[Assert\Length(
  116.         min9,
  117.         max9,
  118.         exactMessage"El teléfono debe tener exactamente {{ limit }} caracteres."
  119.     )]
  120.     #[Assert\NotBlank(message"El teléfono es requerido.")]
  121.     #[Assert\Regex(
  122.         pattern"/^[0-9]*$/",
  123.         message"El teléfono solo puede contener números."
  124.     )]
  125.     #[Groups(['direccion:read''direccion:write'])]
  126.     private ?string $telefono null;
  127.     #[ORM\Column(length100nullabletrue)]
  128.     #[Assert\Length(
  129.         max100,
  130.         maxMessage"El email no puede tener más de {{ limit }} caracteres."
  131.     )]
  132.     #[Assert\NotBlank(message"El email es requerido.")]
  133.     #[Assert\Email(
  134.         message"El email '{{ value }}' no es un email válido."
  135.     )]
  136.     #[Groups(['direccion:read''direccion:write'])]
  137.     private ?string $email null;
  138.     #[ORM\Column(length9)]
  139.     #[Assert\Length(
  140.         min9,
  141.         max9,
  142.         exactMessage"El dni/cif debe tener exactamente {{ limit }} caracteres."
  143.     )]
  144.     #[Assert\NotBlank(message"El dni/cif es requerido.")]
  145.     #[CustomAssert\DniCif]
  146.     #[Groups(['direccion:read''direccion:write'])]
  147.     #[SerializedName('dniCif')]
  148.     private ?string $dni_cif null;
  149.     #[ORM\Column(typeTypes::SMALLINT)]
  150.     #[Groups(['direccion:read''direccion:write'])]
  151.     private ?int $tipo null;
  152.     #[ORM\ManyToOne(inversedBy'direcciones')]
  153.     #[ORM\JoinColumn(nullablefalse)]
  154.     #[Groups(['direccion:read''direccion:write'])]
  155.     private ?User $usuario null;
  156.     #[ORM\Column]
  157.     #[Groups(['direccion:read''direccion:write'])]
  158.     private ?bool $primario null;
  159.     #[ORM\Column(length255)]
  160.     #[ORM\JoinColumn(nullablefalse)]
  161.     #[Groups(['direccion:read''direccion:write'])]
  162.     private ?string $alias null;
  163.     public function __toString(){
  164.         return $this->id;
  165.     }
  166.     public function getId(): ?int
  167.     {
  168.         return $this->id;
  169.     }
  170.     public function getNombre(): ?string
  171.     {
  172.         return $this->nombre;
  173.     }
  174.     public function setNombre(string $nombre): static
  175.     {
  176.         $this->nombre $nombre;
  177.         return $this;
  178.     }
  179.     public function getApellidos(): ?string
  180.     {
  181.         return $this->apellidos;
  182.     }
  183.     public function setApellidos(string $apellidos): static
  184.     {
  185.         $this->apellidos $apellidos;
  186.         return $this;
  187.     }
  188.     public function getCalle1(): ?string
  189.     {
  190.         return $this->calle_1;
  191.     }
  192.     public function setCalle1(string $calle_1): static
  193.     {
  194.         $this->calle_1 $calle_1;
  195.         return $this;
  196.     }
  197.     public function getCalle2(): ?string
  198.     {
  199.         return $this->calle_2;
  200.     }
  201.     public function setCalle2(?string $calle_2): static
  202.     {
  203.         $this->calle_2 $calle_2;
  204.         return $this;
  205.     }
  206.     public function getCiudad(): ?string
  207.     {
  208.         return $this->ciudad;
  209.     }
  210.     public function setCiudad(string $ciudad): static
  211.     {
  212.         $this->ciudad $ciudad;
  213.         return $this;
  214.     }
  215.     public function getProvincia(): ?string
  216.     {
  217.         return $this->provincia;
  218.     }
  219.     public function setProvincia(string $provincia): static
  220.     {
  221.         $this->provincia $provincia;
  222.         return $this;
  223.     }
  224.     public function getCodigoPostal(): ?string
  225.     {
  226.         return $this->codigo_postal;
  227.     }
  228.     public function setCodigoPostal(string $codigo_postal): static
  229.     {
  230.         $this->codigo_postal $codigo_postal;
  231.         return $this;
  232.     }
  233.     public function getTelefono(): ?string
  234.     {
  235.         return $this->telefono;
  236.     }
  237.     public function setTelefono(string $telefono): static
  238.     {
  239.         $this->telefono $telefono;
  240.         return $this;
  241.     }
  242.     public function getEmail(): ?string
  243.     {
  244.         return $this->email;
  245.     }
  246.     public function setEmail(?string $email): static
  247.     {
  248.         $this->email $email;
  249.         return $this;
  250.     }
  251.     public function getDniCif(): ?string
  252.     {
  253.         return $this->dni_cif;
  254.     }
  255.     public function setDniCif(string $dni_cif): static
  256.     {
  257.         $this->dni_cif $dni_cif;
  258.         return $this;
  259.     }
  260.     public function getTipo(): ?int
  261.     {
  262.         return $this->tipo;
  263.     }
  264.     public function setTipo(int $tipo): static
  265.     {
  266.         $this->tipo $tipo;
  267.         return $this;
  268.     }
  269.     public function getUsuario(): ?User
  270.     {
  271.         return $this->usuario;
  272.     }
  273.     public function setUsuario(?User $usuario): static
  274.     {
  275.         $this->usuario $usuario;
  276.         return $this;
  277.     }
  278.     public function isPrimario(): ?bool
  279.     {
  280.         return $this->primario;
  281.     }
  282.     public function setPrimario(bool $primario): static
  283.     {
  284.         $this->primario $primario;
  285.         return $this;
  286.     }
  287.     public function getAlias(): ?string
  288.     {
  289.         return $this->alias;
  290.     }
  291.     public function setAlias(string $alias): static
  292.     {
  293.         $this->alias $alias;
  294.         return $this;
  295.     }
  296. }