src/Entity/Person.php line 15

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Validator\Constraints\MinAge;
  4. use Doctrine\ORM\Mapping as ORM;
  5. use JMS\Serializer\Annotation as Serializer;
  6. use libphonenumber\PhoneNumber;
  7. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  8. use Symfony\Component\Security\Core\User\UserInterface;
  9. use Symfony\Component\Validator\Constraints as Assert;
  10. /**
  11.  * @UniqueEntity("emailAddress")
  12.  */
  13. class Person implements \Serializable\JsonSerializable
  14. {
  15.     /**
  16.      * @ORM\Id()
  17.      * @ORM\GeneratedValue(strategy="AUTO")
  18.      * @ORM\Column(type="integer")
  19.      */
  20.     protected $id;
  21.     
  22.     /**
  23.      * @Assert\NotBlank()
  24.      * @Assert\Choice(
  25.      *     choices = {"m", "f", "o"},
  26.      *     message = "Bitte angeben"
  27.      * )
  28.      * @ORM\Column(type="string")
  29.      */
  30.     protected $salutation;
  31.     
  32.     /**
  33.      * @ORM\Column(type="string", nullable=true)
  34.      */
  35.     protected $title;
  36.     
  37.     /**
  38.      * @Assert\NotBlank()
  39.      * @ORM\Column(type="string")
  40.      */
  41.     protected $firstName;
  42.     
  43.     /**
  44.      * @Assert\NotBlank()
  45.      * @ORM\Column(type="string")
  46.      */
  47.     protected $lastName;
  48.     
  49.     /**
  50.      * @ORM\Column(type="string", nullable=true)
  51.      */
  52.     protected $companyName;
  53.     
  54.     /**
  55.      * @Assert\NotBlank()
  56.      * @ORM\Column(type="string")
  57.      */
  58.     protected $addressStreetNo;
  59.     
  60.     /**
  61.      * @ORM\Column(type="string", nullable=true)
  62.      */
  63.     protected $addressStreetAdd;
  64.     
  65.     /**
  66.      * @Assert\NotBlank()
  67.      * @ORM\Column(type="string")
  68.      */
  69.     protected $addressPostalCode;
  70.     
  71.     /**
  72.      * @Assert\NotBlank()
  73.      * @ORM\Column(type="string")
  74.      */
  75.     protected $addressCity;
  76.     
  77.     /**
  78.      * @Assert\NotBlank()
  79.      * @Assert\Country()
  80.      * @ORM\Column(type="string")
  81.      */
  82.     protected $addressCountry;
  83.     
  84.     /**
  85.      * @Assert\NotBlank()
  86.      * @Assert\Email()
  87.      * @ORM\Column(type="string", unique=true, nullable=true)
  88.      */
  89.     protected $emailAddress;
  90.     
  91.     /**
  92.      * @Assert\NotBlank()
  93.      * @Serializer\Type("libphonenumber\PhoneNumber")
  94.      * @ORM\Column(type="phone_number", nullable=true)
  95.      */
  96.     protected $phoneNumber;
  97.     /**
  98.      * @Assert\NotBlank()
  99.      * @MinAge(age=18)
  100.      * @ORM\Column(type="date")
  101.      */
  102.     protected $dateOfBirth;
  103.     /**
  104.      * @ORM\Column(type="string", options={"default": "de"})
  105.      */
  106.     protected $locale;
  107.     
  108.     public function __construct$blank false )
  109.     {
  110.         if ($blank) {
  111.             $this->setSalutation('m');
  112.             $this->setFirstName('');
  113.             $this->setLastName('');
  114.             $this->setAddressStreetNo('');
  115.             $this->setAddressPostalCode('');
  116.             $this->setAddressCity('');
  117.             $this->setAddressCountry('');
  118.             $this->setDateOfBirth(new \DateTime());
  119.             $this->setEmailAddress('');
  120.             $this->setPhoneNumber('');
  121.             $this->setLocale('de');
  122.         }
  123.     }
  124.     /**
  125.      * @param mixed $id
  126.      */
  127.     public function setId($id)
  128.     {
  129.         $this->id $id;
  130.     }
  131.     /**
  132.      * @return mixed
  133.      */
  134.     public function getId()
  135.     {
  136.         return $this->id;
  137.     }
  138.     /**
  139.      * @return string
  140.      */
  141.     public function getSalutation()
  142.     {
  143.         return $this->salutation;
  144.     }
  145.     /**
  146.      * @param string $m
  147.      * @param string $f
  148.      * @param string $default
  149.      * @return string
  150.      */
  151.     public function getCustomSalutation($m 'm'$f 'f'$default 'm')
  152.     {
  153.         switch ($this->salutation) {
  154.             case 'm':
  155.                 return $m;
  156.             case 'f':
  157.                 return $f;
  158.             default:
  159.                 return $default;
  160.         }
  161.     }
  162.     
  163.     /**
  164.      * @param string $salutation
  165.      */
  166.     public function setSalutation($salutation)
  167.     {
  168.         $this->salutation $salutation;
  169.     }
  170.     
  171.     /**
  172.      * @return string
  173.      */
  174.     public function getTitle()
  175.     {
  176.         return $this->title;
  177.     }
  178.     
  179.     /**
  180.      * @param string $title
  181.      */
  182.     public function setTitle($title)
  183.     {
  184.         $this->title $title;
  185.     }
  186.     
  187.     /**
  188.      * @return string
  189.      */
  190.     public function getFirstName()
  191.     {
  192.         return $this->firstName;
  193.     }
  194.     
  195.     /**
  196.      * @param string $firstName
  197.      */
  198.     public function setFirstName($firstName)
  199.     {
  200.         $this->firstName $firstName;
  201.     }
  202.     
  203.     /**
  204.      * @return string
  205.      */
  206.     public function getLastName()
  207.     {
  208.         return $this->lastName;
  209.     }
  210.     
  211.     /**
  212.      * @param mixed $lastName
  213.      */
  214.     public function setLastName($lastName)
  215.     {
  216.         $this->lastName $lastName;
  217.     }
  218.     
  219.     /**
  220.      * @return mixed
  221.      */
  222.     public function getAddressStreetNo()
  223.     {
  224.         return $this->addressStreetNo;
  225.     }
  226.     
  227.     /**
  228.      * @param mixed $addressStreetNo
  229.      */
  230.     public function setAddressStreetNo($addressStreetNo)
  231.     {
  232.         $this->addressStreetNo $addressStreetNo;
  233.     }
  234.     
  235.     /**
  236.      * @return string
  237.      */
  238.     public function getAddressStreetAdd()
  239.     {
  240.         return $this->addressStreetAdd;
  241.     }
  242.     
  243.     /**
  244.      * @param string $addressStreetAdd
  245.      */
  246.     public function setAddressStreetAdd($addressStreetAdd)
  247.     {
  248.         $this->addressStreetAdd $addressStreetAdd;
  249.     }
  250.     
  251.     /**
  252.      * @return mixed
  253.      */
  254.     public function getAddressPostalCode()
  255.     {
  256.         return $this->addressPostalCode;
  257.     }
  258.     
  259.     /**
  260.      * @param mixed $addressPostalCode
  261.      */
  262.     public function setAddressPostalCode($addressPostalCode)
  263.     {
  264.         $this->addressPostalCode $addressPostalCode;
  265.     }
  266.     
  267.     /**
  268.      * @return string
  269.      */
  270.     public function getAddressCity()
  271.     {
  272.         return $this->addressCity;
  273.     }
  274.     
  275.     /**
  276.      * @param string $addressCity
  277.      */
  278.     public function setAddressCity($addressCity)
  279.     {
  280.         $this->addressCity $addressCity;
  281.     }
  282.     
  283.     /**
  284.      * @return string
  285.      */
  286.     public function getAddressCountry()
  287.     {
  288.         return $this->addressCountry;
  289.     }
  290.     
  291.     /**
  292.      * @param string $addressCountry
  293.      */
  294.     public function setAddressCountry($addressCountry)
  295.     {
  296.         $this->addressCountry $addressCountry;
  297.     }
  298.     /**
  299.      * @param bool $internal    get the internally used address
  300.      * @return string
  301.      */
  302.     public function getEmailAddress($internal false)
  303.     {
  304.         if (!$internal) {
  305.             // remove middle part of 'renting' email
  306.             $email explode('@'$this->emailAddress);
  307.             if (count($email) == 3) {
  308.                 unset($email[1]);
  309.                 return implode('@'$email);
  310.             }
  311.         }
  312.         return $this->emailAddress;
  313.     }
  314.     
  315.     /**
  316.      * @param string $emailAddress
  317.      */
  318.     public function setEmailAddress($emailAddress)
  319.     {
  320.         $this->emailAddress $emailAddress;
  321.     }
  322.     
  323.     /**
  324.      * @return PhoneNumber
  325.      */
  326.     public function getPhoneNumber()
  327.     {
  328.         return $this->phoneNumber;
  329.     }
  330.     
  331.     /**
  332.      * @param string $phoneNumber
  333.      */
  334.     public function setPhoneNumber($phoneNumber)
  335.     {
  336.         $this->phoneNumber $phoneNumber;
  337.     }
  338.     
  339.     /**
  340.      * @return string
  341.      */
  342.     public function getCompanyName()
  343.     {
  344.         return $this->companyName;
  345.     }
  346.     
  347.     /**
  348.      * @param string $companyName
  349.      */
  350.     public function setCompanyName($companyName)
  351.     {
  352.         $this->companyName $companyName;
  353.     }
  354.     
  355.     /**
  356.      * @return mixed
  357.      */
  358.     public function getDateOfBirth()
  359.     {
  360.         return $this->dateOfBirth;
  361.     }
  362.     
  363.     /**
  364.      * @param mixed $dateOfBirth
  365.      */
  366.     public function setDateOfBirth($dateOfBirth)
  367.     {
  368.         $this->dateOfBirth $dateOfBirth;
  369.     }
  370.     /**
  371.      * @return string
  372.      */
  373.     public function getLocale()
  374.     {
  375.         return $this->locale;
  376.     }
  377.     /**
  378.      * @param string $locale
  379.      */
  380.     public function setLocale($locale)
  381.     {
  382.         $this->locale $locale;
  383.     }
  384.     /**
  385.      * @return string
  386.      */
  387.     public function __toString()
  388.     {
  389.         return $this->getFullName();
  390.     }
  391.     
  392.     public function serialize()
  393.     {
  394.         return serialize(
  395.             [
  396.                 $this->id,
  397.                 $this->emailAddress,
  398.                 $this->firstName,
  399.                 $this->lastName,
  400.             ]
  401.         );
  402.     }
  403.     
  404.     public function getFullName()
  405.     {
  406.         return $this->getFirstName().' '.$this->getLastName();
  407.     }
  408.     
  409.     public function unserialize($serialized)
  410.     {
  411.         [
  412.             $this->id,
  413.             $this->emailAddress,
  414.             $this->firstName,
  415.             $this->lastName
  416.         ] = unserialize($serialized);
  417.     }
  418.     
  419.     public function isUserdataComplete()
  420.     {
  421.         if (empty($this->salutation)) return false;
  422.         if (empty($this->firstName)) return false;
  423.         if (empty($this->lastName)) return false;
  424.         if (empty($this->addressStreetNo)) return false;
  425.         if (empty($this->addressPostalCode)) return false;
  426.         if (empty($this->addressCity)) return false;
  427.         if (empty($this->addressCountry)) return false;
  428.         if (empty($this->emailAddress)) return false;
  429.         if (empty($this->phoneNumber)) return false;
  430.         if (empty($this->dateOfBirth)) return false;
  431.         
  432.         return true;
  433.     }
  434.     /**
  435.      * @return mixed
  436.      */
  437.     public function jsonSerialize()
  438.     {
  439.         // TODO: Implement jsonSerialize() method.
  440.         return [
  441.             'id' => $this->id,
  442.             'firstName' => $this->firstName,
  443.             'lastname' => $this->lastName,
  444.         ];
  445.     }
  446. }