Skip to content

Commit

Permalink
Writer internal rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Apr 17, 2014
1 parent ea2a8be commit 35ed8f9
Showing 1 changed file with 20 additions and 19 deletions.
39 changes: 20 additions & 19 deletions src/Writer.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,19 +118,8 @@ public function getNullHandlingMode()
*
* @return array
*/
private function processNullValues(array $row)
private function sanitizeColumnsContent(array $row)
{
$check = array_filter($row, function ($value) {
return (is_null($value) && self::NULL_AS_EXCEPTION != $this->null_handling_mode)
|| self::isValidString($value);
});

if (count($check) != count($row)) {
throw new InvalidArgumentException(
'the converted array must contain only data that can be converted into string'
);
}

if (self::NULL_AS_EXCEPTION == $this->null_handling_mode) {
return $row;
} elseif (self::NULL_AS_EMPTY == $this->null_handling_mode) {
Expand Down Expand Up @@ -235,11 +224,14 @@ private function validateRow($row)
);
}

$row = $this->processNullValues($row);
if (! $this->isColumnsCountConsistent($row)) {
$check = array_filter($row, function ($value) {
return (is_null($value) && self::NULL_AS_EXCEPTION != $this->null_handling_mode)
|| self::isValidString($value);
});

if (count($check) != count($row)) {
throw new InvalidArgumentException(
'You are trying to add '.count($row).' columns to a CSV
that requires '.$this->columns_count.' columns.'
'the converted array must contain only data that can be converted into string'
);
}

Expand All @@ -249,15 +241,24 @@ private function validateRow($row)
/**
* Add a new CSV row to the generated CSV
*
* @param mixed $row a string, an array or an object implementing to '__toString' method
* @param mixed $data a string, an array or an object implementing to '__toString' method
*
* @return self
*
* @throws \InvalidArgumentException If the given row is invalid
*/
public function insertOne($row)
public function insertOne($data)
{
$this->csv->fputcsv($this->validateRow($row), $this->delimiter, $this->enclosure);
$data = $this->validateRow($data);
$data = $this->sanitizeColumnsContent($data);
if (! $this->isColumnsCountConsistent($data)) {
throw new InvalidArgumentException(
'You are trying to add '.count($data).' columns to a CSV
that requires '.$this->columns_count.' columns per row.'
);
}

$this->csv->fputcsv($data, $this->delimiter, $this->enclosure);

return $this;
}
Expand Down

0 comments on commit 35ed8f9

Please sign in to comment.