diff options
author | Ignace Nyamagana Butera <nyamsprod@gmail.com> | 2015-01-28 14:37:48 +0100 |
---|---|---|
committer | Ignace Nyamagana Butera <nyamsprod@gmail.com> | 2015-01-28 14:37:48 +0100 |
commit | 55461b3c43df3a2e12576e27c067fcea5dc73f68 (patch) | |
tree | ec92b166f56b440afc0dc2c5d4a0432038584abb /src | |
parent | 068f97c3fb7d72bb7b1e814398f74d20a0bde11f (diff) | |
download | csv-55461b3c43df3a2e12576e27c067fcea5dc73f68.zip csv-55461b3c43df3a2e12576e27c067fcea5dc73f68.tar.gz csv-55461b3c43df3a2e12576e27c067fcea5dc73f68.tar.bz2 |
adding Writer::useValidation method
Diffstat (limited to 'src')
-rw-r--r-- | src/Writer.php | 70 |
1 files changed, 54 insertions, 16 deletions
diff --git a/src/Writer.php b/src/Writer.php index 70e063f..45ac00b 100644 --- a/src/Writer.php +++ b/src/Writer.php @@ -30,22 +30,22 @@ class Writer extends AbstractCsv /** * set null handling mode to throw exception */ - const NULL_AS_EXCEPTION = 1; + const NULL_AS_EXCEPTION = 'NULL_AS_EXCEPTION'; /** * set null handling mode to remove cell */ - const NULL_AS_SKIP_CELL = 2; + const NULL_AS_SKIP_CELL = 'NULL_AS_SKIP_CELL'; /** * set null handling mode to convert null into empty string */ - const NULL_AS_EMPTY = 3; + const NULL_AS_EMPTY = 'NULL_AS_EMPTY'; /** * disable null handling */ - const DISABLE_NULL_HANDLING = 4; + const DISABLE_NULL_HANDLING = 'DISABLE_NULL_HANDLING'; /** * the object current null handling mode @@ -88,6 +88,13 @@ class Writer extends AbstractCsv protected $newline = "\n"; /** + * should the class validate the input before insertion + * + * @var boolean + */ + protected $useValidation = true; + + /** * Tell the class how to handle null value * * @param int $value a Writer null behavior constant @@ -216,6 +223,20 @@ class Writer extends AbstractCsv } /** + * Tells wether the library should check or not the input + * + * @param bool $status + * + * @return static + */ + public function useValidation($status) + { + $this->useValidation = (bool) $status; + + return $this; + } + + /** * Add a new CSV row to the generated CSV * * @param string[]|string $data a string, an array or an object implementing to '__toString' method @@ -226,13 +247,9 @@ class Writer extends AbstractCsv */ public function insertOne($data) { - $data = $this->validateRow($data); - $data = $this->sanitizeColumnsContent($data); - if (! $this->isColumnsCountConsistent($data)) { - throw new RuntimeException( - 'You are trying to add '.count($data).' columns to a CSV - that requires '.$this->columns_count.' columns per row.' - ); + $data = $this->formatRow($data); + if ($this->useValidation) { + $data = $this->validateRow($data); } $csv = $this->getCsv(); $csv->fputcsv($data, $this->delimiter, $this->enclosure); @@ -246,20 +263,33 @@ class Writer extends AbstractCsv } /** - * Is the submitted row valid - * - * @param string[]|string $row + * format the submitted data to be an array * - * @throws \InvalidArgumentException If the given $row is not valid + * @param array|string $row the row data * * @return array */ - protected function validateRow($row) + protected function formatRow($row) { if (! is_array($row)) { return str_getcsv($row, $this->delimiter, $this->enclosure, $this->escape); } + return $row; + } + + /** + * Is the submitted row valid + * + * @param array $row + * + * @throws \InvalidArgumentException If the given $row is not valid + * @throws \RuntimeException If the given $row does not contain valid column count + * + * @return array + */ + protected function validateRow(array $row) + { if (self::DISABLE_NULL_HANDLING != $this->null_handling_mode) { array_walk($row, function ($value) { if (! $this->isConvertibleContent($value)) { @@ -268,6 +298,14 @@ class Writer extends AbstractCsv ); } }); + $row = $this->sanitizeColumnsContent($row); + } + + if (! $this->isColumnsCountConsistent($row)) { + throw new RuntimeException( + 'You are trying to add '.count($row).' columns to a CSV + that requires '.$this->columns_count.' columns per row.' + ); } return $row; |