<?php
namespace App\Entity;
use App\Validator\Constraints\MinAge;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as Serializer;
use libphonenumber\PhoneNumber;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @UniqueEntity("emailAddress")
*/
class Person implements \Serializable, \JsonSerializable
{
/**
* @ORM\Id()
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer")
*/
protected $id;
/**
* @Assert\NotBlank()
* @Assert\Choice(
* choices = {"m", "f", "o"},
* message = "Bitte angeben"
* )
* @ORM\Column(type="string")
*/
protected $salutation;
/**
* @ORM\Column(type="string", nullable=true)
*/
protected $title;
/**
* @Assert\NotBlank()
* @ORM\Column(type="string")
*/
protected $firstName;
/**
* @Assert\NotBlank()
* @ORM\Column(type="string")
*/
protected $lastName;
/**
* @ORM\Column(type="string", nullable=true)
*/
protected $companyName;
/**
* @Assert\NotBlank()
* @ORM\Column(type="string")
*/
protected $addressStreetNo;
/**
* @ORM\Column(type="string", nullable=true)
*/
protected $addressStreetAdd;
/**
* @Assert\NotBlank()
* @ORM\Column(type="string")
*/
protected $addressPostalCode;
/**
* @Assert\NotBlank()
* @ORM\Column(type="string")
*/
protected $addressCity;
/**
* @Assert\NotBlank()
* @Assert\Country()
* @ORM\Column(type="string")
*/
protected $addressCountry;
/**
* @Assert\NotBlank()
* @Assert\Email()
* @ORM\Column(type="string", unique=true, nullable=true)
*/
protected $emailAddress;
/**
* @Assert\NotBlank()
* @Serializer\Type("libphonenumber\PhoneNumber")
* @ORM\Column(type="phone_number", nullable=true)
*/
protected $phoneNumber;
/**
* @Assert\NotBlank()
* @MinAge(age=18)
* @ORM\Column(type="date")
*/
protected $dateOfBirth;
/**
* @ORM\Column(type="string", options={"default": "de"})
*/
protected $locale;
public function __construct( $blank = false )
{
if ($blank) {
$this->setSalutation('m');
$this->setFirstName('');
$this->setLastName('');
$this->setAddressStreetNo('');
$this->setAddressPostalCode('');
$this->setAddressCity('');
$this->setAddressCountry('');
$this->setDateOfBirth(new \DateTime());
$this->setEmailAddress('');
$this->setPhoneNumber('');
$this->setLocale('de');
}
}
/**
* @param mixed $id
*/
public function setId($id)
{
$this->id = $id;
}
/**
* @return mixed
*/
public function getId()
{
return $this->id;
}
/**
* @return string
*/
public function getSalutation()
{
return $this->salutation;
}
/**
* @param string $m
* @param string $f
* @param string $default
* @return string
*/
public function getCustomSalutation($m = 'm', $f = 'f', $default = 'm')
{
switch ($this->salutation) {
case 'm':
return $m;
case 'f':
return $f;
default:
return $default;
}
}
/**
* @param string $salutation
*/
public function setSalutation($salutation)
{
$this->salutation = $salutation;
}
/**
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* @param string $title
*/
public function setTitle($title)
{
$this->title = $title;
}
/**
* @return string
*/
public function getFirstName()
{
return $this->firstName;
}
/**
* @param string $firstName
*/
public function setFirstName($firstName)
{
$this->firstName = $firstName;
}
/**
* @return string
*/
public function getLastName()
{
return $this->lastName;
}
/**
* @param mixed $lastName
*/
public function setLastName($lastName)
{
$this->lastName = $lastName;
}
/**
* @return mixed
*/
public function getAddressStreetNo()
{
return $this->addressStreetNo;
}
/**
* @param mixed $addressStreetNo
*/
public function setAddressStreetNo($addressStreetNo)
{
$this->addressStreetNo = $addressStreetNo;
}
/**
* @return string
*/
public function getAddressStreetAdd()
{
return $this->addressStreetAdd;
}
/**
* @param string $addressStreetAdd
*/
public function setAddressStreetAdd($addressStreetAdd)
{
$this->addressStreetAdd = $addressStreetAdd;
}
/**
* @return mixed
*/
public function getAddressPostalCode()
{
return $this->addressPostalCode;
}
/**
* @param mixed $addressPostalCode
*/
public function setAddressPostalCode($addressPostalCode)
{
$this->addressPostalCode = $addressPostalCode;
}
/**
* @return string
*/
public function getAddressCity()
{
return $this->addressCity;
}
/**
* @param string $addressCity
*/
public function setAddressCity($addressCity)
{
$this->addressCity = $addressCity;
}
/**
* @return string
*/
public function getAddressCountry()
{
return $this->addressCountry;
}
/**
* @param string $addressCountry
*/
public function setAddressCountry($addressCountry)
{
$this->addressCountry = $addressCountry;
}
/**
* @param bool $internal get the internally used address
* @return string
*/
public function getEmailAddress($internal = false)
{
if (!$internal) {
// remove middle part of 'renting' email
$email = explode('@', $this->emailAddress);
if (count($email) == 3) {
unset($email[1]);
return implode('@', $email);
}
}
return $this->emailAddress;
}
/**
* @param string $emailAddress
*/
public function setEmailAddress($emailAddress)
{
$this->emailAddress = $emailAddress;
}
/**
* @return PhoneNumber
*/
public function getPhoneNumber()
{
return $this->phoneNumber;
}
/**
* @param string $phoneNumber
*/
public function setPhoneNumber($phoneNumber)
{
$this->phoneNumber = $phoneNumber;
}
/**
* @return string
*/
public function getCompanyName()
{
return $this->companyName;
}
/**
* @param string $companyName
*/
public function setCompanyName($companyName)
{
$this->companyName = $companyName;
}
/**
* @return mixed
*/
public function getDateOfBirth()
{
return $this->dateOfBirth;
}
/**
* @param mixed $dateOfBirth
*/
public function setDateOfBirth($dateOfBirth)
{
$this->dateOfBirth = $dateOfBirth;
}
/**
* @return string
*/
public function getLocale()
{
return $this->locale;
}
/**
* @param string $locale
*/
public function setLocale($locale)
{
$this->locale = $locale;
}
/**
* @return string
*/
public function __toString()
{
return $this->getFullName();
}
public function serialize()
{
return serialize(
[
$this->id,
$this->emailAddress,
$this->firstName,
$this->lastName,
]
);
}
public function getFullName()
{
return $this->getFirstName().' '.$this->getLastName();
}
public function unserialize($serialized)
{
[
$this->id,
$this->emailAddress,
$this->firstName,
$this->lastName
] = unserialize($serialized);
}
public function isUserdataComplete()
{
if (empty($this->salutation)) return false;
if (empty($this->firstName)) return false;
if (empty($this->lastName)) return false;
if (empty($this->addressStreetNo)) return false;
if (empty($this->addressPostalCode)) return false;
if (empty($this->addressCity)) return false;
if (empty($this->addressCountry)) return false;
if (empty($this->emailAddress)) return false;
if (empty($this->phoneNumber)) return false;
if (empty($this->dateOfBirth)) return false;
return true;
}
/**
* @return mixed
*/
public function jsonSerialize()
{
// TODO: Implement jsonSerialize() method.
return [
'id' => $this->id,
'firstName' => $this->firstName,
'lastname' => $this->lastName,
];
}
}