Skip to content

Commit

Permalink
[TASK] Introduce DataSet CSV padding
Browse files Browse the repository at this point in the history
  • Loading branch information
ohader committed Feb 6, 2019
1 parent 8cd35bf commit 23a892f
Showing 1 changed file with 37 additions and 4 deletions.
41 changes: 37 additions & 4 deletions Classes/Core/Functional/Framework/DataHandling/DataSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,21 @@ public function getTableNames(): array
return array_keys($this->data);
}

/**
* @return int
*/
public function getMaximumPadding(): int
{
$maximums = array_map(
function (array $tableData) {
return count($tableData['fields'] ?? []);
},
array_values($this->data)
);
// adding additional index since field values are indented by one
return max($maximums) + 1;
}

/**
* @param string $tableName
* @return NULL|array
Expand Down Expand Up @@ -233,8 +248,9 @@ public function getElements(string $tableName)

/**
* @param string $fileName
* @param null|int $padding
*/
public function persist(string $fileName)
public function persist(string $fileName, int $padding = null)
{
$fileHandle = fopen($fileName, 'w');

Expand All @@ -246,15 +262,32 @@ public function persist(string $fileName)
$fields = $tableData['fields'];
array_unshift($fields, '');

fputcsv($fileHandle, [$tableName]);
fputcsv($fileHandle, $fields);
fputcsv($fileHandle, $this->pad([$tableName], $padding));
fputcsv($fileHandle, $this->pad($fields, $padding));

foreach ($tableData['elements'] as $element) {
array_unshift($element, '');
fputcsv($fileHandle, $element);
fputcsv($fileHandle, $this->pad($element, $padding));
}
}

fclose($fileHandle);
}

/**
* @param array $values
* @param null|int $padding
* @return array
*/
protected function pad(array $values, int $padding = null): array
{
if ($padding === null) {
return $values;
}

return array_merge(
$values,
array_fill(0, $padding - count($values), '')
);
}
}

0 comments on commit 23a892f

Please sign in to comment.