src/Entity/Address.php line 23

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use Doctrine\ORM\Mapping as ORM;
  4. use JsonSerializable;
  5. use Symfony\Component\Validator\Constraints as Assert;
  6. /**
  7.  * @ORM\Table( options={"comment":"Obsahuje dorucovani zpetne i fakturacni adresy"},
  8.  * indexes={
  9.  * @ORM\Index(name="address_name", columns={"name"}),
  10.  * @ORM\Index(name="address_address1", columns={"address1"}),
  11.  * @ORM\Index(name="address_address2", columns={"address2"}),
  12.  * @ORM\Index(name="address_zip_code", columns={"zip_code"}),
  13.  * @ORM\Index(name="address_phone", columns={"phone"}),
  14.  * @ORM\Index(name="address_email", columns={"email"})
  15.  * })
  16.  * @ORM\Entity()
  17.  * @ORM\InheritanceType("JOINED")
  18.  * @ORM\DiscriminatorColumn(name="discriminator", type="string")
  19.  */
  20. class Address implements JsonSerializable
  21. {
  22.     public const DEFAULT_COUNTRY_ID 1;
  23.     public const DEFAULT_COUNTRY_ISO 'de';
  24.     /**
  25.      * @ORM\Id()
  26.      * @ORM\GeneratedValue(strategy="IDENTITY")
  27.      * @ORM\Column( type="integer")
  28.      */
  29.     private $id;
  30.     /**
  31.      * @Assert\NotBlank(
  32.      * message="validators.Address.country.notBlank"
  33.      * )
  34.      * @ORM\ManyToOne(targetEntity="App\Entity\Country")
  35.      * @ORM\JoinColumn( nullable=false)
  36.      */
  37.     private $country;
  38.     /**
  39.      * @Assert\NotBlank(
  40.      * message="validators.Address.name.notBlank"
  41.      * )
  42.      * @Assert\Length(
  43.      *      min = 2,
  44.      *      max = 50,
  45.      *      minMessage = "validators.Address.name.minMessage",
  46.      *      maxMessage = "validators.Address.name.maxMessage",
  47.      * )
  48.      * @ORM\Column(type="string", length=255, options={"comment":"Jmeno zakaznika"})
  49.      */
  50.     private $name;
  51.     /**
  52.      * @Assert\NotBlank(
  53.      * message="validators.Address.address1.notBlank"
  54.      * )
  55.      * @Assert\Length(
  56.      *      min = 2,
  57.      *      max = 50,
  58.      *      minMessage = "validators.Address.address1.minMessage",
  59.      *      maxMessage = "validators.Address.address1.maxMessage",
  60.      * )
  61.      * @ORM\Column(type="string", length=255, options={"comment":"Hlavni radek adresy (ulice a cislo)"})
  62.      */
  63.     private $address1;
  64.     /**
  65.      * @Assert\Length(
  66.      *      min = 2,
  67.      *      max = 50,
  68.      *      minMessage = "validators.Address.address1.minMessage",
  69.      *      maxMessage = "validators.Address.address1.maxMessage",
  70.      * )
  71.      * @ORM\Column(type="string", length=255, nullable=true, options={"comment":"Dodatecny radek adresy (firma, byt)"})
  72.      */
  73.     private $address2;
  74.     /**
  75.      * @Assert\NotBlank(
  76.      * message="validators.Address.zipCode.notBlank"
  77.      * )
  78.      * @Assert\Length(
  79.      *      min = 4,
  80.      *      max = 6,
  81.      *      minMessage = "validators.Address.zipCode.minMessage",
  82.      *      maxMessage = "validators.Address.zipCode.maxMessage",
  83.      * )
  84.      * @Assert\Regex(
  85.      *  pattern="/^[a-zA-Z\s\d\-,]+$/",
  86.      *  message="validators.Address.zipcode.regex"
  87.      * )
  88.      * @ORM\Column(type="string", length=10, options={"comment":"Postovni smerovaci cislo nebo ekvivalent"})
  89.      */
  90.     private $zipCode;
  91.     /**
  92.      * @Assert\NotBlank(
  93.      *    message="validators.Address.phone.notBlank"
  94.      * )
  95.      * @Assert\Regex(
  96.      *    pattern="/^\+?[\d\s\.\-\*\#\(\)\\\/]{6,20}$/",
  97.      *    message="validators.Address.phone.regex"
  98.      * )
  99.      * @ORM\Column(type="string", length=18, options={"comment":"Telefonni cislo koncoveho zakaznika"})
  100.      */
  101.     private $phone;
  102.     /**
  103.      * @Assert\Email(
  104.      *    message="validators.Address.email.email"
  105.      * )
  106.       /**
  107.      * @ORM\Column(type="string", length=255, nullable=true, options={"comment":"emailova adresa"})
  108.      */
  109.     private $email;
  110.     /**
  111.      * @ORM\Column(type="string", length=255, nullable=true, options={"comment":"Pojmenovani pro ulozene adresy"})
  112.      */
  113.     private $alias;
  114.     /**
  115.      * @Assert\NotBlank(
  116.      * message="validators.Address.city.notBlank"
  117.      * )
  118.      * @Assert\Length(
  119.      *      min = 2,
  120.      *      max = 50,
  121.      *      minMessage = "validators.Address.city.minMessage",
  122.      *      maxMessage = "validators.Address.city.maxMessage",
  123.      * )
  124.      * @ORM\Column(type="string", length=64, options={"comment":"Mesto"})
  125.      */
  126.     private $city;
  127.     /**
  128.      * @Assert\Length(
  129.      *      min = 2,
  130.      *      max = 63,
  131.      *      minMessage = "validators.Address.district.minMessage",
  132.      *      maxMessage = "validators.Address.district.maxMessage",
  133.      * )
  134.      * @ORM\Column(type="string", length=63, nullable=true, options={"comment":"Městská čtvrť"})
  135.      */
  136.     private $district;
  137.     public function getId(): ?int
  138.     {
  139.         return $this->id;
  140.     }
  141.     public function getName(): ?string
  142.     {
  143.         return $this->name;
  144.     }
  145.     public function setName($name): self
  146.     {
  147.         $this->name trim($name);
  148.         return $this;
  149.     }
  150.     public function getAddress1(): ?string
  151.     {
  152.         return $this->address1;
  153.     }
  154.     public function setAddress1(string $address1): self
  155.     {
  156.         $this->address1 trim($address1);
  157.         return $this;
  158.     }
  159.     public function getAddress2(): ?string
  160.     {
  161.         return $this->address2;
  162.     }
  163.     public function setAddress2($address2): self
  164.     {
  165.         $this->address2 $address2;
  166.         return $this;
  167.     }
  168.     public function getZipCode(): ?string
  169.     {
  170.         return $this->zipCode;
  171.     }
  172.     public function setZipCode($zipCode): self
  173.     {
  174.         $this->zipCode mb_ereg_replace('\s+'''$zipCode);
  175.         return $this;
  176.     }
  177.     public function getPhone(): ?string
  178.     {
  179.         return $this->phone;
  180.     }
  181.     public function setPhone($phone): self
  182.     {
  183.         $this->phone mb_ereg_replace('\s+'''$phone);
  184.         return $this;
  185.     }
  186.     public function getEmail(): ?string
  187.     {
  188.         return $this->email;
  189.     }
  190.     public function setEmail($email): self
  191.     {
  192.         $this->email trim($email);
  193.         return $this;
  194.     }
  195.     public function getAlias(): ?string
  196.     {
  197.         return $this->alias;
  198.     }
  199.     public function setAlias($alias): self
  200.     {
  201.         $this->alias $alias;
  202.         return $this;
  203.     }
  204.     public function getCity(): ?string
  205.     {
  206.         return $this->city;
  207.     }
  208.     public function setCity($city): self
  209.     {
  210.         $this->city trim($city);
  211.         return $this;
  212.     }
  213.     public function getDistrict(): ?string
  214.     {
  215.         return $this->district;
  216.     }
  217.     public function setDistrict($district): self
  218.     {
  219.         $this->district $district;
  220.         return $this;
  221.     }
  222.     public function getCountry(): ?Country
  223.     {
  224.         return $this->country;
  225.     }
  226.     public function setCountry($country): self
  227.     {
  228.         $this->country $country;
  229.         return $this;
  230.     }
  231.     /**
  232.      * pomocna funkce k vytvoreni adresy
  233.      * @param  string  $name
  234.      * @param  string  $address1
  235.      * @param  string|null  $address2
  236.      * @param  string  $city
  237.      * @param  string  $zipCode
  238.      * @param  Country|int  $country
  239.      * @param  string  $phone
  240.      * @param  string|null  $email
  241.      *
  242.      * @return self
  243.      */
  244.     public static function createNewAddress(
  245.         string $name,
  246.         string $address1,
  247.         string $city,
  248.         string $zipCode,
  249.         Country|int $country,
  250.         string $phone,
  251.         string|null $address2 null,
  252.         string|null $email null,
  253.     ): self {
  254.         $address =  new self();
  255.         $address->setName($name);
  256.         $address->setAddress1($address1);
  257.         !$address2 ?: $address->setAddress2($address2);
  258.         $address->setCity($city);
  259.         $address->setZipCode($zipCode);
  260.         $address->setCountry($country);
  261.         $address->setPhone($phone);
  262.         !$email ?: $address->setEmail($email);
  263.         return  $address;
  264.     }
  265.     public function jsonSerialize(): mixed
  266.     {
  267.         return [
  268.             'name' => $this->name,
  269.             'address1' => $this->address1,
  270.             'address2' => $this->address2,
  271.             'city' => $this->city,
  272.             'district' => $this->district,
  273.             'zipCode' => $this->zipCode,
  274.             'country' => $this->country->getIso3(),
  275.         ];
  276.     }
  277.     /**
  278.      * Pokusi se extrahovat jmeno ulice z adresy
  279.      * Pozor ma to jista omezeni viz AddressTest::getTestDataAddress1
  280.      * @return string
  281.      */
  282.     public function getStreet(): string
  283.     {
  284.         return $this->divideStreetAndHouseNumber($this->getAddress1())[0];
  285.     }
  286.     /**
  287.      * zkopirovano z CME
  288.      * vrací pole o třech prvcích [0] => ulice, [1] => číslo popisne, [2] => číslo orientacni
  289.      * matchuje podle predpokladu:
  290.      *   - ulice neni povinna (male vsi bez ulic)
  291.      *   - ulice muze obsahovat cislice (17. listopadu)
  292.      *   - pokud ulice obsahuje carku nebo lomitko, text pred nimi se ignoruje (prum. areal Kyblov, Zelena 11 )
  293.      *   - pokud je carka nebo strednik mezi ulici a cislem, ignoruje se
  294.      *   - pokud je za ulici jen jedno cislo, je to cislo orientacni (posta si to umi prehodit)
  295.      *   - za orientacnim cislem muze nasledovat jedno pismeno (vchod)
  296.      *   - dalsi text za cislem domu se ignoruje (obvykle patro, cislo bytu etc)
  297.      *   - okolostojici mezery se zahazuji
  298.      *   - kdyz se nezadari, vrati se cely vstup jako ulice
  299.      *
  300.      * @param null|string $bothData
  301.      * @param bool $splitHouseNumberData pokud je true, vrací pole o dvou prvcích [0] => ulice, [1] => číslo
  302.      * @return array<string>
  303.      */
  304.     public function divideStreetAndHouseNumber(?string $bothDatabool $splitHouseNumberData true): array
  305.     {
  306.         if (!$bothData) {
  307.             $bothData '';
  308.         }
  309.         // nejdriv zkusime samotna cisla
  310.         if (preg_match('~^\s*((?P<cp>[0-9]+)\s*/\s*)?(?P<co>[0-9]+[a-zA-Z]?)\s*$~'$bothData$matches)) {
  311.             $return = array(''$matches['cp'], $matches['co']);
  312.         } elseif (preg_match('~\s*(?P<ulice>[^/,]+)\s*[,;]?\s+((?P<cp>[0-9]+)\s*/\s*)?(?P<co>[0-9]+[a-zA-Z]?)~'$bothData$matches)) {
  313.             $return = array(trim($matches['ulice']), $matches['cp'], $matches['co']);
  314.         } else {
  315.             $return = array($bothData'''');
  316.         }
  317.         if (!$splitHouseNumberData) {
  318.             if ($return[1] && $return[2]) {
  319.                 $houseNumber $return[1] . "/" $return[2];
  320.             } elseif ($return[1] || $return[2]) {
  321.                 //jedno z polí je prázdné
  322.                 $houseNumber $return[1] . $return[2];
  323.             } else {
  324.                 $houseNumber '';
  325.             }
  326.             return array($return[0], $houseNumber);
  327.         }
  328.         return $return;
  329.     }
  330. }