Support empty extra columns in CSV conversion (#12)

This commit is contained in:
Daniel Siepmann 2023-11-09 13:15:56 +01:00 committed by GitHub
parent 8a83c508a2
commit 77a7d13c70
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 30 additions and 3 deletions

View file

@ -1,5 +1,11 @@
# Changelog # Changelog
## v1.4.1 - 2023-11-09
### Added
- Support empty columns in CSV conversion.
## v1.4.0 - 2023-11-07 ## v1.4.0 - 2023-11-07
### Added ### Added

View file

@ -71,7 +71,7 @@ class Csv implements Converter
break; break;
} }
if (is_array($line) && count($line) === 1 && is_string($line[0])) { if (is_array($line) && count(array_filter($line)) === 1 && is_string($line[0])) {
// Line is a new table, introducing also new columns o next row // Line is a new table, introducing also new columns o next row
$tableName = $line[0]; $tableName = $line[0];
$columns = []; $columns = [];
@ -79,12 +79,12 @@ class Csv implements Converter
} }
if ($columns === [] && is_array($line)) { if ($columns === [] && is_array($line)) {
$columns = array_slice($line, 1); $columns = array_filter($line);
continue; continue;
} }
if (is_array($line)) { if (is_array($line)) {
$values = array_slice($line, 1); $values = array_slice($line, 1, count($columns));
$phpArray[$tableName][] = array_combine($columns, $values); $phpArray[$tableName][] = array_combine($columns, $values);
} }
} }

View file

@ -99,6 +99,10 @@ class CsvTest extends TestCase
'incomingCsvFile' => __DIR__ . '/Fixtures/Csv/RecordsInDifferentTablesIncoming.csv', 'incomingCsvFile' => __DIR__ . '/Fixtures/Csv/RecordsInDifferentTablesIncoming.csv',
'expectedResultFile' => __DIR__ . '/Fixtures/Csv/RecordsInDifferentTablesAssert.php', 'expectedResultFile' => __DIR__ . '/Fixtures/Csv/RecordsInDifferentTablesAssert.php',
], ],
'Extra columns' => [
'incomingCsvFile' => __DIR__ . '/Fixtures/Csv/ExtraColumnsIncoming.csv',
'expectedResultFile' => __DIR__ . '/Fixtures/Csv/ExtraColumnsAssert.php',
],
]; ];
} }
} }

View file

@ -0,0 +1,14 @@
<?php
return array (
'pages' =>
array (
0 =>
array (
'uid' => '1',
'pid' => '0',
'title' => 'Page with uid 1 below 0',
'deleted' => '0',
),
),
);

View file

@ -0,0 +1,3 @@
pages,,,,,,,,
,uid,pid,title,deleted,,,,
,1,0,Page with uid 1 below 0,0,,,,
1 pages
2 uid pid title deleted
3 1 0 Page with uid 1 below 0 0