Skip to content

Commit

Permalink
Update NullHandling names
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Mar 24, 2014
1 parent d3050ed commit eaf2799
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 22 deletions.
45 changes: 30 additions & 15 deletions src/Writer.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
use Traversable;
use InvalidArgumentException;
use OutOfBoundsException;
use RuntimeException;

/**
* A class to manage data insertion into a CSV
Expand All @@ -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
Expand All @@ -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;
}
Expand All @@ -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;
}

/**
Expand All @@ -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 = '';
Expand Down Expand Up @@ -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'
);
}
Expand Down
14 changes: 7 additions & 7 deletions test/WriterTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace League\Csv\Test;
namespace League\Csv\test;

use SplTempFileObject;
use ArrayIterator;
Expand Down Expand Up @@ -44,10 +44,10 @@ public function testInsert()
*/
public function testSetterGetterNullBehavior()
{
$this->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()
Expand All @@ -57,7 +57,7 @@ public function testInsertNullToSkipCell()
'john,doe,[email protected]',
['john', null, '[email protected]'],
];
$this->csv->setNullHandling(Writer::NULL_AS_SKIP_CELL);
$this->csv->setNullHandlingMode(Writer::NULL_AS_SKIP_CELL);
foreach ($expected as $row) {
$this->csv->insertOne($row);
}
Expand All @@ -74,7 +74,7 @@ public function testInsertNullToEmpty()
'john,doe,[email protected]',
['john', null, '[email protected]'],
];
$this->csv->setNullHandling(Writer::NULL_AS_EMPTY);
$this->csv->setNullHandlingMode(Writer::NULL_AS_EMPTY);
foreach ($expected as $row) {
$this->csv->insertOne($row);
}
Expand All @@ -93,7 +93,7 @@ public function testFailedInsertWithWrongData()
}

/**
* @expectedException InvalidArgumentException
* @expectedException RuntimeException
*/
public function testFailedInsertWithMultiDimensionArray()
{
Expand Down

0 comments on commit eaf2799

Please sign in to comment.