50 lines
1.7 KiB
PHP
50 lines
1.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Service\Contracts;
|
|
|
|
use App\Entity\Employee;
|
|
use App\Enum\ContractNature;
|
|
use DateTimeImmutable;
|
|
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
|
|
|
|
final class EmployeeContractChangeRequestFactory
|
|
{
|
|
public function fromEmployee(Employee $employee): EmployeeContractChangeRequest
|
|
{
|
|
return new EmployeeContractChangeRequest(
|
|
contractNature: $this->resolveContractNature($employee->getContractNature()),
|
|
contractStartDate: $this->parseOptionalYmd($employee->getContractStartDate(), 'contractStartDate'),
|
|
contractEndDate: $this->parseOptionalYmd($employee->getContractEndDate(), 'contractEndDate'),
|
|
contractPaidLeaveSettled: $employee->getContractPaidLeaveSettled(),
|
|
contractComment: $employee->getContractComment(),
|
|
);
|
|
}
|
|
|
|
private function resolveContractNature(?string $raw): ?ContractNature
|
|
{
|
|
if (null === $raw || '' === trim($raw)) {
|
|
return null;
|
|
}
|
|
|
|
return ContractNature::tryFrom(trim($raw))
|
|
?? throw new UnprocessableEntityHttpException('contractNature must be one of CDI, CDD, INTERIM.');
|
|
}
|
|
|
|
private function parseOptionalYmd(?string $raw, string $field): ?DateTimeImmutable
|
|
{
|
|
if (null === $raw || '' === trim($raw)) {
|
|
return null;
|
|
}
|
|
|
|
$value = trim($raw);
|
|
$date = DateTimeImmutable::createFromFormat('Y-m-d', $value);
|
|
if (!$date || $date->format('Y-m-d') !== $value) {
|
|
throw new UnprocessableEntityHttpException(sprintf('%s must use Y-m-d format.', $field));
|
|
}
|
|
|
|
return $date;
|
|
}
|
|
}
|