Skip to content

Commit

Permalink
Pass column names to data writer
Browse files Browse the repository at this point in the history
  • Loading branch information
SamMousa committed Jan 29, 2020
1 parent 71531d8 commit e5912a4
Showing 1 changed file with 6 additions and 12 deletions.
18 changes: 6 additions & 12 deletions src/CsvResponseFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,26 +79,20 @@ public function format($response): void
}


private function writeData(Response $response, $handle)
private function writeData(Response $response, $handle, array $columns)
{
foreach($response->data as $row) {

if ($row instanceof Arrayable) {
$row = $row->toArray();
}
$rowData = [];
if (isset($columns)) {
// Map columns.
foreach($columns as $column) {
if (array_key_exists($column, $row)) {
$rowData[] = $row[$column] ?? $this->nullValue;
} else {
$rowData[] = $this->missingValue;
}
}
} else {
foreach($row as $column => $value) {
$rowData[] = $value ?? $this->nullValue;
foreach($columns as $column) {
if (array_key_exists($column, $row)) {
$rowData[] = $row[$column] ?? $this->nullValue;
} else {
$rowData[] = $this->missingValue;
}
}
$this->put($handle, $rowData);
Expand Down

0 comments on commit e5912a4

Please sign in to comment.