Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
lpheller committed Nov 4, 2023
1 parent ed8b6bf commit 53b7cef
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/Csv.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function __construct(string $filePath)
);
}

public static function make(array $data)
public static function write(array $data)
{
return new CsvWriter($data);
}
Expand Down
32 changes: 24 additions & 8 deletions src/CsvWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,26 @@ public function __construct(array $data)
$this->data = $data;
}

public function toFile(string $filePath)
public function toFile(string $filePath, $append = false)
{
$handle = fopen($filePath, 'w');
$fileMode = $append ? 'a' : 'w';
$handle = fopen($filePath, $fileMode);
if ($handle === false) {
throw new \Exception("Could not open file: $filePath");
}

if ($append === true) {
$headers = fgetcsv(fopen($filePath, 'r'));
$this->headers = $headers;
}

if ($this->headers !== []) {
if ($this->headers !== [] && $append === false) {
fputcsv($handle, $this->headers);
}

foreach ($this->data as $row) {
if ($this->headers !== []) {
// if the keys of the row are strings, then we need to merge the headers with the row
// so that the keys are in the same order as the headers
if (array_keys($row) !== range(0, count($row) - 1)) {
$row = array_merge(array_flip($this->headers), $row);
}
$row = $this->normalizeRowWithHeaders($row);
}

fputcsv($handle, $row);
Expand All @@ -43,4 +48,15 @@ public function withHeaders(array $headers)

return $this;
}

protected function normalizeRowWithHeaders($row)
{
// if the keys of the row are strings, then we need to merge the headers with the row
// so that the keys are in the same order as the headers
if (array_keys($row) !== range(0, count($row) - 1)) {
return array_merge(array_flip($this->headers), $row);
}

return $row;
}
}
1 change: 1 addition & 0 deletions tests/Fixtures/data_write.csv
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
Col1,Col2,Col3
Foo,Bar,Baz
Foo2,Bar2,Baz2
57 changes: 48 additions & 9 deletions tests/Unit/CsvWriteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
'Baz',
]];

$file = Csv::make($data)
$file = Csv::write($data)
->toFile(__DIR__.'/../Fixtures/data_write.csv');

expect(file_get_contents(__DIR__.'/../Fixtures/data_write.csv'))
Expand All @@ -25,7 +25,7 @@
],
];

$file = Csv::make($data)
$file = Csv::write($data)
->withHeaders(['Col1', 'Col2', 'Col3'])
->toFile(__DIR__.'/../Fixtures/data_write.csv');

Expand All @@ -47,13 +47,13 @@
],
];

$file = Csv::make($data)
$file = Csv::write($data)
->withHeaders(['Col1', 'Col2', 'Col3'])
->toFile(__DIR__.'/../Fixtures/data_write.csv');

$foo = file_get_contents(__DIR__.'/../Fixtures/data_write.csv');

expect(file_get_contents(__DIR__.'/../Fixtures/data_write.csv'))
expect($foo)
->toBe(
'Col1,Col2,Col3'.PHP_EOL.
'Foo,Bar,Baz'.PHP_EOL
Expand All @@ -68,25 +68,64 @@
],
];

$file = Csv::make($data)
$file = Csv::write($data)
->withHeaders(['Col1', 'Col2', 'Col3'])
->toFile(__DIR__.'/../Fixtures/data_write.csv');

expect(file_get_contents(__DIR__.'/../Fixtures/data_write.csv'))
->toBe(
'Col1,Col2,Col3'.PHP_EOL.
'Foo,Bar,Baz'.PHP_EOL
);
$data = [[
'Foo',
'Bar',
'Baz',
],
]];
$file = Csv::write($data)
->toFile(__DIR__.'/../Fixtures/data_write.csv', true);

expect(file_get_contents(__DIR__.'/../Fixtures/data_write.csv'))
->toBe(
'Col1,Col2,Col3'.PHP_EOL.
'Foo,Bar,Baz'.PHP_EOL.
'Foo,Bar,Baz'.PHP_EOL
);
});

test('It appends assoc data to a file in the right order', function () {
$data = [
[
'Col2' => 'Bar',
'Col1' => 'Foo',
'Col3' => 'Baz',
],
];

$file = Csv::make($data)
$file = Csv::write($data)
->withHeaders(['Col1', 'Col2', 'Col3'])
->toFile(__DIR__.'/../Fixtures/data_write.csv', 'a');
->toFile(__DIR__.'/../Fixtures/data_write.csv');

expect(file_get_contents(__DIR__.'/../Fixtures/data_write.csv'))
->toBe(
'Col1,Col2,Col3'.PHP_EOL.
'Foo,Bar,Baz'.PHP_EOL.
'Foo,Bar,Baz'.PHP_EOL
);
$data = [
[
'Col2' => 'Bar2',
'Col1' => 'Foo2',
'Col3' => 'Baz2',
],
];
$file = Csv::write($data)
->withHeaders(['Col1', 'Col2', 'Col3'])
->toFile(__DIR__.'/../Fixtures/data_write.csv', true);

expect(file_get_contents(__DIR__.'/../Fixtures/data_write.csv'))
->toBe(
'Col1,Col2,Col3'.PHP_EOL.
'Foo,Bar,Baz'.PHP_EOL.
'Foo2,Bar2,Baz2'.PHP_EOL
);
});

0 comments on commit 53b7cef

Please sign in to comment.