Merge branch 'develop' into trunk

This commit is contained in:
Jérémy Munsch 2026-11-17 10:26:20 +01:00
commit d016a66601
No known key found for this signature in database
GPG key ID: 2AAECDA6B7330F91
2 changed files with 135 additions and 3 deletions

View file

@ -4,7 +4,9 @@ declare(strict_types=1);
namespace App\Services;
use League\Csv\Bom;
use League\Csv\CharsetConverter;
use League\Csv\Info;
use League\Csv\Reader;
use League\Csv\Serializer\Denormalizer;
use Psr\Log\LoggerInterface;
@ -16,6 +18,9 @@ class EmployeeCsvReaderService
private const string CSV_DELIMITER = ';';
/** @var array<int, string> */
private const array SUPPORTED_DELIMITERS = [';', ',', "\t"];
public function __construct(
private readonly LoggerInterface $logger
) {
@ -33,6 +38,8 @@ class EmployeeCsvReaderService
$this->logger->debug('Input stream is readable.');
$headers = [];
try {
$sourceEncoding = $this->detectEncoding($fromStream);
$csv = $this->createCsvReader($fromStream);
@ -42,6 +49,10 @@ class EmployeeCsvReaderService
$csv->setDelimiter(self::CSV_DELIMITER);
if ($sourceEncoding !== null) {
if (in_array($sourceEncoding, ['UTF-16', 'UTF-32'], true)) {
$csv->includeInputBOM();
}
CharsetConverter::addTo($csv, $sourceEncoding, 'UTF-8');
}
@ -52,7 +63,7 @@ class EmployeeCsvReaderService
));
$results = [];
$headers = array_map(trim(...), $csv->getHeader());
$headers = $this->resolveHeaders($csv);
$denormalizer = new Denormalizer(EmployeeCsvImportLineService::class, $headers);
foreach ($csv->getRecords() as $offset => $record) {
@ -76,6 +87,7 @@ class EmployeeCsvReaderService
return $results;
} catch (\Throwable $e) {
$this->logger->error('Failed to read employees from CSV.', [
'headers' => $headers,
'exception' => $e,
]);
@ -87,6 +99,42 @@ class EmployeeCsvReaderService
}
}
/**
* @param \League\Csv\Reader<array<array-key, mixed>> $csv
* @return array<int, string>
*/
private function resolveHeaders(Reader $csv): array
{
$headers = $this->normalizeHeaders($csv->getHeader());
if (count($headers) > 1) {
return $headers;
}
$stats = Info::getDelimiterStats($csv, self::SUPPORTED_DELIMITERS, 1);
arsort($stats);
$bestDelimiter = array_key_first($stats);
if ($bestDelimiter !== null && $stats[$bestDelimiter] > 0 && $bestDelimiter !== $csv->getDelimiter()) {
$csv->setDelimiter($bestDelimiter);
$headers = $this->normalizeHeaders($csv->getHeader());
}
return $headers;
}
/**
* @param array<int, string> $headers
* @return array<int, string>
*/
private function normalizeHeaders(array $headers): array
{
return array_map(
static fn (string $header): string => mb_trim(preg_replace('/^\xEF\xBB\xBF/', '', $header) ?? $header),
$headers
);
}
/**
* League\Csv stream filters (used by CharsetConverter) require a PHP stream resource
* rather than an SplFileObject instance.
@ -125,13 +173,27 @@ class EmployeeCsvReaderService
throw new \UnexpectedValueException('Failed to read sample from input stream.');
}
// ? Has BOM UTF-8 at the beginning of the sample ?
if (str_starts_with($sample, "\xEF\xBB\xBF")) {
$bom = Bom::tryFromSequence($sample);
if ($bom === Bom::Utf8) {
$fromStream->rewind();
return null;
}
if ($bom !== null) {
$fromStream->rewind();
$encoding = match ($bom) {
Bom::Utf16Le, Bom::Utf16Be => 'UTF-16',
Bom::Utf32Le, Bom::Utf32Be => 'UTF-32',
};
$this->logger->info('Detected BOM encoding, applying conversion.', [
'detected' => $bom->encoding(),
]);
return $encoding;
}
$safeSample = strlen($sample) === 4096 ? mb_strcut($sample, 0, 4092, 'UTF-8') : $sample;
// * Detect encoding.

View file

@ -206,4 +206,74 @@ describe('EmployeeCsvReaderService', function (): void {
->and($firstItem->lastName)->toBe('Noël')
->and($firstItem->firstName)->toBe('Céline');
});
it('imports employees from a comma-delimited Windows-1252 CSV file', function (): void {
// GIVEN
/** @var \Psr\Log\LoggerInterface&\Mockery\MockInterface $logger */
$logger = \Mockery::mock(LoggerInterface::class);
$logger->shouldReceive('debug')->once();
$logger->shouldReceive('info')->once();
$logger->shouldReceive('error')->never();
$service = new EmployeeCsvReaderService($logger);
$filePath = tempnam(sys_get_temp_dir(), 'employees-csv-comma-');
expect($filePath)->not->toBeFalse();
$utf8Csv = implode("\n", [
'Matricule,Nom,Prénom,Date début,Date fin,Date fin prévue,Etablissement,Site,Service',
'1234567,Dupont,Éric,01/10/2026,,,ETAB01,PA,LDCCAV030',
]);
file_put_contents($filePath, mb_convert_encoding($utf8Csv, 'Windows-1252', 'UTF-8'));
$fileObject = new \SplFileObject($filePath, 'r');
// WHEN
$items = $service->importEmployeesFromCsv($fileObject);
// THEN
$firstItem = $items[0];
assert($firstItem instanceof EmployeeCsvImportLineService);
expect($items)->toHaveCount(1)
->and($firstItem->matricule)->toBe('1234567')
->and($firstItem->firstName)->toBe('Éric');
unlink($filePath);
});
it('imports employees from a UTF-16LE BOM tab-delimited CSV file', function (): void {
// GIVEN
/** @var \Psr\Log\LoggerInterface&\Mockery\MockInterface $logger */
$logger = \Mockery::mock(LoggerInterface::class);
$logger->shouldReceive('debug')->once();
$logger->shouldReceive('info')->once();
$logger->shouldReceive('error')->never();
$service = new EmployeeCsvReaderService($logger);
$filePath = tempnam(sys_get_temp_dir(), 'employees-csv-utf16-');
expect($filePath)->not->toBeFalse();
$utf8Csv = implode("\n", [
"Matricule\tNom\tPrénom\tDate début\tDate fin\tDate fin prévue\tEtablissement\tSite\tService",
"1234567\tFrançois\tÉric\t01/10/2026\t\t\tETAB01\tPA\tLDCCAV030",
]);
file_put_contents($filePath, "\xFF\xFE" . mb_convert_encoding($utf8Csv, 'UTF-16LE', 'UTF-8'));
$fileObject = new \SplFileObject($filePath, 'r');
// WHEN
$items = $service->importEmployeesFromCsv($fileObject);
// THEN
$firstItem = $items[0];
assert($firstItem instanceof EmployeeCsvImportLineService);
expect($items)->toHaveCount(1)
->and($firstItem->lastName)->toBe('François')
->and($firstItem->firstName)->toBe('Éric');
unlink($filePath);
});
});