2026-11-10 18:08:41 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
|
2026-11-14 08:25:50 +01:00
|
|
|
use League\Csv\Serializer\MapCell;
|
2026-11-10 18:08:41 +01:00
|
|
|
|
2026-11-10 19:29:20 +01:00
|
|
|
class EmployeeCsvImportLineService
|
2026-11-10 18:08:41 +01:00
|
|
|
{
|
2026-11-14 08:25:50 +01:00
|
|
|
private const array SUPPORTED_DATE_FORMATS = ['!d/m/Y', '!d-m-Y'];
|
2026-11-10 18:08:41 +01:00
|
|
|
|
2026-11-14 08:25:50 +01:00
|
|
|
#[MapCell(column: 'Matricule')]
|
2026-11-10 18:08:41 +01:00
|
|
|
public string $matricule;
|
|
|
|
|
|
2026-11-14 08:25:50 +01:00
|
|
|
#[MapCell(column: 'Nom')]
|
2026-11-10 18:08:41 +01:00
|
|
|
public string $lastName;
|
|
|
|
|
|
2026-11-14 08:25:50 +01:00
|
|
|
#[MapCell(column: 'Prénom')]
|
2026-11-10 18:08:41 +01:00
|
|
|
public string $firstName;
|
|
|
|
|
|
2026-11-14 08:25:50 +01:00
|
|
|
public \DateTimeImmutable $startDate;
|
|
|
|
|
|
|
|
|
|
public ?\DateTimeImmutable $endDate = null;
|
|
|
|
|
|
|
|
|
|
public ?\DateTimeImmutable $expectedEndDate = null;
|
|
|
|
|
|
|
|
|
|
#[MapCell(column: 'Etablissement')]
|
|
|
|
|
public string $establishment;
|
|
|
|
|
|
|
|
|
|
#[MapCell(column: 'Site')]
|
|
|
|
|
public string $site;
|
|
|
|
|
|
|
|
|
|
public string $service;
|
|
|
|
|
|
|
|
|
|
#[MapCell(column: 'Date début')]
|
|
|
|
|
public function setStartDate(?string $value): void
|
2026-11-10 18:08:41 +01:00
|
|
|
{
|
|
|
|
|
$this->startDate = $this->parseMandatoryDate($value);
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-14 08:25:50 +01:00
|
|
|
#[MapCell(column: 'Date fin')]
|
|
|
|
|
public function setEndDate(?string $value): void
|
2026-11-10 18:08:41 +01:00
|
|
|
{
|
|
|
|
|
$this->endDate = $this->parseDate($value);
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-14 08:25:50 +01:00
|
|
|
#[MapCell(column: 'Date fin prévue')]
|
|
|
|
|
public function setExpectedEndDate(?string $value): void
|
2026-11-10 18:08:41 +01:00
|
|
|
{
|
|
|
|
|
$this->expectedEndDate = $this->parseDate($value);
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-14 08:25:50 +01:00
|
|
|
#[MapCell(column: 'Service')]
|
|
|
|
|
public function setService(?string $value): void
|
2026-11-10 18:08:41 +01:00
|
|
|
{
|
2026-11-14 08:25:50 +01:00
|
|
|
$value ??= '';
|
2026-11-10 18:08:41 +01:00
|
|
|
// NOTE: Hack in order to remove the site repetition in the service code, e.g., LDCCAV030 -> 030
|
|
|
|
|
$this->service = preg_replace('/^.*?(\d+)$/', '$1', $value) ?? $value;
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-14 08:25:50 +01:00
|
|
|
private function parseMandatoryDate(?string $value): \DateTimeImmutable
|
2026-11-10 18:08:41 +01:00
|
|
|
{
|
|
|
|
|
$date = $this->parseDate($value);
|
|
|
|
|
|
|
|
|
|
if ($date === null) {
|
|
|
|
|
throw new \InvalidArgumentException("Invalid date format: $value");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $date;
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-14 08:25:50 +01:00
|
|
|
private function parseDate(?string $value): ?\DateTimeImmutable
|
2026-11-10 18:08:41 +01:00
|
|
|
{
|
|
|
|
|
if (empty($value)) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-14 08:25:50 +01:00
|
|
|
foreach (self::SUPPORTED_DATE_FORMATS as $format) {
|
|
|
|
|
$date = \DateTimeImmutable::createFromFormat($format, $value);
|
|
|
|
|
if ($date !== false) {
|
|
|
|
|
return $date;
|
|
|
|
|
}
|
2026-11-10 18:08:41 +01:00
|
|
|
}
|
|
|
|
|
|
2026-11-14 08:25:50 +01:00
|
|
|
throw new \InvalidArgumentException("Invalid date format: $value");
|
2026-11-10 18:08:41 +01:00
|
|
|
}
|
|
|
|
|
}
|