summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorIgnace Nyamagana Butera <nyamsprod@gmail.com>2013-12-03 12:58:37 +0100
committerIgnace Nyamagana Butera <nyamsprod@gmail.com>2013-12-03 12:58:37 +0100
commit0e1bfd6fc47e5413681835c31cb64a1babc997e2 (patch)
treef685951dad41452a98c21eef8afc4fa53e04db43
parentbe235e029cdb4fe6a385066c5f41e4d0464d271e (diff)
downloadcsv-0e1bfd6fc47e5413681835c31cb64a1babc997e2.zip
csv-0e1bfd6fc47e5413681835c31cb64a1babc997e2.tar.gz
csv-0e1bfd6fc47e5413681835c31cb64a1babc997e2.tar.bz2
Wrapper::save method simplification
-rw-r--r--src/Bakame/Csv/Wrapper.php27
-rw-r--r--test/Bakame/Csv/WrapperTest.php2
2 files changed, 14 insertions, 15 deletions
diff --git a/src/Bakame/Csv/Wrapper.php b/src/Bakame/Csv/Wrapper.php
index 1d42043..8e0a9a0 100644
--- a/src/Bakame/Csv/Wrapper.php
+++ b/src/Bakame/Csv/Wrapper.php
@@ -120,7 +120,7 @@ class Wrapper
}
/**
- * Parse a CSV string
+ * Load a CSV string
* @param string $str the csv content string
*
* @return \SplTempFileObject
@@ -137,10 +137,12 @@ class Wrapper
}
/**
- * Parse a CSV File
+ * 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)
{
@@ -153,25 +155,21 @@ class Wrapper
/**
* Save the given data into a CSV
- * @param mixed $data (Can be an array OR an Traversable object)
- * @param mixed $path (Can be a file path OR an SplFileInfo object)
+ * @param array|Traversable $data the Data to be saved
+ * @param string|SplFileInfo $path where to save the data
*
* @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
*/
public function save($data, $path)
{
if (! is_array($data) && ! $data instanceof Traversable) {
- throw new WrapperException('$data must be an Array or a Traversable instance');
- }
- $file = null;
- if ($path instanceof SplFileInfo) {
- $file = $path->openFile('w');
- } elseif (is_string($path)) {
- $file = new SplFileObject($path, 'w');
- }
- if (! $file instanceof SplFileObject) {
- throw new WrapperException('You must specify a SplFileInfo Object or a valid File Path');
+ throw new WrapperException('$data must be an Array or a Traversable object');
}
+
+ $file = ($path instanceof SplFileInfo) ? $path->openFile('w') : new SplFileObject($path, 'w');
$file->setCsvControl($this->delimiter, $this->enclosure, $this->escape);
foreach ($data as $row) {
if (is_string($row)) {
@@ -179,6 +177,7 @@ class Wrapper
}
$row = (array) $row;
array_walk($row, function (&$value) {
+ $value = (string) $value;
$value = trim($value);
});
$file->fputcsv($row);
diff --git a/test/Bakame/Csv/WrapperTest.php b/test/Bakame/Csv/WrapperTest.php
index b26bd4f..0ac1cd2 100644
--- a/test/Bakame/Csv/WrapperTest.php
+++ b/test/Bakame/Csv/WrapperTest.php
@@ -118,7 +118,7 @@ class WrapperTest extends \PHPUnit_Framework_TestCase
}
/**
- * @expectedException Bakame\Csv\WrapperException
+ * @expectedException \RuntimeException
*/
public function testSaveExceptionBadPath()
{