diff options
author | Ignace Nyamagana Butera <nyamsprod@gmail.com> | 2015-02-16 16:52:59 +0100 |
---|---|---|
committer | Ignace Nyamagana Butera <nyamsprod@gmail.com> | 2015-02-16 16:52:59 +0100 |
commit | 269f03d0a7db2c9c66b4ff5fb463ca1aab1ae2a7 (patch) | |
tree | c0c35a4fccf95b5c4e6e08e59504c1c5cc267354 /src | |
parent | e46df385a0459822dfc8357fe05c02cd3977f74a (diff) | |
download | csv-269f03d0a7db2c9c66b4ff5fb463ca1aab1ae2a7.zip csv-269f03d0a7db2c9c66b4ff5fb463ca1aab1ae2a7.tar.gz csv-269f03d0a7db2c9c66b4ff5fb463ca1aab1ae2a7.tar.bz2 |
Writer optimization
Diffstat (limited to 'src')
-rw-r--r-- | src/Exporter/Formatter.php | 32 | ||||
-rw-r--r-- | src/Exporter/Validator.php | 46 | ||||
-rw-r--r-- | src/Writer.php | 54 |
3 files changed, 68 insertions, 64 deletions
diff --git a/src/Exporter/Formatter.php b/src/Exporter/Formatter.php index dd2b9f6..f994951 100644 --- a/src/Exporter/Formatter.php +++ b/src/Exporter/Formatter.php @@ -26,7 +26,7 @@ trait Formatter * * @var callable[] */ - protected $formatterRules = []; + protected $formatters = []; /** * add a formatter to the collection @@ -37,7 +37,7 @@ trait Formatter */ public function addFormatter(callable $callable) { - $this->formatterRules[] = $callable; + $this->formatters[] = $callable; return $this; } @@ -51,9 +51,9 @@ trait Formatter */ public function removeFormatter(callable $callable) { - $res = array_search($callable, $this->formatterRules, true); + $res = array_search($callable, $this->formatters, true); if (false !== $res) { - unset($this->formatterRules[$res]); + unset($this->formatters[$res]); } return $this; @@ -68,7 +68,7 @@ trait Formatter */ public function hasFormatter(callable $callable) { - return false !== array_search($callable, $this->formatterRules, true); + return false !== array_search($callable, $this->formatters, true); } /** @@ -78,28 +78,8 @@ trait Formatter */ public function clearFormatters() { - $this->formatterRules = []; + $this->formatters = []; return $this; } - - /** - * Format the given row - * - * @param array $row - * - * @return array - */ - protected function formatRow($row) - { - if (! is_array($row)) { - $row = str_getcsv($row, $this->delimiter, $this->enclosure, $this->escape); - } - - foreach ($this->formatterRules as $formatter) { - $row = (array) $formatter($row); - } - - return (array) $row; - } } diff --git a/src/Exporter/Validator.php b/src/Exporter/Validator.php index d9a3169..3fb72f7 100644 --- a/src/Exporter/Validator.php +++ b/src/Exporter/Validator.php @@ -26,21 +26,21 @@ trait Validator * * @var string */ - protected $lastValidator; + protected $lastValidatorErrorName; /** * The last failed row * * @var array|null */ - protected $lastRowData; + protected $lastValidatorErrorData; /** * Callables to validate the row before insertion * * @var callable[] */ - protected $validationRules = []; + protected $validators = []; /** * add a Validator to the collection @@ -52,7 +52,8 @@ trait Validator */ public function addValidator(callable $callable, $name) { - $this->validationRules[(string) $name] = $callable; + $name = trim($name); + $this->validators[$name] = $callable; return $this; } @@ -66,8 +67,9 @@ trait Validator */ public function removeValidator($name) { - if (array_key_exists($name, $this->validationRules)) { - unset($this->validationRules[$name]); + $name = trim($name); + if (array_key_exists($name, $this->validators)) { + unset($this->validators[$name]); } return $this; @@ -82,7 +84,9 @@ trait Validator */ public function hasValidator($name) { - return array_key_exists($name, $this->validationRules); + $name = trim($name); + + return array_key_exists($name, $this->validators); } /** @@ -92,41 +96,19 @@ trait Validator */ public function clearValidators() { - $this->validationRules = []; + $this->validators = []; return $this; } /** - * validate a row - * - * @param array $row - * - * @return bool - */ - protected function validateRow(array $row) - { - $this->lastValidator = null; - $this->lastRowData = null; - foreach ($this->validationRules as $name => $validator) { - if (! $validator($row)) { - $this->lastValidator = $name; - $this->lastRowData = $row; - return false; - } - } - - return true; - } - - /** * Return the name of the last validation failed rule * * @return string */ public function getLastValidatorErrorName() { - return $this->lastValidator; + return $this->lastValidatorErrorName; } /** @@ -136,6 +118,6 @@ trait Validator */ public function getLastValidatorErrorData() { - return $this->lastRowData; + return $this->lastValidatorErrorData; } } diff --git a/src/Writer.php b/src/Writer.php index 57747c9..f5ff007 100644 --- a/src/Writer.php +++ b/src/Writer.php @@ -38,14 +38,14 @@ class Writer extends AbstractCsv protected $csv; /** - * Trait to validate the row before insertion + * Trait to format the row before insertion */ - use Exporter\Validator; + use Exporter\Formatter; /** - * Trait to format the row before insertion + * Trait to validate the row before insertion */ - use Exporter\Formatter; + use Exporter\Validator; /** * Add multiple lines to the CSV your are generating @@ -83,9 +83,9 @@ class Writer extends AbstractCsv public function insertOne($row) { $row = $this->formatRow($row); - if (! $this->validateRow($row)) { + if ($this->validators && ! $this->validateRow($row)) { throw new InvalidArgumentException( - 'Row validation failed with the following validator: '. $this->getLastValidatorErrorName() + 'Row validation failed with the following validator: '. $this->lastValidatorErrorName ); } $csv = $this->getCsv(); @@ -99,6 +99,48 @@ class Writer extends AbstractCsv } /** + * Format the given row + * + * @param array|string $row + * + * @return array + */ + protected function formatRow($row) + { + if (! is_array($row)) { + $row = str_getcsv($row, $this->delimiter, $this->enclosure, $this->escape); + } + + foreach ($this->formatters as $formatter) { + $row = $formatter($row); + } + + return $row; + } + + /** + * validate a row + * + * @param array $row + * + * @return bool + */ + 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; + } + } + + return true; + } + + /** * set the csv container as a SplFileObject instance * insure we use the same object for insertion to * avoid loosing the cursor position |