- Added EmployeesImportResults class to encapsulate import results. - Created ImportEmployees action to handle CSV import and upsert employees. - Introduced ImportEmployeesInput class for input validation. - Developed EmployeeCsvReaderService for reading and parsing CSV files. - Implemented EmployeeCsvImportLine to map CSV rows to employee data. - Added EnhancedCsvReader for improved CSV handling. - Updated composer.json to include fusonic/csv-reader dependency. - Created tests for the new employee import functionality.
159 lines
4.5 KiB
PHP
159 lines
4.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Lib;
|
|
|
|
use Fusonic\CsvReader\CsvReader;
|
|
use Fusonic\CsvReader\CsvReaderOptions;
|
|
use Fusonic\CsvReader\Exceptions\CsvReaderException;
|
|
use Fusonic\CsvReader\Mapping\MappingBuilder;
|
|
use Webmozart\Assert\Assert;
|
|
|
|
class EnhancedCsvReader extends CsvReader
|
|
{
|
|
public function __construct(
|
|
protected mixed $fileToRead,
|
|
private CsvReaderOptions $options = new CsvReaderOptions(),
|
|
) {
|
|
if (! \is_string($fileToRead) && ! \is_resource($fileToRead)) {
|
|
throw new \TypeError(
|
|
'Fusonic\\CsvReader\\CsvReader::__construct(): Argument #1 ($fileToRead) must be of type string or resource.'
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Iterates data from the configured CSV file and maps each row to an object of type $className.
|
|
*
|
|
* @template T of object
|
|
* @template E
|
|
*
|
|
* @param class-string<T> $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<T|E>
|
|
*/
|
|
#[\Override]
|
|
public function readObjects(string $className, ?callable $callback = null): iterable
|
|
{
|
|
$resource = $this->getResource();
|
|
|
|
/** @var int $resourcePosition */
|
|
$resourcePosition = ftell($resource);
|
|
|
|
// Skip the BOM
|
|
if ($this->options->removeBOM) {
|
|
$this->inheritedSkipBom($resource);
|
|
}
|
|
|
|
// Read header
|
|
$header = null;
|
|
|
|
if ($this->options->hasHeaderRow) {
|
|
$header = fgetcsv(
|
|
stream: $resource,
|
|
length: 0,
|
|
separator: $this->options->delimiter,
|
|
enclosure: $this->options->enclosure,
|
|
escape: $this->options->escape
|
|
);
|
|
|
|
if ($header === false) {
|
|
throw new CsvReaderException('Cannot get line from file pointer and parse it for CSV fields.');
|
|
}
|
|
}
|
|
|
|
// Build mapping
|
|
$mappingBuilder = new MappingBuilder();
|
|
$mapping = $mappingBuilder->build($header, $className);
|
|
|
|
$line = $this->options->hasHeaderRow ? 1 : 0;
|
|
while (
|
|
($row = fgetcsv(
|
|
$resource,
|
|
0,
|
|
$this->options->delimiter,
|
|
$this->options->enclosure,
|
|
$this->options->escape
|
|
)) !== false
|
|
) {
|
|
try {
|
|
Assert::allString($row);
|
|
|
|
yield $this->inheritedMap($row, $className, $mapping);
|
|
} catch (\Throwable $e) {
|
|
if ($callback !== null) {
|
|
yield $callback($line, $e);
|
|
}
|
|
}
|
|
$line++;
|
|
}
|
|
|
|
// Rewind resource to its original position or close it if source was a path
|
|
if (\is_resource($this->fileToRead)) {
|
|
fseek($this->fileToRead, $resourcePosition);
|
|
} else {
|
|
fclose($resource);
|
|
}
|
|
}
|
|
|
|
/** @return resource */
|
|
private function getResource(): mixed
|
|
{
|
|
if (\is_resource($this->fileToRead)) {
|
|
return $this->fileToRead;
|
|
}
|
|
|
|
if (is_string($this->fileToRead)) {
|
|
$resource = fopen($this->fileToRead, 'r');
|
|
if ($resource === false) {
|
|
throw new CsvReaderException('Cannot open the CSV file.');
|
|
}
|
|
|
|
return $resource;
|
|
}
|
|
|
|
throw new CsvReaderException('Invalid file to read.');
|
|
}
|
|
|
|
/**
|
|
* @param resource $resource
|
|
*/
|
|
private function inheritedSkipBom($resource): void
|
|
{
|
|
$position = ftell($resource);
|
|
|
|
if ($position === false) {
|
|
throw new CsvReaderException('Cannot read the current position of the file read/write pointer.');
|
|
}
|
|
|
|
if (fread($resource, 3) !== \chr(0xEF) . \chr(0xBB) . \chr(0xBF)) {
|
|
fseek($resource, $position);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @template T of object
|
|
*
|
|
* @param array<string> $row
|
|
* @param class-string<T> $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) {
|
|
$mapping->setValue(
|
|
$object,
|
|
$this->options->valueConverter->convert(
|
|
$row[$mapping->getIndex()],
|
|
$mapping->getType()
|
|
)
|
|
);
|
|
}
|
|
|
|
return $object;
|
|
}
|
|
}
|