summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/AbstractCsv.php2
-rw-r--r--src/Exception/InvalidRowException.php71
-rw-r--r--src/Exception/ValidationException.php26
-rw-r--r--src/Exporter/ColumnConsistencyValidator.php (renamed from src/Exporter/ColumnConsistency.php)3
-rw-r--r--src/Exporter/DataFormatterCollection.php (renamed from src/Exporter/Formatter.php)2
-rw-r--r--src/Exporter/DataValidatorCollection.php (renamed from src/Exporter/Validator.php)36
-rw-r--r--src/Exporter/ForbiddenNullValuesValidator.php (renamed from src/Exporter/NullValidator.php)6
-rw-r--r--src/Exporter/NullFormatter.php92
-rw-r--r--src/Exporter/SkipNullValuesFormatter.php37
-rw-r--r--src/Writer.php30
10 files changed, 125 insertions, 180 deletions
diff --git a/src/AbstractCsv.php b/src/AbstractCsv.php
index 6ec2b23..b23b74f 100644
--- a/src/AbstractCsv.php
+++ b/src/AbstractCsv.php
@@ -217,7 +217,7 @@ abstract class AbstractCsv implements JsonSerializable, IteratorAggregate
/**
* Return the CSV Iterator
*
- * @return \SplFileObject
+ * @return \Iterator
*/
public function getIterator()
{
diff --git a/src/Exception/InvalidRowException.php b/src/Exception/InvalidRowException.php
new file mode 100644
index 0000000..f2e30c0
--- /dev/null
+++ b/src/Exception/InvalidRowException.php
@@ -0,0 +1,71 @@
+<?php
+/**
+* This file is part of the League.csv library
+*
+* @license http://opensource.org/licenses/MIT
+* @link https://github.com/thephpleague/csv/
+* @version 7.0.0
+* @package League.csv
+*
+* For the full copyright and license information, please view the LICENSE
+* file that was distributed with this source code.
+*/
+namespace League\Csv\Exception;
+
+use InvalidArgumentException;
+
+/**
+ * Thrown when a data is not validated prior to insertion
+ *
+ * @package League.csv
+ * @since 7.0.0
+ *
+ */
+class InvalidRowException extends \InvalidArgumentException
+{
+ /**
+ * Validator which did not validated the data
+ * @var string
+ */
+ private $name;
+
+ /**
+ * Validator Data which caused the error
+ * @var array
+ */
+ private $data;
+
+ /**
+ * New Instance
+ *
+ * @param string $name validator name
+ * @param array $data invalid data
+ * @param string $message exception message
+ */
+ public function __construct($name, array $data = [], $message = "")
+ {
+ parent::__construct($message);
+ $this->name = $name;
+ $this->data = $data;
+ }
+
+ /**
+ * return the validator name
+ *
+ * @return string
+ */
+ public function getName()
+ {
+ return $this->name;
+ }
+
+ /**
+ * return the invalid data submitted
+ *
+ * @return array
+ */
+ public function getData()
+ {
+ return $this->data;
+ }
+}
diff --git a/src/Exception/ValidationException.php b/src/Exception/ValidationException.php
deleted file mode 100644
index 8a7e113..0000000
--- a/src/Exception/ValidationException.php
+++ /dev/null
@@ -1,26 +0,0 @@
-<?php
-/**
-* This file is part of the League.csv library
-*
-* @license http://opensource.org/licenses/MIT
-* @link https://github.com/thephpleague/csv/
-* @version 7.0.0
-* @package League.csv
-*
-* For the full copyright and license information, please view the LICENSE
-* file that was distributed with this source code.
-*/
-namespace League\Csv\Exception;
-
-use InvalidArgumentException;
-
-/**
- * Thrown when a data is not validated prior to insertion
- *
- * @package League.csv
- * @since 7.0.0
- *
- */
-class ValidationException extends InvalidArgumentException
-{
-}
diff --git a/src/Exporter/ColumnConsistency.php b/src/Exporter/ColumnConsistencyValidator.php
index 3af1f2f..2afa574 100644
--- a/src/Exporter/ColumnConsistency.php
+++ b/src/Exporter/ColumnConsistencyValidator.php
@@ -13,7 +13,6 @@
namespace League\Csv\Exporter;
use InvalidArgumentException;
-use RuntimeException;
/**
* A class to manage column consistency on data insertion into a CSV
@@ -22,7 +21,7 @@ use RuntimeException;
* @since 7.0.0
*
*/
-class ColumnConsistency
+class ColumnConsistencyValidator
{
/**
* The number of column per row
diff --git a/src/Exporter/Formatter.php b/src/Exporter/DataFormatterCollection.php
index f994951..495c5e3 100644
--- a/src/Exporter/Formatter.php
+++ b/src/Exporter/DataFormatterCollection.php
@@ -19,7 +19,7 @@ namespace League\Csv\Exporter;
* @since 7.0.0
*
*/
-trait Formatter
+trait DataFormatterCollection
{
/**
* Callables to format the row before insertion
diff --git a/src/Exporter/Validator.php b/src/Exporter/DataValidatorCollection.php
index 3fb72f7..3a1dae5 100644
--- a/src/Exporter/Validator.php
+++ b/src/Exporter/DataValidatorCollection.php
@@ -19,23 +19,9 @@ namespace League\Csv\Exporter;
* @since 7.0.0
*
*/
-trait Validator
+trait DataValidatorCollection
{
/**
- * The last failed validator
- *
- * @var string
- */
- protected $lastValidatorErrorName;
-
- /**
- * The last failed row
- *
- * @var array|null
- */
- protected $lastValidatorErrorData;
-
- /**
* Callables to validate the row before insertion
*
* @var callable[]
@@ -100,24 +86,4 @@ trait Validator
return $this;
}
-
- /**
- * Return the name of the last validation failed rule
- *
- * @return string
- */
- public function getLastValidatorErrorName()
- {
- return $this->lastValidatorErrorName;
- }
-
- /**
- * Returns the last failed data
- *
- * @return array|null
- */
- public function getLastValidatorErrorData()
- {
- return $this->lastValidatorErrorData;
- }
}
diff --git a/src/Exporter/NullValidator.php b/src/Exporter/ForbiddenNullValuesValidator.php
index 5fbb724..383680d 100644
--- a/src/Exporter/NullValidator.php
+++ b/src/Exporter/ForbiddenNullValuesValidator.php
@@ -19,16 +19,14 @@ namespace League\Csv\Exporter;
* @since 7.0.0
*
*/
-class NullValidator
+class ForbiddenNullValuesValidator
{
/**
* Is the submitted row valid
*
* @param array $row
*
- * @throws \InvalidArgumentException If the given $row is not valid
- *
- * @return array
+ * @return bool
*/
public function __invoke(array $row)
{
diff --git a/src/Exporter/NullFormatter.php b/src/Exporter/NullFormatter.php
deleted file mode 100644
index 40be048..0000000
--- a/src/Exporter/NullFormatter.php
+++ /dev/null
@@ -1,92 +0,0 @@
-<?php
-/**
-* This file is part of the League.csv library
-*
-* @license http://opensource.org/licenses/MIT
-* @link https://github.com/thephpleague/csv/
-* @version 7.0.0
-* @package League.csv
-*
-* For the full copyright and license information, please view the LICENSE
-* file that was distributed with this source code.
-*/
-namespace League\Csv\Exporter;
-
-use InvalidArgumentException;
-use OutOfBoundsException;
-
-/**
- * A class to handle null value formatting on data insertion into a CSV
- *
- * @package League.csv
- * @since 7.0.0
- *
- */
-class NullFormatter
-{
- /**
- * set null handling mode to remove cell
- */
- const NULL_AS_SKIP_CELL = 1;
-
- /**
- * set null handling mode to convert null into empty string
- */
- const NULL_AS_EMPTY = 2;
-
- /**
- * the object current null handling mode
- *
- * @var int
- */
- private $null_handling_mode = self::NULL_AS_EMPTY;
-
- /**
- * Tell the class how to handle null value
- *
- * @param int $value a Writer null behavior constant
- *
- * @throws \OutOfBoundsException If the Integer is not valid
- *
- * @return static
- */
- public function setMode($value)
- {
- if (! in_array($value, [self::NULL_AS_SKIP_CELL, self::NULL_AS_EMPTY])) {
- throw new OutOfBoundsException('invalid value for null handling');
- }
- $this->null_handling_mode = $value;
-
- return $this;
- }
-
- /**
- * null handling getter
- *
- * @return int
- */
- public function getMode()
- {
- return $this->null_handling_mode;
- }
-
- /**
- * Is the submitted row valid
- *
- * @param array $row
- *
- * @throws \InvalidArgumentException If the given $row is not valid
- *
- * @return array
- */
- public function __invoke(array $row)
- {
- if (self::NULL_AS_EMPTY == $this->null_handling_mode) {
- return str_replace(null, '', $row);
- }
-
- return array_filter($row, function ($value) {
- return ! is_null($value);
- });
- }
-}
diff --git a/src/Exporter/SkipNullValuesFormatter.php b/src/Exporter/SkipNullValuesFormatter.php
new file mode 100644
index 0000000..62987a4
--- /dev/null
+++ b/src/Exporter/SkipNullValuesFormatter.php
@@ -0,0 +1,37 @@
+<?php
+/**
+* This file is part of the League.csv library
+*
+* @license http://opensource.org/licenses/MIT
+* @link https://github.com/thephpleague/csv/
+* @version 7.0.0
+* @package League.csv
+*
+* For the full copyright and license information, please view the LICENSE
+* file that was distributed with this source code.
+*/
+namespace League\Csv\Exporter;
+
+/**
+ * A class to remove null value from data before insertion into a CSV
+ *
+ * @package League.csv
+ * @since 7.0.0
+ *
+ */
+class SkipNullValuesFormatter
+{
+ /**
+ * remove null value form the submitted array
+ *
+ * @param array $row
+ *
+ * @return array
+ */
+ public function __invoke(array $row)
+ {
+ return array_filter($row, function ($value) {
+ return ! is_null($value);
+ });
+ }
+}
diff --git a/src/Writer.php b/src/Writer.php
index b7f95e9..cd64d5c 100644
--- a/src/Writer.php
+++ b/src/Writer.php
@@ -13,7 +13,7 @@
namespace League\Csv;
use InvalidArgumentException;
-use League\Csv\Exception\ValidationException;
+use League\Csv\Exception\InvalidRowException;
use League\Csv\Exporter;
use Traversable;
@@ -39,14 +39,14 @@ class Writer extends AbstractCsv
protected $csv;
/**
- * Trait to format the row before insertion
+ * Data Formatters Collection trait
*/
- use Exporter\Formatter;
+ use Exporter\DataFormatterCollection;
/**
- * Trait to validate the row before insertion
+ * Data Validators Collection trait
*/
- use Exporter\Validator;
+ use Exporter\DataValidatorCollection;
/**
* Add multiple lines to the CSV your are generating
@@ -84,11 +84,7 @@ class Writer extends AbstractCsv
public function insertOne($row)
{
$row = $this->formatRow($row);
- if ($this->validators && ! $this->validateRow($row)) {
- throw new ValidationException(
- 'Row validation failed with the following validator: '. $this->lastValidatorErrorName
- );
- }
+ $this->validateRow($row);
$csv = $this->getCsv();
$csv->fputcsv($row, $this->delimiter, $this->enclosure);
if ("\n" !== $this->newline) {
@@ -124,21 +120,17 @@ class Writer extends AbstractCsv
*
* @param array $row
*
- * @return bool
+ * @throws \League\Csv\Exception\InvalidRowException If the validation failed
+ *
+ * @return void
*/
protected function validateRow(array $row)
{
- $this->lastValidatorErrorName = null;
- $this->lastValidatorErrorData = null;
foreach ($this->validators as $name => $validator) {
- if (! $validator($row)) {
- $this->lastValidatorErrorName = $name;
- $this->lastValidatorErrorData = $row;
- return false;
+ if (true !== $validator($row)) {
+ throw new InvalidRowException($name, $row, 'row validation failed');
}
}
-
- return true;
}
/**