diff --git a/src/Csv.php b/src/Csv.php index d2cd578..31414be 100644 --- a/src/Csv.php +++ b/src/Csv.php @@ -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')))) ); @@ -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'] ); diff --git a/tests/CsvTest.php b/tests/CsvTest.php index a0549d5..11affe8 100644 --- a/tests/CsvTest.php +++ b/tests/CsvTest.php @@ -203,15 +203,13 @@ 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); @@ -219,34 +217,54 @@ public function testWriteToFile() $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()));