Skip to content

Commit

Permalink
Support empty extra columns in CSV conversion (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielSiepmann authored Nov 9, 2023
1 parent 8a83c50 commit 77a7d13
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## v1.4.1 - 2023-11-09

### Added

- Support empty columns in CSV conversion.

## v1.4.0 - 2023-11-07

### Added
Expand Down
6 changes: 3 additions & 3 deletions Classes/Converter/Csv.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,20 @@ private function buildContent(SplFileObject $incomingFile): string
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
$tableName = $line[0];
$columns = [];
continue;
}

if ($columns === [] && is_array($line)) {
$columns = array_slice($line, 1);
$columns = array_filter($line);
continue;
}

if (is_array($line)) {
$values = array_slice($line, 1);
$values = array_slice($line, 1, count($columns));
$phpArray[$tableName][] = array_combine($columns, $values);
}
}
Expand Down
4 changes: 4 additions & 0 deletions Tests/Functional/Converter/CsvTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ public static function possibleCsvFiles(): array
'incomingCsvFile' => __DIR__ . '/Fixtures/Csv/RecordsInDifferentTablesIncoming.csv',
'expectedResultFile' => __DIR__ . '/Fixtures/Csv/RecordsInDifferentTablesAssert.php',
],
'Extra columns' => [
'incomingCsvFile' => __DIR__ . '/Fixtures/Csv/ExtraColumnsIncoming.csv',
'expectedResultFile' => __DIR__ . '/Fixtures/Csv/ExtraColumnsAssert.php',
],
];
}
}
14 changes: 14 additions & 0 deletions Tests/Functional/Converter/Fixtures/Csv/ExtraColumnsAssert.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

return array (
'pages' =>
array (
0 =>
array (
'uid' => '1',
'pid' => '0',
'title' => 'Page with uid 1 below 0',
'deleted' => '0',
),
),
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pages,,,,,,,,
,uid,pid,title,deleted,,,,
,1,0,Page with uid 1 below 0,0,,,,

0 comments on commit 77a7d13

Please sign in to comment.