<?php declare(strict_types=1);
namespace App\Entity\Embed;
use ApiPlatform\Core\Annotation\ApiProperty;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\{PropertyAccess\PropertyAccess, Serializer\Annotation\Groups, Validator\Constraints as Assert};
#[ORM\Embeddable]
class Person
{
public function __construct(
string $firstName = null,
string $lastName = null,
string $surname = null,
string $gender = null,
\DateTime $birthDate = null,
string $jobTitle = null,
string $city = null
) {
$this->firstName = $firstName;
$this->lastName = $lastName;
$this->surname = $surname;
$this->gender = $this->convertGender($gender);
$this->birthDate = $birthDate;
$this->jobTitle = $jobTitle;
$this->city = $city;
}
private function convertGender(?string $gender): string
{
if ($gender === null) {
return 'Male';
}
$gender = \ucfirst(\strtolower($gender));
if (!\in_array($gender, ['Male', 'Female'])) {
$gender = 'Male';
}
return $gender;
}
#[Assert\NotBlank(groups: ['newRecord'])]
#[Assert\Length(min: 2, max: 255)]
#[Groups(['User:read', 'User:write', 'Manager:read', 'Developer:read', 'TimeEntry:read', 'Project:read', 'Profile:read', 'Team:read', 'TeamMember:read'])]
#[ORM\Column(nullable: true)]
private ?string $firstName;
#[Assert\NotBlank(groups: ['newRecord'])]
#[Assert\Length(min: 2, max: 255)]
#[Groups(['User:read', 'User:write', 'Manager:read', 'Developer:read', 'TimeEntry:read', 'Project:read', 'Profile:read', 'Team:read', 'TeamMember:read'])]
#[ORM\Column(nullable: true)]
private ?string $lastName;
#[Groups(['User:read', 'User:write', 'Manager:read', 'Developer:read', 'TimeEntry:read', 'Project:read', 'Profile:read'])]
#[Assert\Length(max: 255)]
#[ORM\Column(nullable: true)]
private ?string $surname;
#[Assert\Choice(choices: ['Male', 'Feemale'])]
#[ApiProperty(
attributes: ['openapi_context' => ['type' => 'string', 'enum' => ['Male', 'Feemale'], 'example' => 'Feemale']]
)]
#[Groups(['User:read', 'User:write', 'Manager:read', 'Developer:read', 'TimeEntry:read', 'Project:read', 'Profile:read'])]
#[ORM\Column(nullable: true, options: ['default' => 'Male'])]
private ?string $gender;
#[Groups(['User:read', 'User:write', 'Manager:read', 'Developer:read', 'TimeEntry:read', 'Project:read', 'Profile:read'])]
#[ORM\Column(type: 'date', nullable: true)]
private ?\DateTime $birthDate;
#[Groups(['User:read', 'User:write', 'Manager:read', 'Developer:read', 'TimeEntry:read', 'Project:read', 'Profile:read'])]
#[ORM\Column(nullable: true)]
private ?string $jobTitle = null;
#[Groups(['User:read', 'User:write', 'Manager:read', 'Developer:read', 'TimeEntry:read', 'Project:read', 'Profile:read'])]
#[ORM\Column(nullable: true)]
private ?string $city = null;
public function getFirstName(): ?string
{
return $this->firstName;
}
public function setFirstName(?string $firstName): self
{
$this->firstName = $firstName;
return $this;
}
public function getLastName(): ?string
{
return $this->lastName;
}
public function setLastName(?string $lastName): self
{
$this->lastName = $lastName;
return $this;
}
public function getSurname(): ?string
{
return $this->surname;
}
public function setSurname(?string $surname): self
{
$this->surname = $surname;
return $this;
}
public function getGender(): ?string
{
return $this->gender;
}
public function setGender(string $gender): self
{
$this->gender = $this->convertGender($gender);
return $this;
}
public function getBirthDate(): ?\DateTime
{
return $this->birthDate;
}
public function setBirthDate(?\DateTime $birthDate): self
{
$this->birthDate = $birthDate;
return $this;
}
public function getJobTitle(): ?string
{
return $this->jobTitle;
}
public function setJobTitle(string $jobTitle): self
{
$this->jobTitle = $jobTitle;
return $this;
}
public function getCity(): ?string
{
return $this->city;
}
public function setCity(string $city): self
{
$this->city = $city;
return $this;
}
public function updateData(Person $person): self
{
$propertyAccessor = PropertyAccess::createPropertyAccessor();
foreach (\get_object_vars($this) as $propertyName => $propertyValue) {
if (($newValue = $propertyAccessor->getValue($person, $propertyName)) !== null) {
$propertyAccessor->setValue($this, $propertyName, $newValue);
}
}
return $this;
}
public function getFullName(): string
{
return \implode(' ', \array_filter([$this->lastName, $this->firstName, $this->surname]));
}
}