# Conflicts: # app/Services/EmployeeCsvReaderService.php # tests/Feature/App/Console/Commands/UpsertEmployeesFormFileTest.php # tests/Unit/App/Services/EmployeeCsvReaderServiceTest.php
221 lines
6.7 KiB
PHP
221 lines
6.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Services;
|
|
|
|
use Illuminate\Container\Container;
|
|
use Illuminate\Contracts\Debug\ExceptionHandler;
|
|
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;
|
|
use Webmozart\Assert\Assert;
|
|
|
|
class EmployeeCsvReaderService
|
|
{
|
|
private const string CSV_ESCAPE = '';
|
|
|
|
private const string CSV_DELIMITER = ';';
|
|
|
|
/** @var array<int, string> */
|
|
private const array SUPPORTED_DELIMITERS = [';', ',', "\t"];
|
|
|
|
public function __construct(
|
|
private readonly LoggerInterface $logger
|
|
) {
|
|
}
|
|
|
|
/**
|
|
* @return array<int, EmployeeCsvImportLineService|string>
|
|
*/
|
|
public function importEmployeesFromCsv(\SplFileObject $fromStream): array
|
|
{
|
|
if (! $fromStream->isReadable()) {
|
|
$this->logger->error('Input stream is not readable.');
|
|
throw new \InvalidArgumentException('Input stream must be readable.');
|
|
}
|
|
|
|
$this->logger->debug('Input stream is readable.');
|
|
|
|
$headers = [];
|
|
|
|
try {
|
|
$sourceEncoding = $this->detectEncoding($fromStream);
|
|
$csv = $this->createCsvReader($fromStream);
|
|
|
|
$csv->setHeaderOffset(0);
|
|
$csv->setEscape(self::CSV_ESCAPE);
|
|
$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');
|
|
}
|
|
|
|
// * Trim lines.
|
|
$csv->addFormatter(static fn (array $row): array => array_map(
|
|
static fn (mixed $value): ?string => is_string($value) ? mb_trim($value) : null,
|
|
$row
|
|
));
|
|
|
|
$results = [];
|
|
$headers = $this->resolveHeaders($csv);
|
|
$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,
|
|
]);
|
|
|
|
$results[] = "Line {$line}: {$e->getMessage()}";
|
|
}
|
|
}
|
|
|
|
return $results;
|
|
} catch (\Throwable $e) {
|
|
$this->logger->error('Failed to read employees from CSV.', [
|
|
'headers' => $headers,
|
|
'exception' => $e,
|
|
]);
|
|
|
|
if (function_exists('report') && Container::getInstance()->bound(ExceptionHandler::class)) {
|
|
report($e);
|
|
}
|
|
|
|
$message = $headers !== []
|
|
? sprintf('%s (headers: %s)', $e->getMessage(), implode(', ', $headers))
|
|
: $e->getMessage();
|
|
|
|
return [$message];
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param \League\Csv\Reader<array<array-key, mixed>> $csv
|
|
* @return array<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<string> $headers
|
|
* @return array<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.
|
|
*
|
|
* @return \League\Csv\Reader<array<array-key, mixed>>
|
|
*/
|
|
private function createCsvReader(\SplFileObject $fromStream): Reader
|
|
{
|
|
$realPath = $fromStream->getRealPath();
|
|
if ($realPath !== false && is_file($realPath)) {
|
|
return Reader::fromPath($realPath);
|
|
}
|
|
|
|
$fromStream->rewind();
|
|
$content = '';
|
|
while (! $fromStream->eof()) {
|
|
$chunk = $fromStream->fread(8192);
|
|
if ($chunk === false) {
|
|
throw new \UnexpectedValueException('Failed to read from input stream.');
|
|
}
|
|
$content .= $chunk;
|
|
}
|
|
|
|
$fromStream->rewind();
|
|
|
|
return Reader::fromString($content);
|
|
}
|
|
|
|
private function detectEncoding(\SplFileObject $fromStream): ?string
|
|
{
|
|
$fromStream->rewind();
|
|
$sample = $fromStream->fread(4096);
|
|
|
|
if ($sample === false) {
|
|
$this->logger->error('Failed to read sample from input stream.');
|
|
throw new \UnexpectedValueException('Failed to read sample from input stream.');
|
|
}
|
|
|
|
$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.
|
|
$detected = mb_detect_encoding($safeSample, ['UTF-8', 'Windows-1252', 'ISO-8859-1'], true);
|
|
|
|
// * Rewind before reading with EnhancedCsvReader.
|
|
$fromStream->rewind();
|
|
|
|
if ($detected !== false && $detected !== 'UTF-8') {
|
|
$this->logger->info('Detected non-UTF-8 encoding, applying conversion.', [
|
|
'detected' => $detected,
|
|
]);
|
|
|
|
return $detected;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|