Skip to content

Commit

Permalink
improved handling in the library
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Feb 22, 2014
1 parent 963d063 commit 6713622
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 6 deletions.
21 changes: 17 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,[email protected]');

//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,[email protected]');
```

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`.

Expand Down
2 changes: 1 addition & 1 deletion src/AbstractCsv.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 7 additions & 1 deletion src/Reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
*/
namespace Bakame\Csv;

use SplFileObject;
use SplTempFileObject;
use InvalidArgumentException;
use CallbackFilterIterator;
use Bakame\Csv\Iterator\MapIterator;
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 6 additions & 0 deletions test/ReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 6713622

Please sign in to comment.