ldc-cocotte/app/Services/EmployeeCsvImportLineService.php
Jérémy Munsch 552d220b69
refactor(csv): replace Fusonic CSV reader with League CSV
Updated the CSV reading implementation to use the League CSV library, enhancing flexibility and maintainability.
Removed the EnhancedCsvReader class and adjusted the EmployeeCsvReaderService accordingly.
2026-11-14 08:25:50 +01:00

88 lines
2.2 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Services;
use League\Csv\Serializer\MapCell;
class EmployeeCsvImportLineService
{
private const array SUPPORTED_DATE_FORMATS = ['!d/m/Y', '!d-m-Y'];
#[MapCell(column: 'Matricule')]
public string $matricule;
#[MapCell(column: 'Nom')]
public string $lastName;
#[MapCell(column: 'Prénom')]
public string $firstName;
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
{
$this->startDate = $this->parseMandatoryDate($value);
}
#[MapCell(column: 'Date fin')]
public function setEndDate(?string $value): void
{
$this->endDate = $this->parseDate($value);
}
#[MapCell(column: 'Date fin prévue')]
public function setExpectedEndDate(?string $value): void
{
$this->expectedEndDate = $this->parseDate($value);
}
#[MapCell(column: 'Service')]
public function setService(?string $value): void
{
$value ??= '';
// 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;
}
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;
}
foreach (self::SUPPORTED_DATE_FORMATS as $format) {
$date = \DateTimeImmutable::createFromFormat($format, $value);
if ($date !== false) {
return $date;
}
}
throw new \InvalidArgumentException("Invalid date format: $value");
}
}