From 67136229aa994e4438db067ca7c6f6e24895f718 Mon Sep 17 00:00:00 2001 From: ignace nyamagana butera Date: Sat, 22 Feb 2014 13:00:15 +0100 Subject: [PATCH] improved handling in the library --- README.md | 21 +++++++++++++++++---- src/AbstractCsv.php | 2 +- src/Reader.php | 8 +++++++- test/ReaderTest.php | 6 ++++++ 4 files changed, 31 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 8b28e75c..19ee9090 100644 --- a/README.md +++ b/README.md @@ -82,17 +82,30 @@ use Bakame\Csv\Reader; use Bakame\Csv\Writer; $reader = new Reader('/path/to/your/csv/file.csv'); -$reader = new Reader(new SpliFileInfo('/path/to/your/csv/file.csv')); +$reader = new Reader(new SpliFileInfo('/path/to/your/csv/file.csv'), 'rt'); $reader = Reader::createFromString('john,doe,john.doe@example.com'); //or -$writer = new Writer('/path/to/your/csv/file.csv', 'w'); -$writer = new Writer(new SpliFileObject('/path/to/your/csv/file.csv'), 'a+'); +$writer = new Writer('/path/to/your/csv/file.csv', 'ab+'); +$writer = new Writer(new SpliFileObject('/path/to/your/csv/file.csv')); $writer = Writer::createFromString('john,doe,john.doe@example.com'); ``` -Both classes constructors take one optional parameter `$open_mode` representing the file open mode used by the PHP [fopen](http://php.net/manual/en/function.fopen.php) function. +Both classes constructors take one optional parameter `$open_mode` representing the file open mode used by the PHP [fopen](http://php.net/manual/en/function.fopen.php) function. + +The `$open_mode` parameter is taken into account if you instantiate your object with: + +* a `SplFileInfo` +* a string path + +The `$open_mode` parameter is **ignore** if you instantiate your object with: + +* a `SplFileObject` +* a `SplTempFileObject` + +When not explicitly set: + * The `Bakame\Csv\Writer` `$open_mode` default value is `w`. * The `Bakame\Csv\Reader` `$open_mode` default value is `r`. diff --git a/src/AbstractCsv.php b/src/AbstractCsv.php index 6c1b258b..4f1582e2 100644 --- a/src/AbstractCsv.php +++ b/src/AbstractCsv.php @@ -166,7 +166,7 @@ public static function isValidString($str) */ protected function fetchFile($path, $open_mode) { - if ($path instanceof SplTempFileObject) { + if ($path instanceof SplFileObject) { return $path; } $open_mode = strtolower($open_mode); diff --git a/src/Reader.php b/src/Reader.php index 8d82c031..a8d6432e 100644 --- a/src/Reader.php +++ b/src/Reader.php @@ -32,6 +32,8 @@ */ namespace Bakame\Csv; +use SplFileObject; +use SplTempFileObject; use InvalidArgumentException; use CallbackFilterIterator; use Bakame\Csv\Iterator\MapIterator; @@ -194,7 +196,11 @@ public function fetchCol($columnIndex = 0, callable $callable = null) */ public function getWriter($open_mode = 'w') { - $csv = new Writer($this->csv, $open_mode); + $obj = $this->csv; + if (! $obj instanceof SplTempFileObject) { + $obj = new SplFileObject($obj->getRealPath(), $open_mode); + } + $csv = new Writer($obj); $csv->setDelimiter($this->delimiter); $csv->setEnclosure($this->enclosure); $csv->setEscape($this->escape); diff --git a/test/ReaderTest.php b/test/ReaderTest.php index 25d02415..8353593d 100644 --- a/test/ReaderTest.php +++ b/test/ReaderTest.php @@ -161,4 +161,10 @@ public function testGetWriter() EOF; $this->assertSame($expected, $writer->toHTML()); } + + public function testGetWriter2() + { + $csv = (new Reader(__DIR__.'/foo.csv'))->getWriter('a+'); + $this->assertInstanceOf('\Bakame\Csv\Writer', $csv); + } }