Make ImportModel compatible with League/CSV v9
This commit is contained in:
parent
6a6331ba15
commit
3720af4c02
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue