From eaf27992a94f849f63543e03e066b0ab86be262d Mon Sep 17 00:00:00 2001 From: Ignace Nyamagana Butera Date: Mon, 24 Mar 2014 09:00:54 +0100 Subject: [PATCH] Update NullHandling names --- src/Writer.php | 45 ++++++++++++++++++++++++++++++--------------- test/WriterTest.php | 14 +++++++------- 2 files changed, 37 insertions(+), 22 deletions(-) diff --git a/src/Writer.php b/src/Writer.php index 9a2765ff..3632ea54 100644 --- a/src/Writer.php +++ b/src/Writer.php @@ -35,6 +35,7 @@ use Traversable; use InvalidArgumentException; use OutOfBoundsException; +use RuntimeException; /** * A class to manage data insertion into a CSV @@ -45,14 +46,27 @@ */ class Writer extends AbstractCsv { - + /** + * set null handling mode to throw exception + */ const NULL_AS_EXCEPTION = 1; + /** + * set null handling mode to remove cell + */ const NULL_AS_SKIP_CELL = 2; + /** + * set null handling mode to convert null into empty string + */ const NULL_AS_EMPTY = 3; - private $null_handling = self::NULL_AS_EXCEPTION; + /** + * the object current null handling mode + * + * @var integer + */ + private $null_handling_mode = self::NULL_AS_EXCEPTION; /** * The constructor @@ -74,14 +88,12 @@ public function __construct($path, $open_mode = 'w') * * @throws OutOfBoundsException If the Integer is not valid */ - public function setNullHandling($value) + public function setNullHandlingMode($value) { if (!in_array($value, [self::NULL_AS_SKIP_CELL, self::NULL_AS_EXCEPTION, self::NULL_AS_EMPTY])) { - throw new OutOfBoundsException( - 'invalid value for null handling' - ); + throw new OutOfBoundsException('invalid value for null handling'); } - $this->null_handling = $value; + $this->null_handling_mode = $value; return $this; } @@ -91,9 +103,9 @@ public function setNullHandling($value) * * @return integer */ - public function getNullHandling() + public function getNullHandlingMode() { - return $this->null_handling; + return $this->null_handling_mode; } /** @@ -105,9 +117,9 @@ public function getNullHandling() */ private function formatRow(array $row) { - if (self::NULL_AS_EXCEPTION == $this->null_handling) { + if (self::NULL_AS_EXCEPTION == $this->null_handling_mode) { return $row; - } elseif (self::NULL_AS_EMPTY == $this->null_handling) { + } elseif (self::NULL_AS_EMPTY == $this->null_handling_mode) { foreach ($row as &$value) { if (is_null($value)) { $value = ''; @@ -143,16 +155,19 @@ public function insertOne($row) ); } $check = array_filter($row, function ($value) { - return (is_null($value) && self::NULL_AS_EXCEPTION != $this->null_handling) + return (is_null($value) && self::NULL_AS_EXCEPTION != $this->null_handling_mode) || self::isValidString($value); }); if (count($check) == count($row)) { - $row = $this->formatRow($row); - $this->csv->fputcsv($row, $this->delimiter, $this->enclosure); + $this->csv->fputcsv( + $this->formatRow($row), + $this->delimiter, + $this->enclosure + ); return $this; } - throw new InvalidArgumentException( + throw new RuntimeException( 'the provided data can not be transform into a single CSV data row' ); } diff --git a/test/WriterTest.php b/test/WriterTest.php index 2e87c91e..ecfcb907 100644 --- a/test/WriterTest.php +++ b/test/WriterTest.php @@ -1,6 +1,6 @@ csv->setNullHandling(Writer::NULL_AS_SKIP_CELL); - $this->assertSame(Writer::NULL_AS_SKIP_CELL, $this->csv->getNullHandling()); + $this->csv->setNullHandlingMode(Writer::NULL_AS_SKIP_CELL); + $this->assertSame(Writer::NULL_AS_SKIP_CELL, $this->csv->getNullHandlingMode()); - $this->csv->setNullHandling(23); + $this->csv->setNullHandlingMode(23); } public function testInsertNullToSkipCell() @@ -57,7 +57,7 @@ public function testInsertNullToSkipCell() 'john,doe,john.doe@example.com', ['john', null, 'john.doe@example.com'], ]; - $this->csv->setNullHandling(Writer::NULL_AS_SKIP_CELL); + $this->csv->setNullHandlingMode(Writer::NULL_AS_SKIP_CELL); foreach ($expected as $row) { $this->csv->insertOne($row); } @@ -74,7 +74,7 @@ public function testInsertNullToEmpty() 'john,doe,john.doe@example.com', ['john', null, 'john.doe@example.com'], ]; - $this->csv->setNullHandling(Writer::NULL_AS_EMPTY); + $this->csv->setNullHandlingMode(Writer::NULL_AS_EMPTY); foreach ($expected as $row) { $this->csv->insertOne($row); } @@ -93,7 +93,7 @@ public function testFailedInsertWithWrongData() } /** - * @expectedException InvalidArgumentException + * @expectedException RuntimeException */ public function testFailedInsertWithMultiDimensionArray() {