Skip to content

Commit

Permalink
feat: file reader add option delimiter and defaultData
Browse files Browse the repository at this point in the history
FileReader use array for options
  • Loading branch information
Davidmattei committed Oct 29, 2024
1 parent 1bbe7d5 commit 6e27cc1
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 16 deletions.
9 changes: 8 additions & 1 deletion EMS/common-bundle/src/Common/File/FileReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,20 @@ final class FileReader implements FileReaderInterface
/**
* {@inheritDoc}
*/
public function getData(string $filename, bool $skipFirstRow = false, string $encoding = null): array
public function getData(string $filename, array $options = []): array
{
$skipFirstRow = true === ($options['skipFirstRow'] ?? false);
$encoding = $options['encoding'] ?? null;

$reader = IOFactory::createReaderForFile($filename);
if (($reader instanceof Csv || $reader instanceof Html || $reader instanceof Slk) && null !== $encoding) {
$reader->setInputEncoding($encoding);
}

if ($reader instanceof Csv && isset($options['delimiter'])) {
$reader->setDelimiter($options['delimiter']);
}

$data = $reader->load($filename)->getActiveSheet()->toArray();

if ($skipFirstRow) {
Expand Down
8 changes: 7 additions & 1 deletion EMS/common-bundle/src/Contracts/File/FileReaderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@
interface FileReaderInterface
{
/**
* @param array{
* skipFirstRow?: bool,
* encoding?: ?string,
* delimiter?: ?string,
* } $options
*
* @return array<mixed>
*/
public function getData(string $filename, bool $skipFirstRow = false, string $encoding = null): array;
public function getData(string $filename, array $options = []): array;
}
33 changes: 21 additions & 12 deletions elasticms-cli/src/Client/File/FileReaderImportConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@

class FileReaderImportConfig
{
/**
* @param array<string, mixed> $defaultData
*/
private function __construct(
public bool $generateHash = false,
public bool $deleteMissingDocuments = false,
public ?string $delimiter = null,
public array $defaultData = [],
public ?string $ouuidExpression = "row['ouuid']",
public ?string $encoding = null
public ?string $encoding = null,
) {
}

Expand All @@ -24,23 +29,27 @@ public static function createFromArray(array $config): self
$optionsResolver = new OptionsResolver();
$optionsResolver
->setDefaults([
'generateHash' => false,
'deleteMissingDocuments' => false,
'ouuidExpression' => 'row[\'ouuid\']',
'delimiter' => null,
'default_data' => [],
'delete_missing_documents' => false,
'encoding' => null,
'generate_hash' => false,
'ouuid_expression' => 'row[\'ouuid\']',
])
->setAllowedTypes('generateHash', 'bool')
->setAllowedTypes('deleteMissingDocuments', 'bool')
->setAllowedTypes('ouuidExpression', ['string', 'null'])
->setAllowedTypes('delete_missing_documents', 'bool')
->setAllowedTypes('generate_hash', 'bool')
->setAllowedTypes('ouuid_expression', ['string', 'null'])
;

$config = $optionsResolver->resolve($config);
$options = $optionsResolver->resolve($config);

return new self(
generateHash: $config['generateHash'],
deleteMissingDocuments: $config['deleteMissingDocuments'],
ouuidExpression: $config['ouuidExpression'],
encoding: $config['encoding'],
generateHash: $options['generate_hash'],
deleteMissingDocuments: $options['delete_missing_documents'],
delimiter: $options['delimiter'],
defaultData: $options['default_data'],
ouuidExpression: $options['ouuid_expression'],
encoding: $options['encoding'],
);
}
}
5 changes: 4 additions & 1 deletion elasticms-cli/src/Command/Document/DocumentUpdateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$config = DocumentUpdateConfig::fromFile($this->configFile);

$this->io->section('Reading data');
$dataArray = $this->fileReader->getData($this->dataFilePath, $this->dataSkipFirstRow);
$dataArray = $this->fileReader->getData(
filename: $this->dataFilePath,
options: ['skipFirstRow' => $this->dataSkipFirstRow]
);

$data = new Data($dataArray);
$this->io->writeln(\sprintf('Loaded data in memory: %d rows', \count($data)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}

$expressionLanguage = new ExpressionLanguage();
$rows = $this->fileReader->getData($file->getFilename(), false, $config->encoding);
$rows = $this->fileReader->getData($file->getFilename(), [
'delimiter' => $config->delimiter,
'encoding' => $config->encoding,
]);
$header = \array_map('trim', $rows[0] ?? []);

$ouuids = [];
Expand Down

0 comments on commit 6e27cc1

Please sign in to comment.