Skip to content

Commit

Permalink
feat: file encoding for csv reader
Browse files Browse the repository at this point in the history
  • Loading branch information
theus77 committed Sep 8, 2023
1 parent 4c33937 commit 5fbed42
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
8 changes: 7 additions & 1 deletion EMS/common-bundle/src/Common/File/FileReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,21 @@

use EMS\CommonBundle\Contracts\File\FileReaderInterface;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Reader\Csv;
use PhpOffice\PhpSpreadsheet\Reader\Html;
use PhpOffice\PhpSpreadsheet\Reader\Slk;

final class FileReader implements FileReaderInterface
{
/**
* {@inheritDoc}
*/
public function getData(string $filename, bool $skipFirstRow = false): array
public function getData(string $filename, bool $skipFirstRow = false, string $encoding = null): array
{
$reader = IOFactory::createReaderForFile($filename);
if (($reader instanceof Csv || $reader instanceof Html || $reader instanceof Slk) && null !== $encoding) {
$reader->setInputEncoding($encoding);
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ interface FileReaderInterface
/**
* @return array<mixed>
*/
public function getData(string $filename, bool $skipFirstRow = false): array;
public function getData(string $filename, bool $skipFirstRow = false, string $encoding = null): array;
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ final class FileReaderImportCommand extends AbstractCommand
private const OPTION_GENERATE_HASH = 'generate-hash';
private const OPTION_DELETE_MISSING_DOCUMENTS = 'delete-missing-document';
private const OPTION_HASH_FILE = 'hash-file';
private const OPTION_ENCODING = 'encoding';
private string $ouuidExpression;
private string $contentType;
private string $file;
private bool $dryRun;
private bool $hashOuuid;
private bool $deleteMissingDocuments;
private bool $hashFile;
private ?string $encoding;

public function __construct(private readonly AdminHelper $adminHelper, private readonly FileReaderInterface $fileReader)
{
Expand All @@ -50,6 +52,7 @@ protected function configure(): void
->addOption(self::OPTION_DELETE_MISSING_DOCUMENTS, null, InputOption::VALUE_NONE, 'The command will delete content type document that are missing in the import file')
->addOption(self::OPTION_OUUID_EXPRESSION, null, InputOption::VALUE_OPTIONAL, 'Expression language apply to excel rows in order to identify the document by its ouuid. If equal to null new document will be created', "row['ouuid']")
->addOption(self::OPTION_HASH_FILE, null, InputOption::VALUE_NONE, 'Specify that the file argument is a file hash not a file path.')
->addOption(self::OPTION_ENCODING, null, InputOption::VALUE_OPTIONAL, 'Specify the file\'s encoding for csv, html and Slk file')
;
}

Expand All @@ -63,6 +66,7 @@ protected function initialize(InputInterface $input, OutputInterface $output): v
$this->hashOuuid = $this->getOptionBool(self::OPTION_GENERATE_HASH);
$this->deleteMissingDocuments = $this->getOptionBool(self::OPTION_DELETE_MISSING_DOCUMENTS);
$this->hashFile = $this->getOptionBool(self::OPTION_HASH_FILE);
$this->encoding = $this->getOptionStringNull(self::OPTION_ENCODING);
}

protected function execute(InputInterface $input, OutputInterface $output): int
Expand All @@ -79,7 +83,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$file = $this->hashFile ? $this->getFileByHash($this->file) : $this->file;

$expressionLanguage = new ExpressionLanguage();
$rows = $this->fileReader->getData($file);
$rows = $this->fileReader->getData($file, false, $this->encoding);
$header = $rows[0] ?? [];

$ouuids = [];
Expand Down

0 comments on commit 5fbed42

Please sign in to comment.