diff options
author | ignace nyamagana butera <nyamsprod@gmail.com> | 2014-02-22 13:00:15 +0100 |
---|---|---|
committer | ignace nyamagana butera <nyamsprod@gmail.com> | 2014-02-22 13:00:15 +0100 |
commit | 67136229aa994e4438db067ca7c6f6e24895f718 (patch) | |
tree | 87994e00c17c3e71a40731a7acbbd5125462d536 | |
parent | 963d063b2a8818cabbfcb4e6d3200cc8d1a70647 (diff) | |
download | csv-67136229aa994e4438db067ca7c6f6e24895f718.zip csv-67136229aa994e4438db067ca7c6f6e24895f718.tar.gz csv-67136229aa994e4438db067ca7c6f6e24895f718.tar.bz2 |
improved handling in the library4.2.1
-rw-r--r-- | README.md | 21 | ||||
-rw-r--r-- | src/AbstractCsv.php | 2 | ||||
-rw-r--r-- | src/Reader.php | 8 | ||||
-rw-r--r-- | test/ReaderTest.php | 6 |
4 files changed, 31 insertions, 6 deletions
@@ -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 6c1b258..4f1582e 100644 --- a/src/AbstractCsv.php +++ b/src/AbstractCsv.php @@ -166,7 +166,7 @@ class AbstractCsv implements JsonSerializable, IteratorAggregate */ 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 8d82c03..a8d6432 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 @@ class Reader extends AbstractCsv */ 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 25d0241..8353593 100644 --- a/test/ReaderTest.php +++ b/test/ReaderTest.php @@ -161,4 +161,10 @@ class ReaderTest extends PHPUnit_Framework_TestCase EOF; $this->assertSame($expected, $writer->toHTML()); } + + public function testGetWriter2() + { + $csv = (new Reader(__DIR__.'/foo.csv'))->getWriter('a+'); + $this->assertInstanceOf('\Bakame\Csv\Writer', $csv); + } } |