From 5fbed42f76c324a313c09b5c92d6d6a9b0edc664 Mon Sep 17 00:00:00 2001 From: Mathieu De Keyzer Date: Sat, 9 Sep 2023 00:51:28 +0200 Subject: [PATCH] feat: file encoding for csv reader --- EMS/common-bundle/src/Common/File/FileReader.php | 8 +++++++- .../src/Contracts/File/FileReaderInterface.php | 2 +- .../src/Command/FileReader/FileReaderImportCommand.php | 6 +++++- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/EMS/common-bundle/src/Common/File/FileReader.php b/EMS/common-bundle/src/Common/File/FileReader.php index e2aa902f1..a5bd958dc 100644 --- a/EMS/common-bundle/src/Common/File/FileReader.php +++ b/EMS/common-bundle/src/Common/File/FileReader.php @@ -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(); diff --git a/EMS/common-bundle/src/Contracts/File/FileReaderInterface.php b/EMS/common-bundle/src/Contracts/File/FileReaderInterface.php index fed2c0c47..8ee982038 100644 --- a/EMS/common-bundle/src/Contracts/File/FileReaderInterface.php +++ b/EMS/common-bundle/src/Contracts/File/FileReaderInterface.php @@ -9,5 +9,5 @@ interface FileReaderInterface /** * @return array */ - public function getData(string $filename, bool $skipFirstRow = false): array; + public function getData(string $filename, bool $skipFirstRow = false, string $encoding = null): array; } diff --git a/elasticms-cli/src/Command/FileReader/FileReaderImportCommand.php b/elasticms-cli/src/Command/FileReader/FileReaderImportCommand.php index f3f886887..80e5046f5 100644 --- a/elasticms-cli/src/Command/FileReader/FileReaderImportCommand.php +++ b/elasticms-cli/src/Command/FileReader/FileReaderImportCommand.php @@ -26,6 +26,7 @@ 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; @@ -33,6 +34,7 @@ final class FileReaderImportCommand extends AbstractCommand private bool $hashOuuid; private bool $deleteMissingDocuments; private bool $hashFile; + private ?string $encoding; public function __construct(private readonly AdminHelper $adminHelper, private readonly FileReaderInterface $fileReader) { @@ -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') ; } @@ -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 @@ -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 = [];