From 552d220b698d94c13a7e9ec90c8f7d52ac672282 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Munsch?= Date: Sat, 14 Nov 2026 08:25:50 +0100 Subject: [PATCH] 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. --- app/Lib/EnhancedCsvReader.php | 193 ------------------ app/Services/EmployeeCsvImportLineService.php | 70 ++++--- app/Services/EmployeeCsvReaderService.php | 56 +++-- composer.json | 2 +- composer.lock | 151 +++++++++----- .../Services/EmployeeCsvReaderServiceTest.php | 2 + 6 files changed, 177 insertions(+), 297 deletions(-) delete mode 100644 app/Lib/EnhancedCsvReader.php diff --git a/app/Lib/EnhancedCsvReader.php b/app/Lib/EnhancedCsvReader.php deleted file mode 100644 index 30fc9760..00000000 --- a/app/Lib/EnhancedCsvReader.php +++ /dev/null @@ -1,193 +0,0 @@ - $className the class which data should be mapped to - * @param ?callable(int, \Throwable): E $callback the callback to handle errors for each row - * @return \Traversable - */ - #[\Override] - public function readObjects(string $className, ?callable $callback = null): iterable - { - $resource = $this->getResource(); - - /** @var int $resourcePosition */ - $resourcePosition = $resource->ftell(); - - // Skip the BOM - if ($this->options->removeBOM) { - $this->inheritedSkipBom($resource); - } - - // Read header - $header = null; - - if ($this->options->hasHeaderRow) { - $header = $resource->fgetcsv( - separator: $this->options->delimiter, - enclosure: $this->options->enclosure, - escape: $this->options->escape - ); - - if ($header === false || $header === [null]) { - throw new CsvReaderException('Cannot get line from file pointer and parse it for CSV fields.'); - } - - Assert::allString($header); - $header = $this->convertRowEncoding($header); - } - - // Build mapping - $mappingBuilder = new MappingBuilder(); - $mapping = $mappingBuilder->build($header, $className); - - $line = $this->options->hasHeaderRow ? 2 : 1; - - while (! $resource->eof()) { - $row = $resource->fgetcsv( - separator: $this->options->delimiter, - enclosure: $this->options->enclosure, - escape: $this->options->escape - ); - - if ($row === false || $row === [null]) { - $line++; - - continue; - } - - try { - Assert::allString($row); - $row = $this->convertRowEncoding($row); - - yield $this->inheritedMap($row, $className, $mapping); - } catch (\Throwable $e) { - if ($callback !== null) { - yield $callback($line, $e); - } else { - throw $e; - } - } - $line++; - } - - // Rewind resource to its original position or close it if source was a path - if ($this->fileToRead instanceof \SplFileObject) { - $this->fileToRead->fseek($resourcePosition); - } else { - $resource = null; - } - } - - /** - * @param array $row - * @return array - */ - private function convertRowEncoding(array $row): array - { - if ($this->sourceEncoding === null || $this->sourceEncoding === 'UTF-8') { - return $row; - } - - return array_map( - fn (string $value): string => mb_convert_encoding( - $value, - 'UTF-8', - $this->sourceEncoding - ) ?: $value, - $row - ); - } - - private function getResource(): \SplFileObject - { - if ($this->fileToRead instanceof \SplFileObject) { - return $this->fileToRead; - } - - if (\is_string($this->fileToRead)) { - $resource = new \SplFileObject($this->fileToRead, 'r'); - if (! $resource->isReadable()) { - throw new CsvReaderException('Cannot open the CSV file.'); - } - - return $resource; - } - - throw new CsvReaderException('Invalid file to read.'); - } - - private function inheritedSkipBom(\SplFileObject $resource): void - { - $position = $resource->ftell(); - - if ($position === false) { - throw new CsvReaderException('Cannot read the current position of the file read/write pointer.'); - } - - if ($resource->fread(3) !== \chr(0xEF) . \chr(0xBB) . \chr(0xBF)) { - $resource->fseek($position); - } - } - - /** - * @template T of object - * - * @param array $row - * @param class-string $className - * @param \Fusonic\CsvReader\Mapping\MappingInfo[] $mappings - * @return T - */ - protected function inheritedMap(array $row, string $className, array $mappings): object - { - $object = new $className(); - - foreach ($mappings as $mapping) { - if (! array_key_exists($mapping->getIndex(), $row)) { - throw new CsvReaderException("Column index {$mapping->getIndex()} does not exist in CSV row."); - } - - $mapping->setValue( - $object, - $this->options->valueConverter->convert( - $row[$mapping->getIndex()], - $mapping->getType() - ) - ); - } - - return $object; - } -} diff --git a/app/Services/EmployeeCsvImportLineService.php b/app/Services/EmployeeCsvImportLineService.php index 514f976f..ce5a0120 100644 --- a/app/Services/EmployeeCsvImportLineService.php +++ b/app/Services/EmployeeCsvImportLineService.php @@ -4,61 +4,62 @@ declare(strict_types=1); namespace App\Services; -use Fusonic\CsvReader\Attributes\TitleMapping; +use League\Csv\Serializer\MapCell; class EmployeeCsvImportLineService { - private const DATE_FORMAT = 'd/m/Y'; + private const array SUPPORTED_DATE_FORMATS = ['!d/m/Y', '!d-m-Y']; - #[TitleMapping('Matricule')] + #[MapCell(column: 'Matricule')] public string $matricule; - #[TitleMapping('Nom')] + #[MapCell(column: 'Nom')] public string $lastName; - #[TitleMapping('Prénom')] + #[MapCell(column: 'Prénom')] public string $firstName; - #[TitleMapping('Date début')] - public function setStartDate(string $value): void + 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); } - public \DateTimeImmutable $startDate; - - #[TitleMapping('Date fin')] - public function setEndDate(string $value): void + #[MapCell(column: '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 + #[MapCell(column: '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 + #[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; } - public string $service; - - private function parseMandatoryDate(string $value): \DateTimeImmutable + private function parseMandatoryDate(?string $value): \DateTimeImmutable { $date = $this->parseDate($value); @@ -69,18 +70,19 @@ class EmployeeCsvImportLineService return $date; } - private function parseDate(string $value): ?\DateTimeImmutable + 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"); + foreach (self::SUPPORTED_DATE_FORMATS as $format) { + $date = \DateTimeImmutable::createFromFormat($format, $value); + if ($date !== false) { + return $date; + } } - return $date; + throw new \InvalidArgumentException("Invalid date format: $value"); } } diff --git a/app/Services/EmployeeCsvReaderService.php b/app/Services/EmployeeCsvReaderService.php index d26fd77a..b060b766 100644 --- a/app/Services/EmployeeCsvReaderService.php +++ b/app/Services/EmployeeCsvReaderService.php @@ -4,12 +4,18 @@ declare(strict_types=1); namespace App\Services; -use App\Lib\EnhancedCsvReader; -use Fusonic\CsvReader\CsvReaderOptions; +use League\Csv\CharsetConverter; +use League\Csv\Reader; +use League\Csv\Serializer\Denormalizer; use Psr\Log\LoggerInterface; +use Webmozart\Assert\Assert; class EmployeeCsvReaderService { + private const string CSV_ESCAPE = ''; + + private const string CSV_DELIMITER = ';'; + public function __construct( private readonly LoggerInterface $logger ) { @@ -28,32 +34,54 @@ class EmployeeCsvReaderService $this->logger->debug('Input stream is readable.'); try { - $options = new CsvReaderOptions(); - $options->delimiter = ';'; - $sourceEncoding = $this->detectEncoding($fromStream); - $reader = new EnhancedCsvReader($fromStream, $options, $sourceEncoding); + $csv = Reader::from($fromStream); + $csv->setHeaderOffset(0); + $csv->setEscape(self::CSV_ESCAPE); + $csv->setDelimiter(self::CSV_DELIMITER); - $items = $reader->readObjects( - EmployeeCsvImportLineService::class, - function (int $line, \Throwable $e): string { + if ($sourceEncoding !== null) { + CharsetConverter::addTo($csv, $sourceEncoding, 'UTF-8'); + } + + // * Trim lines. + $csv->addFormatter(static fn (array $row): array => array_map( + static fn (?string $value): ?string => $value !== null ? mb_trim($value) : null, + $row + )); + + $results = []; + $headers = array_map(trim(...), $csv->getHeader()); + $denormalizer = new Denormalizer(EmployeeCsvImportLineService::class, $headers); + + foreach ($csv->getRecords() as $offset => $record) { + try { + $mappedObject = $denormalizer->denormalize($record); + + Assert::isInstanceOf($mappedObject, EmployeeCsvImportLineService::class); + + $results[] = $mappedObject; + } catch (\Throwable $e) { + $line = $offset + 1; $this->logger->error('Failed to read line from CSV.', [ 'line' => $line, 'exception' => $e, ]); - return $e->getMessage(); + $results[] = $e->getMessage(); } - ); + } - return iterator_to_array($items, false); + return $results; } catch (\Throwable $e) { $this->logger->error('Failed to read employees from CSV.', [ 'exception' => $e, ]); - report($e); + if (function_exists('report')) { + report($e); + } return []; } @@ -73,7 +101,7 @@ class EmployeeCsvReaderService if (str_starts_with($sample, "\xEF\xBB\xBF")) { $fromStream->rewind(); - return 'UTF-8'; + return null; } $safeSample = strlen($sample) === 4096 ? mb_strcut($sample, 0, 4092, 'UTF-8') : $sample; diff --git a/composer.json b/composer.json index 79d8ba8d..8496325d 100644 --- a/composer.json +++ b/composer.json @@ -11,12 +11,12 @@ "php": "^8.5", "axlon/laravel-postal-code-validation": "^3.9", "diglactic/laravel-breadcrumbs": "^10.1", - "fusonic/csv-reader": "^1.0", "kwaadpepper/serial-int-caster": "^2.0", "laravel/framework": "^13.0", "laravel/tinker": "^3.0", "laravel/ui": "^4.6", "ldc/ldccrmv4client": "^4.0", + "league/csv": "*", "maatwebsite/excel": "4.x-dev", "mews/purifier": "^3.3", "moneyphp/money": "^4.9", diff --git a/composer.lock b/composer.lock index 4f668914..4a7eac48 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "c8e35cb5f86dd59bc4a8addb54133ba3", + "content-hash": "3b4525dc0f29aff6e84ff0aebf6f015a", "packages": [ { "name": "axlon/laravel-postal-code-validation", @@ -1081,60 +1081,6 @@ ], "time": "2025-12-03T09:33:47+00:00" }, - { - "name": "fusonic/csv-reader", - "version": "1.0.0", - "source": { - "type": "git", - "url": "https://github.com/fusonic/php-csv-reader.git", - "reference": "1b3a6f25f66144fffa724a861ef7395a20016f60" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/fusonic/php-csv-reader/zipball/1b3a6f25f66144fffa724a861ef7395a20016f60", - "reference": "1b3a6f25f66144fffa724a861ef7395a20016f60", - "shasum": "" - }, - "require": { - "php": ">=8.4" - }, - "require-dev": { - "friendsofphp/php-cs-fixer": "^3.95", - "infection/infection": "^0.35", - "phpstan/phpstan": "^2.2", - "phpstan/phpstan-deprecation-rules": "^2.0", - "phpstan/phpstan-phpunit": "^2.0", - "phpstan/phpstan-strict-rules": "^2.0", - "phpunit/phpunit": "^13.4", - "rector/rector": "^2.6", - "tomasvotruba/type-coverage": "^2.3" - }, - "suggest": { - "ext-intl": "Required to use IntlValueConverter for advanced value conversion." - }, - "type": "library", - "autoload": { - "psr-4": { - "Fusonic\\CsvReader\\": "src/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Fusonic GmbH", - "email": "php@fusonic.net" - } - ], - "description": "Map data from CSV files to typed PHP models.", - "support": { - "issues": "https://github.com/fusonic/php-csv-reader/issues", - "source": "https://github.com/fusonic/php-csv-reader/tree/1.0.0" - }, - "time": "2026-11-04T08:38:29+00:00" - }, { "name": "giggsey/libphonenumber-for-php-lite", "version": "9.0.41", @@ -2521,6 +2467,101 @@ ], "time": "2022-12-11T20:36:23+00:00" }, + { + "name": "league/csv", + "version": "9.29.0", + "source": { + "type": "git", + "url": "https://github.com/thephpleague/csv.git", + "reference": "13d5600d2c935c8785707e7d1d44ca1f2a22d6f4" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/thephpleague/csv/zipball/13d5600d2c935c8785707e7d1d44ca1f2a22d6f4", + "reference": "13d5600d2c935c8785707e7d1d44ca1f2a22d6f4", + "shasum": "" + }, + "require": { + "ext-filter": "*", + "php": "^8.2.0" + }, + "require-dev": { + "ext-dom": "*", + "ext-xdebug": "*", + "friendsofphp/php-cs-fixer": "^3.92.3", + "phpbench/phpbench": "^1.4.3", + "phpstan/phpstan": "^2.2.8", + "phpstan/phpstan-deprecation-rules": "^2.0.5", + "phpstan/phpstan-phpunit": "^2.0.18", + "phpstan/phpstan-strict-rules": "^2.0.12", + "phpunit/phpunit": "^11.5.22 || ^12.5.4 || ^13.3.1", + "rector/rector": "^2.6.3", + "symfony/var-dumper": "^6.4.8 || ^7.4.0 || ^8.0" + }, + "suggest": { + "ext-dom": "Required to use the XMLConverter and the HTMLConverter classes", + "ext-iconv": "Needed to ease transcoding CSV using iconv stream filters", + "ext-mbstring": "Needed to ease transcoding CSV using mb stream filters", + "ext-mysqli": "Requiered to use the package with the MySQLi extension", + "ext-pdo": "Required to use the package with the PDO extension", + "ext-pgsql": "Requiered to use the package with the PgSQL extension", + "ext-sqlite3": "Required to use the package with the SQLite3 extension" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "9.x-dev" + } + }, + "autoload": { + "files": [ + "src/functions_include.php" + ], + "psr-4": { + "League\\Csv\\": "src" + }, + "classmap": [ + "polyfill" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Ignace Nyamagana Butera", + "email": "nyamsprod@gmail.com", + "homepage": "https://github.com/nyamsprod/", + "role": "Developer" + } + ], + "description": "CSV data manipulation made easy in PHP", + "homepage": "https://csv.thephpleague.com", + "keywords": [ + "convert", + "csv", + "export", + "filter", + "import", + "read", + "transform", + "write" + ], + "support": { + "docs": "https://csv.thephpleague.com", + "issues": "https://github.com/thephpleague/csv/issues", + "rss": "https://github.com/thephpleague/csv/releases.atom", + "source": "https://github.com/thephpleague/csv" + }, + "funding": [ + { + "url": "https://github.com/sponsors/nyamsprod", + "type": "github" + } + ], + "time": "2026-10-28T08:56:24+00:00" + }, { "name": "league/flysystem", "version": "3.36.0", diff --git a/tests/Unit/App/Services/EmployeeCsvReaderServiceTest.php b/tests/Unit/App/Services/EmployeeCsvReaderServiceTest.php index 2851ead8..cca4d72f 100644 --- a/tests/Unit/App/Services/EmployeeCsvReaderServiceTest.php +++ b/tests/Unit/App/Services/EmployeeCsvReaderServiceTest.php @@ -6,6 +6,8 @@ use App\Services\EmployeeCsvImportLineService; use App\Services\EmployeeCsvReaderService; use Psr\Log\LoggerInterface; +covers(EmployeeCsvReaderService::class); + afterEach(function (): void { \Mockery::close(); });