diff options
author | Ignace Nyamagana Butera <nyamsprod@gmail.com> | 2013-12-03 13:23:51 +0100 |
---|---|---|
committer | Ignace Nyamagana Butera <nyamsprod@gmail.com> | 2013-12-03 13:23:51 +0100 |
commit | aa45ed84f9f7f549eebd41fb52e736035a527220 (patch) | |
tree | 3709f1e5fccb499a73ea01b9d980384b92203ca4 | |
parent | 0e1bfd6fc47e5413681835c31cb64a1babc997e2 (diff) | |
download | csv-aa45ed84f9f7f549eebd41fb52e736035a527220.zip csv-aa45ed84f9f7f549eebd41fb52e736035a527220.tar.gz csv-aa45ed84f9f7f549eebd41fb52e736035a527220.tar.bz2 |
Remove WrapperException
-rw-r--r-- | src/Bakame/Csv/Wrapper.php | 43 | ||||
-rw-r--r-- | src/Bakame/Csv/WrapperException.php | 13 | ||||
-rw-r--r-- | test/Bakame/Csv/WrapperTest.php | 18 |
3 files changed, 46 insertions, 28 deletions
diff --git a/src/Bakame/Csv/Wrapper.php b/src/Bakame/Csv/Wrapper.php index 8e0a9a0..22d4093 100644 --- a/src/Bakame/Csv/Wrapper.php +++ b/src/Bakame/Csv/Wrapper.php @@ -6,10 +6,18 @@ use SplFileInfo; use SplFileObject; use SplTempFileObject; use Traversable; +use InvalidArgumentException; +use RuntimeException; class Wrapper { /** + * File access type supported + * @var [type] + */ + private $mode_list = ['r+', 'w', 'w+', 'a', 'a+', 'c', 'c+']; + + /** * the field delimiter (one character only) * @var string */ @@ -32,11 +40,13 @@ class Wrapper * @param string $delimiter * * @return self + * + * @throws \InvalidArgumentException If $delimeter is not a single character */ public function setDelimiter($delimiter = ',') { if (1 != mb_strlen($delimiter)) { - throw new WrapperException('The delimiter must be a single character'); + throw new InvalidArgumentException('The delimiter must be a single character'); } $this->delimiter = $delimiter; @@ -48,11 +58,13 @@ class Wrapper * @param string $enclosure * * @return self + * + * @throws \InvalidArgumentException If $enclosure is not a single character */ public function setEnclosure($enclosure = '"') { if (1 != mb_strlen($enclosure)) { - throw new WrapperException('The enclosure must be a single character'); + throw new InvalidArgumentException('The enclosure must be a single character'); } $this->enclosure = $enclosure; @@ -64,11 +76,13 @@ class Wrapper * @param string $escape * * @return self + * + * @throws \InvalidArgumentException If $escape is not a single character */ public function setEscape($escape = "\\") { if (1 != mb_strlen($escape)) { - throw new WrapperException('The escape character must be a single character'); + throw new InvalidArgumentException('The escape character must be a single character'); } $this->escape = $escape; @@ -146,7 +160,7 @@ class Wrapper */ public function loadFile($str) { - $file = new SplFileObject($str, 'r+'); + $file = new SplFileObject($str, 'r'); $file->setFlags(SplFileObject::READ_CSV); $file->setCsvControl($this->delimiter, $this->enclosure, $this->escape); @@ -155,21 +169,30 @@ class Wrapper /** * Save the given data into a CSV - * @param array|Traversable $data the Data to be saved + * @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 * * @return \SplFileObject * - * @throws \Bakame\Csv\WrapperException If $data is not an array or a Traversable object - * @throws \RuntimeException If the $file can not be instantiate + * @throws \InvalidArgumentException If $data is not an array or a Traversable object + * @throws \RuntimeException If the $file can not be instantiate */ - public function save($data, $path) + 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 WrapperException('$data must be an Array or a Traversable object'); + throw new InvalidArgumentException('$data must be an Array or a Traversable object'); } - $file = ($path instanceof SplFileInfo) ? $path->openFile('w') : new SplFileObject($path, 'w'); + $file = ($path instanceof SplFileInfo) ? $path->openFile($mode) : new SplFileObject($path, $mode); $file->setCsvControl($this->delimiter, $this->enclosure, $this->escape); foreach ($data as $row) { if (is_string($row)) { diff --git a/src/Bakame/Csv/WrapperException.php b/src/Bakame/Csv/WrapperException.php deleted file mode 100644 index 3a279f2..0000000 --- a/src/Bakame/Csv/WrapperException.php +++ /dev/null @@ -1,13 +0,0 @@ -<?php - -namespace Bakame\Csv; - -use InvalidArgumentException; - -/** - * Base Exception for the Wrapper Class - */ -class WrapperException extends InvalidArgumentException -{ - -} diff --git a/test/Bakame/Csv/WrapperTest.php b/test/Bakame/Csv/WrapperTest.php index 0ac1cd2..c0272c1 100644 --- a/test/Bakame/Csv/WrapperTest.php +++ b/test/Bakame/Csv/WrapperTest.php @@ -16,7 +16,7 @@ class WrapperTest extends \PHPUnit_Framework_TestCase } /** - * @expectedException Bakame\Csv\WrapperException + * @expectedException InvalidArgumentException */ public function testDelimeter() { @@ -27,7 +27,7 @@ class WrapperTest extends \PHPUnit_Framework_TestCase } /** - * @expectedException Bakame\Csv\WrapperException + * @expectedException InvalidArgumentException */ public function testEscape() { @@ -38,7 +38,7 @@ class WrapperTest extends \PHPUnit_Framework_TestCase } /** - * @expectedException Bakame\Csv\WrapperException + * @expectedException InvalidArgumentException */ public function testEnclosure() { @@ -110,7 +110,7 @@ class WrapperTest extends \PHPUnit_Framework_TestCase } /** - * @expectedException Bakame\Csv\WrapperException + * @expectedException InvalidArgumentException */ public function testSaveExceptionBadData() { @@ -118,7 +118,15 @@ class WrapperTest extends \PHPUnit_Framework_TestCase } /** - * @expectedException \RuntimeException + * @expectedException InvalidArgumentException + */ + public function testSaveExceptionBadMode() + { + $this->wrapper->save('foo', 'php://temp', 'x'); + } + + /** + * @expectedException RuntimeException */ public function testSaveExceptionBadPath() { |