diff options
author | Ignace Nyamagana Butera <nyamsprod@gmail.com> | 2013-12-03 14:21:58 +0100 |
---|---|---|
committer | Ignace Nyamagana Butera <nyamsprod@gmail.com> | 2013-12-03 14:21:58 +0100 |
commit | 22e4d5821198ad7eb7f34a8f4a643cf7fed3222d (patch) | |
tree | e9bb150b0a7f694f7302de20b33c26c828dc671e | |
parent | aa45ed84f9f7f549eebd41fb52e736035a527220 (diff) | |
download | csv-22e4d5821198ad7eb7f34a8f4a643cf7fed3222d.zip csv-22e4d5821198ad7eb7f34a8f4a643cf7fed3222d.tar.gz csv-22e4d5821198ad7eb7f34a8f4a643cf7fed3222d.tar.bz2 |
adding file mode access
-rw-r--r-- | src/Bakame/Csv/Wrapper.php | 73 | ||||
-rw-r--r-- | test/Bakame/Csv/WrapperTest.php | 20 |
2 files changed, 75 insertions, 18 deletions
diff --git a/src/Bakame/Csv/Wrapper.php b/src/Bakame/Csv/Wrapper.php index 22d4093..3578dca 100644 --- a/src/Bakame/Csv/Wrapper.php +++ b/src/Bakame/Csv/Wrapper.php @@ -12,19 +12,22 @@ use RuntimeException; class Wrapper { /** - * File access type supported + * file access type supported + * * @var [type] */ - private $mode_list = ['r+', 'w', 'w+', 'a', 'a+', 'c', 'c+']; + private $mode_list = ['r', 'r+', 'w', 'w+', 'x', 'x+', 'a', 'a+', 'c', 'c+']; /** * the field delimiter (one character only) + * * @var string */ private $delimiter = ','; /** * the field enclosure character (one character only) + * * @var string */ private $enclosure = '"'; @@ -37,6 +40,7 @@ class Wrapper /** * set the field delimeter + * * @param string $delimiter * * @return self @@ -55,6 +59,7 @@ class Wrapper /** * set the field enclosure + * * @param string $enclosure * * @return self @@ -73,6 +78,7 @@ class Wrapper /** * set the field escape character + * * @param string $escape * * @return self @@ -135,6 +141,7 @@ class Wrapper /** * Load a CSV string + * * @param string $str the csv content string * * @return \SplTempFileObject @@ -152,15 +159,42 @@ class Wrapper /** * Load a CSV File + * * @param string $str the file path * * @return \SplFileObject * * @throws \RuntimeException if the $file can not be instantiate */ - public function loadFile($str) + public function loadFile($path, $mode = 'r') + { + $this->filterMode($mode, ['w', 'a', 'x', 'c']); + + return $this->create($path, $mode); + } + + /** + * Return a new \SplFileObject + * + * @param string|SplFileInfo $path where to save the data + * @param string $mode specifies the type of access you require to the file + * + * @return \SplFileObject + * + * @throws \InvalidArgumentException If the $mode is invalid + * @throws \InvalidArgumentException If the $file is not set + */ + public function create($path, $mode) { - $file = new SplFileObject($str, 'r'); + $this->filterMode($mode); + if ($path instanceof SplFileInfo) { + $file = $path->openFile($mode); + } elseif (is_string($path)) { + $file = new SplFileObject($path, $mode); + } + if (! isset($file)) { + throw new InvalidArgumentException('$path must be a SplFileInfo object or a valid file path.'); + } $file->setFlags(SplFileObject::READ_CSV); $file->setCsvControl($this->delimiter, $this->enclosure, $this->escape); @@ -168,7 +202,23 @@ class Wrapper } /** + * validate the type of access you require for a given file + * + * @param string $mode specifies the type of access you require to the file + * @param array $options non valid type of access + */ + private function filterMode(&$mode, array $options = []) + { + $mode = strtolower($mode); + $mode_list = array_diff($this->mode_list, $options); + if (! in_array($mode, $mode_list)) { + throw new InvalidArgumentException('$mode can not be equal to "'.$mode.'"'); + } + } + + /** * Save the given data into a CSV + * * @param array|Traversable $data the data to be saved * @param string|SplFileInfo $path where to save the data * @param string $mode specifies the type of access you require to the file @@ -176,24 +226,16 @@ class Wrapper * @return \SplFileObject * * @throws \InvalidArgumentException If $data is not an array or a Traversable object - * @throws \RuntimeException If the $file can not be instantiate + * @throws \InvalidArgumentException If the $mode is invalid */ public function save($data, $path, $mode = 'w') { - $mode = strtolower($mode); - if (! in_array($mode, $this->mode_list)) { - throw new InvalidArgumentException( - 'Invalid $mode, available mode are : "'.implode('", "', $this->mode_list).'". - Please refer to PHP built-in fopen function documentation for more information.' - ); - } - if (! is_array($data) && ! $data instanceof Traversable) { throw new InvalidArgumentException('$data must be an Array or a Traversable object'); } - $file = ($path instanceof SplFileInfo) ? $path->openFile($mode) : new SplFileObject($path, $mode); - $file->setCsvControl($this->delimiter, $this->enclosure, $this->escape); + $this->filterMode($mode, ['r']); + $file = $this->create($path, $mode); foreach ($data as $row) { if (is_string($row)) { $row = explode($this->delimiter, $row); @@ -205,7 +247,6 @@ class Wrapper }); $file->fputcsv($row); } - $file->setFlags(SplFileObject::READ_CSV); return $file; } diff --git a/test/Bakame/Csv/WrapperTest.php b/test/Bakame/Csv/WrapperTest.php index c0272c1..c6fbf7b 100644 --- a/test/Bakame/Csv/WrapperTest.php +++ b/test/Bakame/Csv/WrapperTest.php @@ -59,6 +59,14 @@ class WrapperTest extends \PHPUnit_Framework_TestCase } } + /** + * @expectedException InvalidArgumentException + */ + public function testCreateCsv() + { + $this->wrapper->create(__DIR__.'/foo.csv', 'z'); + } + public function testloadFile() { $expected = ['foo', 'bar', 'baz']; @@ -75,6 +83,14 @@ class WrapperTest extends \PHPUnit_Framework_TestCase } } + /** + * @expectedException InvalidArgumentException + */ + public function testloadFileException() + { + $this->wrapper->loadFile(__DIR__.'/foo.csv', 'w'); + } + public function testSaveArray() { $arr = [ @@ -122,11 +138,11 @@ class WrapperTest extends \PHPUnit_Framework_TestCase */ public function testSaveExceptionBadMode() { - $this->wrapper->save('foo', 'php://temp', 'x'); + $this->wrapper->save(['foo'], 'php://temp', 'r'); } /** - * @expectedException RuntimeException + * @expectedException InvalidArgumentException */ public function testSaveExceptionBadPath() { |