From cce1ff1309874f716c4030dfc8f53322b1e6f45e Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Tue, 17 Mar 2015 17:54:50 +0100 Subject: [PATCH] init --- .gitignore | 3 + composer.json | 21 +++++ phpunit.xml | 18 ++++ src/Deblan/CsvValidator/Validator.php | 123 ++++++++++++++++++++++++++ 4 files changed, 165 insertions(+) create mode 100644 .gitignore create mode 100644 composer.json create mode 100644 phpunit.xml create mode 100644 src/Deblan/CsvValidator/Validator.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3e52175 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*.swp +tags +vendor diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..9f6fff3 --- /dev/null +++ b/composer.json @@ -0,0 +1,21 @@ +{ + "name": "deblan/csv-validator", + "description": "CSV validator library", + "license": "BSD-2-Clause", + "authors": [ + { + "name": "Simon Vieille", + "email": "simon@deblan.fr" + } + ], + "autoload": { + "psr-0": { + "": "src/" + } + }, + "minimum-stability": "stable", + "require": { + "symfony/validator": "2.*", + "deblan/csv": "dev-master" + } +} diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..eabb967 --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,18 @@ + + + + + tests/ + + + diff --git a/src/Deblan/CsvValidator/Validator.php b/src/Deblan/CsvValidator/Validator.php new file mode 100644 index 0000000..5954c03 --- /dev/null +++ b/src/Deblan/CsvValidator/Validator.php @@ -0,0 +1,123 @@ +parser = $parser; + $this->parser->parse(); + $this->validator = $validator; + } + + public function addFieldConstraint($key, Constraint $constraint) + { + if (!array_key_exists($key, $this->fieldsConstraints)) { + $this->fieldsConstraints[$key] = []; + } + + $this->fieldsConstraints[$key][] = $constraint; + + return $this; + } + + public function addDataContraint(Constraint $constraint) + { + $this->dataConstraints[] = $constraint; + + return $this; + } + + public function validate() + { + if ($this->hasValidate) { + return; + } + + foreach ($this->parser->getDatas() as $line => $data) { + foreach ($this->dataConstraints as $constraint) { + $violations = $this->validator->validateValue($data, $constraint); + + $this->mergeViolationsMessages($violations, $line); + } + + foreach ($this->fieldsConstraints as $key => $constraints) { + if (!isset($data[$key])) { + $this->mergeErrorMessage(sprintf('Field "%s" does not exist.', $key + 1), $line, $key); + } else { + foreach ($constraints as $constraint) { + $violations = $this->validator->validateValue($data[$key], $constraint); + + $this->mergeViolationsMessages($violations, $line, $key); + } + } + } + } + + $this->hasValidate = true; + } + + protected function mergeViolationsMessages(ConstraintViolationList $violations, $line, $key = null) + { + if (!array_key_exists($line, $this->errors)) { + $this->errors[$line] = []; + } + + if (is_int($key)) { + $key++; + } + + foreach ($violations as $violation) { + $message = sprintf('Line %d%s: %s', $line + 1, $key !== null ? ', field '.($key) : '', $violation->getMessage()); + + $this->errors[$line][] = $message; + } + } + + protected function mergeErrorMessage($message, $line, $key = null) + { + if (!array_key_exists($line, $this->errors)) { + $this->errors[$line] = []; + } + + if (is_int($key)) { + $key++; + } + + $message = sprintf('Line %d%s: %s', $line + 1, $key !== null ? ', field '.($key) : '', $message); + + $this->errors[$line][] = $message; + } + + public function isValid() + { + if (!$this->hasValidate) { + throw new \RuntimeException('You must validate before.'); + } + + return empty($this->errors); + } + + public function getErrors() + { + return $this->errors; + } +}