src/Entity/Embed/Person.php line 10

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace App\Entity\Embed;
  3. use ApiPlatform\Core\Annotation\ApiProperty;
  4. use Doctrine\ORM\Mapping as ORM;
  5. use Symfony\Component\{PropertyAccess\PropertyAccessSerializer\Annotation\GroupsValidator\Constraints as Assert};
  6. #[ORM\Embeddable]
  7. class Person
  8. {
  9.     public function __construct(
  10.         string $firstName null,
  11.         string $lastName null,
  12.         string $surname null,
  13.         string $gender null,
  14.         \DateTime $birthDate null,
  15.         string $jobTitle null,
  16.         string $city null
  17.     ) {
  18.         $this->firstName $firstName;
  19.         $this->lastName $lastName;
  20.         $this->surname $surname;
  21.         $this->gender $this->convertGender($gender);
  22.         $this->birthDate $birthDate;
  23.         $this->jobTitle $jobTitle;
  24.         $this->city $city;
  25.     }
  26.     private function convertGender(?string $gender): string
  27.     {
  28.         if ($gender === null) {
  29.             return 'Male';
  30.         }
  31.         $gender \ucfirst(\strtolower($gender));
  32.         if (!\in_array($gender, ['Male''Female'])) {
  33.             $gender 'Male';
  34.         }
  35.         return $gender;
  36.     }
  37.     #[Assert\NotBlank(groups: ['newRecord'])]
  38.     #[Assert\Length(min2max255)]
  39.     #[Groups(['User:read''User:write''Manager:read''Developer:read''TimeEntry:read''Project:read''Profile:read''Team:read''TeamMember:read'])]
  40.     #[ORM\Column(nullabletrue)]
  41.     private ?string $firstName;
  42.     #[Assert\NotBlank(groups: ['newRecord'])]
  43.     #[Assert\Length(min2max255)]
  44.     #[Groups(['User:read''User:write''Manager:read''Developer:read''TimeEntry:read''Project:read''Profile:read''Team:read''TeamMember:read'])]
  45.     #[ORM\Column(nullabletrue)]
  46.     private ?string $lastName;
  47.     #[Groups(['User:read''User:write''Manager:read''Developer:read''TimeEntry:read''Project:read''Profile:read'])]
  48.     #[Assert\Length(max255)]
  49.     #[ORM\Column(nullabletrue)]
  50.     private ?string $surname;
  51.     #[Assert\Choice(choices: ['Male''Feemale'])]
  52.     #[ApiProperty(
  53.         attributes: ['openapi_context' => ['type' => 'string''enum' => ['Male''Feemale'], 'example' => 'Feemale']]
  54.     )]
  55.     #[Groups(['User:read''User:write''Manager:read''Developer:read''TimeEntry:read''Project:read''Profile:read'])]
  56.     #[ORM\Column(nullabletrueoptions: ['default' => 'Male'])]
  57.     private ?string $gender;
  58.     #[Groups(['User:read''User:write''Manager:read''Developer:read''TimeEntry:read''Project:read''Profile:read'])]
  59.     #[ORM\Column(type'date'nullabletrue)]
  60.     private ?\DateTime $birthDate;
  61.     #[Groups(['User:read''User:write''Manager:read''Developer:read''TimeEntry:read''Project:read''Profile:read'])]
  62.     #[ORM\Column(nullabletrue)]
  63.     private ?string $jobTitle null;
  64.     #[Groups(['User:read''User:write''Manager:read''Developer:read''TimeEntry:read''Project:read''Profile:read'])]
  65.     #[ORM\Column(nullabletrue)]
  66.     private ?string $city null;
  67.     public function getFirstName(): ?string
  68.     {
  69.         return $this->firstName;
  70.     }
  71.     public function setFirstName(?string $firstName): self
  72.     {
  73.         $this->firstName $firstName;
  74.         return $this;
  75.     }
  76.     public function getLastName(): ?string
  77.     {
  78.         return $this->lastName;
  79.     }
  80.     public function setLastName(?string $lastName): self
  81.     {
  82.         $this->lastName $lastName;
  83.         return $this;
  84.     }
  85.     public function getSurname(): ?string
  86.     {
  87.         return $this->surname;
  88.     }
  89.     public function setSurname(?string $surname): self
  90.     {
  91.         $this->surname $surname;
  92.         return $this;
  93.     }
  94.     public function getGender(): ?string
  95.     {
  96.         return $this->gender;
  97.     }
  98.     public function setGender(string $gender): self
  99.     {
  100.         $this->gender $this->convertGender($gender);
  101.         return $this;
  102.     }
  103.     public function getBirthDate(): ?\DateTime
  104.     {
  105.         return $this->birthDate;
  106.     }
  107.     public function setBirthDate(?\DateTime $birthDate): self
  108.     {
  109.         $this->birthDate $birthDate;
  110.         return $this;
  111.     }
  112.     public function getJobTitle(): ?string
  113.     {
  114.         return $this->jobTitle;
  115.     }
  116.     public function setJobTitle(string $jobTitle): self
  117.     {
  118.         $this->jobTitle $jobTitle;
  119.         return $this;
  120.     }
  121.     public function getCity(): ?string
  122.     {
  123.         return $this->city;
  124.     }
  125.     public function setCity(string $city): self
  126.     {
  127.         $this->city $city;
  128.         return $this;
  129.     }
  130.     public function updateData(Person $person): self
  131.     {
  132.         $propertyAccessor PropertyAccess::createPropertyAccessor();
  133.         foreach (\get_object_vars($this) as $propertyName => $propertyValue) {
  134.             if (($newValue $propertyAccessor->getValue($person$propertyName)) !== null) {
  135.                 $propertyAccessor->setValue($this$propertyName$newValue);
  136.             }
  137.         }
  138.         return $this;
  139.     }
  140.     public function getFullName(): string
  141.     {
  142.         return \implode(' '\array_filter([$this->lastName$this->firstName$this->surname]));
  143.     }
  144. }