Make ImportModel compatible with League/CSV v9

This commit is contained in:
Ben Thomson 2020-04-17 11:37:30 +08:00
parent 6a6331ba15
commit 3720af4c02
No known key found for this signature in database
GPG Key ID: E2B9C73B52D15AA0
1 changed files with 14 additions and 11 deletions

View File

@ -5,6 +5,7 @@ use Str;
use Lang;
use Model;
use League\Csv\Reader as CsvReader;
use League\Csv\Statement as CsvStatement;
/**
* Model used for importing data
@ -108,11 +109,6 @@ abstract class ImportModel extends Model
*/
$reader = CsvReader::createFromPath($filePath, 'r');
// Filter out empty rows
$reader->addFilter(function (array $row) {
return count($row) > 1 || reset($row) !== null;
});
if ($options['delimiter'] !== null) {
$reader->setDelimiter($options['delimiter']);
}
@ -125,15 +121,11 @@ abstract class ImportModel extends Model
$reader->setEscape($options['escape']);
}
if ($options['firstRowTitles']) {
$reader->setOffset(1);
}
if (
$options['encoding'] !== null &&
$reader->isActiveStreamFilter()
) {
$reader->appendStreamFilter(sprintf(
$reader->addStreamFilter(sprintf(
'%s%s:%s',
TranscodeFilter::FILTER_NAME,
strtolower($options['encoding']),
@ -141,8 +133,19 @@ abstract class ImportModel extends Model
));
}
// Create reader statement
$stmt = (new CsvStatement)
->where(function (array $row) {
// Filter out empty rows
return count($row) > 1 || reset($row) !== null;
});
if ($options['firstRowTitles']) {
$stmt = $stmt->offset(1);
}
$result = [];
$contents = $reader->fetch();
$contents = $stmt->process($reader);
foreach ($contents as $row) {
$result[] = $this->processImportRow($row, $matches);
}