ldc-cocotte/app/Services/EmployeeCsvImportLineService.php
Jérémy Munsch b2989b4967
refactor(employee): update import handling and output structure
- Replaced resource handling with SplFileObject for better readability.
- Introduced ImportEmployeesOutput class for structured results.
- Updated related services and tests to accommodate new structures.
2026-11-10 19:31:34 +01:00

86 lines
2 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Services;
use Fusonic\CsvReader\Attributes\TitleMapping;
class EmployeeCsvImportLineService
{
private const DATE_FORMAT = 'd/m/Y';
#[TitleMapping('Matricule')]
public string $matricule;
#[TitleMapping('Nom')]
public string $lastName;
#[TitleMapping('Prénom')]
public string $firstName;
#[TitleMapping('Date début')]
public function setStartDate(string $value): void
{
$this->startDate = $this->parseMandatoryDate($value);
}
public \DateTimeImmutable $startDate;
#[TitleMapping('Date fin')]
public function setEndDate(string $value): void
{
$this->endDate = $this->parseDate($value);
}
public ?\DateTimeImmutable $endDate;
#[TitleMapping('Date fin prévue')]
public function setExpectedEndDate(string $value): void
{
$this->expectedEndDate = $this->parseDate($value);
}
public ?\DateTimeImmutable $expectedEndDate;
#[TitleMapping('Etablissement')]
public string $establishment;
#[TitleMapping('Site')]
public string $site;
#[TitleMapping('Service')]
public function setService(string $value): void
{
// 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;
}
public string $service;
private function parseMandatoryDate(string $value): \DateTimeImmutable
{
$date = $this->parseDate($value);
if ($date === null) {
throw new \InvalidArgumentException("Invalid date format: $value");
}
return $date;
}
private function parseDate(string $value): ?\DateTimeImmutable
{
if (empty($value)) {
return null;
}
$date = \DateTimeImmutable::createFromFormat(self::DATE_FORMAT, $value);
if ($date === false) {
throw new \InvalidArgumentException("Invalid date format: $value");
}
return $date;
}
}