Skip to content

Commit

Permalink
Add appen methods to make it easier to add data to existing CSV files
Browse files Browse the repository at this point in the history
  • Loading branch information
nicksagona committed Oct 18, 2019
1 parent 4b5e219 commit 43a9350
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 24 deletions.
26 changes: 23 additions & 3 deletions src/Csv.php
Original file line number Diff line number Diff line change
Expand Up @@ -314,14 +314,34 @@ public function writeToFile($to)
* @param boolean $validate
* @return void
*/
public static function appendToFile($file, $data, array $options = [], $validate = true)
public static function appendDataToFile($file, $data, array $options = [], $validate = true)
{
if (!file_exists($file)) {
throw new Exception("Error: The file '" . $file . "' does not exist.");
}

foreach ($data as $row) {
self::appendRowToFile($file, $row, $options, $validate);
}
}

/**
* Append additional CSV row of data to a pre-existing file
*
* @param string $file
* @param array $row
* @param array $options
* @param boolean $validate
* @return void
*/
public static function appendRowToFile($file, array $row, array $options = [], $validate = true)
{
if (!file_exists($file)) {
throw new Exception("Error: The file '" . $file . "' does not exist.");
}

if ($validate) {
$keys = array_keys($data);
$keys = array_keys($row);
$headers = array_map(
function($value) { return str_replace('"', '', $value);}, explode(',', trim(fgets(fopen($file, 'r'))))
);
Expand All @@ -333,7 +353,7 @@ function($value) { return str_replace('"', '', $value);}, explode(',', trim(fget

$options = self::processOptions($options);
$csvRow = self::serializeRow(
(array)$data, [], $options['delimiter'], $options['enclosure'],
(array)$row, [], $options['delimiter'], $options['enclosure'],
$options['escape'], $options['newline'], $options['limit']
);

Expand Down
60 changes: 39 additions & 21 deletions tests/CsvTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,50 +203,68 @@ public function testIsValid()
public function testWriteToFile()
{
$data = [
'my_table' => [
[
'first_name' => 'Bob',
'last_name' => 'Smith'
],
[
'first_name' => 'Jane',
'last_name' => 'Smith'
]
[
'first_name' => 'Bob',
'last_name' => 'Smith'
],
[
'first_name' => 'Jane',
'last_name' => 'Smith'
]
];
$string = new Csv($data);
$string->writeToFile(__DIR__ . '/tmp/test.csv');
$this->assertFileExists(__DIR__ . '/tmp/test.csv');
}

public function testAppendDataToFileExistsException()
{
$this->expectException('Pop\Csv\Exception');
$data = [
[
'first_name' => 'John'
]
];

Csv::appendDataToFile(__DIR__ . '/tmp/bad.csv', $data);
}


public function testAppendToFileException()
public function testAppendDataToFileHeadersException()
{
$this->expectException('Pop\Csv\Exception');
$data = [
'my_table' => [
[
'first_name' => 'John'
]
[
'first_name' => 'John'
]
];

Csv::appendToFile(__DIR__ . '/tmp/test.csv', $data);
Csv::appendDataToFile(__DIR__ . '/tmp/test.csv', $data);
}

public function testAppendToFileExistsException()
{
$this->expectException('Pop\Csv\Exception');
$data = [
[
'first_name' => 'John',
'last_name' => 'Smith'
]
];

Csv::appendRowToFile(__DIR__ . '/tmp/bad.csv', $data);
}

public function testAppendToFile()
{
$data = [
'my_table' => [
[
'first_name' => 'John',
'last_name' => 'Smith'
]
[
'first_name' => 'John',
'last_name' => 'Smith'
]
];

Csv::appendToFile(__DIR__ . '/tmp/test.csv', $data);
Csv::appendDataToFile(__DIR__ . '/tmp/test.csv', $data);
$csv = Csv::loadFile(__DIR__ . '/tmp/test.csv');
$this->assertEquals(3, count($csv->getData()));

Expand Down

0 comments on commit 43a9350

Please sign in to comment.