Skip to content

Commit

Permalink
Use League\Csv\Reader instead of Ddeboer\DataImport see BT#10895
Browse files Browse the repository at this point in the history
Fixes issues with files with BOM
Requires composer update
  • Loading branch information
jmontoyaa committed Feb 19, 2018
1 parent 1d21005 commit 98dd6d5
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 23 deletions.
1 change: 1 addition & 0 deletions composer.json
Expand Up @@ -95,6 +95,7 @@
"sylius/translation": "0.13.0",
"patchwork/utf8": "~1.2",
"ddeboer/data-import": "@stable",
"league/csv": "~8.0",
"phpoffice/phpexcel": "~1.8",
"webit/eval-math": "1.0.1",
"clue/graph": "~0.9.0",
Expand Down
32 changes: 9 additions & 23 deletions main/inc/lib/import.lib.php
@@ -1,9 +1,7 @@
<?php
/* For licensing terms, see /license.txt */

use Ddeboer\DataImport\Workflow;
use Ddeboer\DataImport\Reader\CsvReader;
use Ddeboer\DataImport\Writer\ArrayWriter;
use League\Csv\Reader;

/**
* Class Import
Expand All @@ -17,22 +15,11 @@ class Import
/**
* @param string $path
* @param bool $setFirstRowAsHeader
* @return CsvReader
* @return array
*/
public static function csv_reader($path, $setFirstRowAsHeader = true)
{
if (empty($path)) {
return false;
}

$file = new \SplFileObject($path);
$csvReader = new CsvReader($file, ';');

if ($setFirstRowAsHeader) {
$csvReader->setHeaderRowNumber(0);
}

return $csvReader;
return self::csvToArray($path);
}

/**
Expand All @@ -55,14 +42,13 @@ public static function csv_reader($path, $setFirstRowAsHeader = true)
*/
public static function csvToArray($filename)
{
$csvReader = self::csv_reader($filename);
$resultArray = [];
if ($csvReader) {
$workflow = new Workflow\StepAggregator($csvReader);
$writer = new ArrayWriter($resultArray);
$workflow->addWriter($writer)->process();
if (empty($filename)) {
return [];
}
$reader = Reader::createFromPath($filename, 'r');
$reader->setDelimiter(';');
$reader->stripBom(true);

return $resultArray;
return $reader->fetchAssoc(0);
}
}

0 comments on commit 98dd6d5

Please sign in to comment.