diff --git a/.gitignore b/.gitignore index 3e52175..22d0d82 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1 @@ -*.swp -tags vendor diff --git a/LICENSE b/LICENSE index b57a6c2..80445fb 100644 --- a/LICENSE +++ b/LICENSE @@ -20,4 +20,5 @@ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. \ No newline at end of file +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + diff --git a/README.md b/README.md index fa1e271..a83c640 100644 --- a/README.md +++ b/README.md @@ -1,16 +1,22 @@ csv-validator ============= +[![](https://phpci.gitnet.fr/build-status/image/2?branch=master&label=PHPCensor&style=flat-square)](https://phpci.gitnet.fr/build-status/view/2) + CSV validator library That uses the constraints of Symfony framework: [http://symfony.com/doc/current/reference/constraints.html](http://symfony.com/doc/current/reference/constraints.html). +* [Installation](#installation) +* [Example](#example) +* [Contributors](#contributors) + Installation ------------ You need [composer](https://getcomposer.org/): - composer require deblan/csv-validator dev-master + composer require deblan/csv-validator Example @@ -39,7 +45,7 @@ $validator->addFieldConstraint(0, new Email()); $validator->addFieldConstraint(1, new Date()); // Validate the legend -$validator->setExpectedLegend(array('foo', 'bar', 'bim')); +$validator->setExpectedHeaders(['foo', 'bar', 'bim']); // An line must contain 3 columns $validator->addDataConstraint(new Callback(function($data, ExecutionContextInterface $context) { @@ -49,17 +55,17 @@ $validator->addDataConstraint(new Callback(function($data, ExecutionContextInter })); // Initialisation of the parser -$parser = new CsvParser(__DIR__.'/tests/fixtures/example.csv'); -$parser->setHasLegend(true); +$parser = new CsvParser(); +$parser->setHasHeaders(true); -$validator->validate($parser); +$validator->validate($parser->parseFile(__DIR__.'/tests/fixtures/example.csv')); if ($validator->isValid() === false) { foreach ($validator->getErrors() as $error) { - $line = $error->getLine(); + $line = $error->getLine(); $column = $error->getColumn(); $message = $error->getViolation()->getMessage(); - + echo <<
  • Line: $line
  • @@ -73,3 +79,42 @@ EOF; } } ``` + +Run `example.php` and see results: + +``` + + + + +``` + +Contributors +------------ + +* Simon Vieille +* Gautier Deruette diff --git a/composer.json b/composer.json index c9aa77f..4e561ff 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,7 @@ "minimum-stability": "dev", "require": { "php": ">=5.6.0", - "symfony/validator": "2.*", - "deblan/csv": "v1.1" + "symfony/validator": "3.*", + "deblan/csv": "v2.0.*" } } diff --git a/example.php b/example.php index 48250f6..e764726 100644 --- a/example.php +++ b/example.php @@ -2,7 +2,6 @@ use Deblan\Csv\CsvParser; use Deblan\CsvValidator\Validator; -use Symfony\Component\Validator\Validation; use Symfony\Component\Validator\Constraints\Email; use Symfony\Component\Validator\Constraints\Date; use Symfony\Component\Validator\Constraints\Callback; @@ -20,27 +19,27 @@ $validator->addFieldConstraint(0, new Email()); $validator->addFieldConstraint(1, new Date()); // Validate the legend -$validator->setExpectedLegend(array('foo', 'bar', 'bim')); +$validator->setExpectedHeaders(['foo', 'bar', 'bim']); // An line must contain 3 columns -$validator->addDataConstraint(new Callback(function($data, ExecutionContextInterface $context) { +$validator->addDataConstraint(new Callback(function ($data, ExecutionContextInterface $context) { if (count($data) !== 6) { // 6 because of the legend (3 fields * 2) $context->addViolation('The line must contain 3 columns'); } })); // Initialisation of the parser -$parser = new CsvParser(__DIR__.'/tests/fixtures/example.csv'); -$parser->setHasLegend(true); +$parser = new CsvParser(); +$parser->setHasHeaders(true); -$validator->validate($parser); +$validator->validate($parser->parseFile(__DIR__.'/tests/fixtures/example.csv')); if ($validator->isValid() === false) { foreach ($validator->getErrors() as $error) { - $line = $error->getLine(); + $line = $error->getLine(); $column = $error->getColumn(); $message = $error->getViolation()->getMessage(); - + echo <<
  • Line: $line
  • diff --git a/phpci.yml b/phpci.yml new file mode 100644 index 0000000..01be34a --- /dev/null +++ b/phpci.yml @@ -0,0 +1,14 @@ +build_settings: + verbose: false + prefer_symlink: false + +setup: + composer: + action: "install" + +test: + php_unit: + directory: "tests/" + args: "--configuration 'phpunit.xml'" + +complete: diff --git a/src/Deblan/CsvValidator/Validator.php b/src/Deblan/CsvValidator/Validator.php index 98702cc..6374910 100644 --- a/src/Deblan/CsvValidator/Validator.php +++ b/src/Deblan/CsvValidator/Validator.php @@ -10,7 +10,8 @@ use Symfony\Component\Validator\ConstraintViolation; use Symfony\Component\Validator\Validation; /** - * Class Validator + * Class Validator. + * * @author Simon Vieille */ class Validator @@ -36,7 +37,7 @@ class Validator protected $dataConstraints = []; /** - * @var boolean + * @var bool */ protected $hasValidate = false; @@ -44,32 +45,32 @@ class Validator * @var array */ protected $errors = []; - + /** * @var array */ - protected $expectedLegend = []; + protected $expectedHeaders = []; /** - * Constructor + * Constructor. * - * @param CsvParser $parser * @param RecursiveValidator $validator */ public function __construct(RecursiveValidator $validator = null) { if ($validator === null) { $validator = Validation::createValidator(); - } - + } + $this->validator = $validator; } /** - * Append a constraint to a specific column + * Append a constraint to a specific column. * - * @param $key The column number + * @param int $key The column number * @param Constraint $constraint The constraint + * * @return Validator */ public function addFieldConstraint($key, Constraint $constraint) @@ -84,10 +85,10 @@ class Validator } /** - * Append a constraint to a specific line + * Append a constraint to a specific line. * - * @param $key The column number * @param Constraint $constraint The constraint + * * @return Validator */ public function addDataConstraint(Constraint $constraint) @@ -98,62 +99,62 @@ class Validator } /** - * Set the expected legend + * Set the expected legend. * * @param array $legend Expected legend + * * @return Validator */ - public function setExpectedLegend(array $legend) + public function setExpectedHeaders(array $legend) { - $this->expectedLegend = $legend; + $this->expectedHeaders = $legend; return $this; } /** - * Run the validation + * Run the validation. + * + * @param CsvParser $parser */ public function validate(CsvParser $parser) { if ($this->parser !== $parser) { $this->parser = $parser; - $this->parser->parse(); $this->errors = []; - } - elseif ($this->hasValidate) { + } elseif ($this->hasValidate) { return; } - - $this->validateLegend(); + + $this->validateHeaders(); $this->validateDatas(); $this->validateFields(); - $this->hasValidate = true; } - + /** - * Validates the legend + * Validates the legend. */ - protected function validateLegend() + protected function validateHeaders() { - if (!$this->parser->getHasLegend()) { + if (!$this->parser->getHasHeaders()) { return; } - if (empty($this->expectedLegend)) { + if (empty($this->expectedHeaders)) { return; } - - if ($this->parser->getLegend() !== $this->expectedLegend) { + + if ($this->parser->getHeaders() !== $this->expectedHeaders) { $this->mergeErrorMessage('Invalid legend.', 1); } } /** - * Validates datas + * Validates datas. */ - protected function validateDatas() + protected function validateDatas() { if (empty($this->dataConstraints)) { return; @@ -161,7 +162,7 @@ class Validator foreach ($this->parser->getDatas() as $line => $data) { foreach ($this->dataConstraints as $constraint) { - $violations = $this->validator->validateValue($data, $constraint); + $violations = $this->validator->validate($data, $constraint); $this->mergeViolationsMessages($violations, $this->getTrueLine($line)); } @@ -169,30 +170,30 @@ class Validator } /** - * Validates fields + * Validates fields. */ - protected function validateFields() + protected function validateFields() { if (empty($this->fieldConstraints)) { return; } - + foreach ($this->parser->getDatas() as $line => $data) { foreach ($this->fieldConstraints as $key => $constraints) { if (!isset($data[$key])) { $column = $this->getTrueColunm($key); $this->mergeErrorMessage( - sprintf('Field "%s" does not exist.', $column), - $this->getTrueLine($line), + sprintf('Field "%s" does not exist.', $column), + $this->getTrueLine($line), $column ); } else { foreach ($constraints as $constraint) { - $violations = $this->validator->validateValue($data[$key], $constraint); + $violations = $this->validator->validate($data[$key], $constraint); $this->mergeViolationsMessages( - $violations, - $this->getTrueLine($line), + $violations, + $this->getTrueLine($line), $this->getTrueColunm($key) ); } @@ -202,11 +203,11 @@ class Validator } /** - * Add violations + * Add violations. * * @param ConstraintViolationList $violations - * @param integer $line The line of the violations - * @param integer|null $key The column of the violations + * @param int $line The line of the violations + * @param int|null $key The column of the violations */ protected function mergeViolationsMessages(ConstraintViolationList $violations, $line, $key = null) { @@ -220,11 +221,11 @@ class Validator } /** - * Create and append a violation from a string error + * Create and append a violation from a string error. * - * @param string $message The error message - * @param integer $line The line of the violations - * @param integer|null $key The column of the violations + * @param string $message The error message + * @param int $line The line of the violations + * @param int|null $key The column of the violations */ protected function mergeErrorMessage($message, $line, $key = null) { @@ -233,9 +234,9 @@ class Validator } /** - * Returns the validation status + * Returns the validation status. * - * @return boolean + * @return bool * @throw RuntimeException No validation yet */ public function isValid() @@ -248,7 +249,7 @@ class Validator } /** - * Returns the errors + * Returns the errors. * * @return array */ @@ -258,51 +259,55 @@ class Validator } /** - * Generate a ConstraintViolation + * Generate a ConstraintViolation. * * @param string $message The error message + * * @return ConstraintViolation */ - protected function generateConstraintViolation($message) + protected function generateConstraintViolation($message) { return new ConstraintViolation($message, $message, [], null, '', null); } - + /** - * Generate a Violation + * Generate a Violation. + * + * @param string $message The error message + * @param int $line The line of the violations + * @param int|null $key The column of the violations * - * @param string $message The error message - * @param integer $line The line of the violations - * @param integer|null $key The column of the violations * @return Violation */ - protected function generateViolation($line, $key, ConstraintViolation $violation) + protected function generateViolation($line, $key, ConstraintViolation $violation) { return new Violation($line, $key, $violation); } /** - * Get the true line number of an error + * Get the true line number of an error. * - * @param integer $line - * @return integer + * @param int $line + * + * @return int */ - protected function getTrueLine($line) + protected function getTrueLine($line) { - if ($this->parser->getHasLegend()) { - $line++; + if ($this->parser->getHasHeaders()) { + ++$line; } return ++$line; } - + /** - * Get the true culumn number of an error + * Get the true culumn number of an error. * - * @param integer $key - * @return integer + * @param int $key + * + * @return int */ - protected function getTrueColunm($key) + protected function getTrueColunm($key) { return ++$key; } diff --git a/src/Deblan/CsvValidator/Violation.php b/src/Deblan/CsvValidator/Violation.php index 1989c8a..a6a6e78 100644 --- a/src/Deblan/CsvValidator/Violation.php +++ b/src/Deblan/CsvValidator/Violation.php @@ -5,31 +5,32 @@ namespace Deblan\CsvValidator; use Symfony\Component\Validator\ConstraintViolation; /** - * Class Violation + * Class Violation. + * * @author Simon Vieille */ class Violation { /** - * @var integer + * @var int */ protected $line; /** - * @var integer + * @var int */ protected $column; - + /** * @var ConstraintViolation */ protected $violation; /** - * Constructor + * Constructor. * - * @param integer $line The line of the violation - * @param integer $column The column of the violation + * @param int $line The line of the violation + * @param int $column The column of the violation * @param ConstraintViolation $violation The violation */ public function __construct($line, $column, ConstraintViolation $violation) @@ -41,6 +42,7 @@ class Violation /** * @param int $line + * * @return Violation */ public function setLine($line) @@ -60,6 +62,7 @@ class Violation /** * @param int $column + * * @return Violation */ public function setColumn($column) @@ -83,6 +86,7 @@ class Violation /** * @param ConstraintViolation $violation + * * @return Violation */ public function setViolation(ConstraintViolation $violation) diff --git a/tests/ExampleTest.php b/tests/ExampleTest.php new file mode 100644 index 0000000..4d0705f --- /dev/null +++ b/tests/ExampleTest.php @@ -0,0 +1,44 @@ + + */ +class ExampleTest extends \PHPUnit_Framework_TestCase +{ + public function testExemple() + { + $content = shell_exec('php -f '.__DIR__.'/../example.php'); + + $this->assertEquals('
      +
    • Line: 1
    • +
    • Column:
    • +
    • +

      Invalid legend.

      +
    • +
    +
      +
    • Line: 4
    • +
    • Column:
    • +
    • +

      The line must contain 3 columns

      +
    • +
    +
      +
    • Line: 2
    • +
    • Column: 1
    • +
    • +

      This value is not a valid email address.

      +
    • +
    +
      +
    • Line: 3
    • +
    • Column: 2
    • +
    • +

      This value is not a valid date.

      +
    • +
    +', $content); + } +} diff --git a/tests/ValidatorTest.php b/tests/ValidatorTest.php index 50f28df..f3d75ac 100644 --- a/tests/ValidatorTest.php +++ b/tests/ValidatorTest.php @@ -15,72 +15,90 @@ class ValidatorTest extends \PHPUnit_Framework_TestCase $this->setExpectedException('\RuntimeException'); $validator->isValid(); } - + + public function testExpectedHeaders() + { + $parser = $this->generateParser('example.csv'); + $parser->setHasHeaders(true); + + $validator = $this->generateValidator(); + $validator->setExpectedHeaders(['foo', 'bar', 'boo']); + $validator->validate($parser->parseFile(__DIR__.'/fixtures/example.csv')); + $this->assertEquals(true, $validator->isValid()); + $this->assertEquals(0, count($validator->getErrors())); + + $validator = $this->generateValidator(); + $validator->setExpectedHeaders(['bad', 'legend']); + $validator->validate($parser->parseFile(__DIR__.'/fixtures/example.csv')); + $this->assertEquals(false, $validator->isValid()); + $this->assertEquals(1, count($validator->getErrors())); + } + public function testNoConstraint() { $parser = $this->generateParser('example.csv'); - $validator = $this->generateValidator($parser); - $validator->validate(); + $validator = $this->generateValidator(); + $validator->validate($parser->parseFile(__DIR__.'/fixtures/example.csv')); $this->assertEquals(true, $validator->isValid()); } - + public function testFieldContraintsOk() { $parser = $this->generateParser('example.csv'); - $validator = $this->generateValidator($parser); + $validator = $this->generateValidator(); $validator->addFieldConstraint(0, new NotBlank()); - $validator->validate(); + $validator->validate($parser->parseFile(__DIR__.'/fixtures/example.csv')); $this->assertEquals(true, $validator->isValid()); $this->assertEquals(0, count($validator->getErrors())); - + $parser = $this->generateParser('example.csv'); - $validator = $this->generateValidator($parser); + $validator = $this->generateValidator(); $validator->addFieldConstraint(1, new NotBlank()); - $validator->validate(); + $validator->validate($parser->parseFile(__DIR__.'/fixtures/example.csv')); $this->assertEquals(true, $validator->isValid()); $this->assertEquals(0, count($validator->getErrors())); - + $parser = $this->generateParser('example.csv'); - $validator = $this->generateValidator($parser); + $validator = $this->generateValidator(); $validator->addFieldConstraint(0, new NotBlank()); $validator->addFieldConstraint(1, new NotBlank()); - $validator->validate(); + $validator->validate($parser->parseFile(__DIR__.'/fixtures/example.csv')); $this->assertEquals(true, $validator->isValid()); $this->assertEquals(0, count($validator->getErrors())); } - + public function testFieldContraintsKo() { - $parser = $this->generateParser('example.csv'); - $validator = $this->generateValidator($parser); + $parser = $this->generateParser(); + $validator = $this->generateValidator(); $validator->addFieldConstraint(0, new Email()); - $validator->validate(); + $validator->validate($parser->parseFile(__DIR__.'/fixtures/example.csv')); $this->assertEquals(false, $validator->isValid()); - $this->assertEquals(4, count($validator->getErrors())); - - $parser = $this->generateParser('example.csv'); - $validator = $this->generateValidator($parser); + $this->assertEquals(2, count($validator->getErrors())); + + $parser = $this->generateParser(); + $validator = $this->generateValidator(); $validator->addFieldConstraint(1, new Email()); - $validator->validate(); + $validator->validate($parser->parseFile(__DIR__.'/fixtures/example.csv')); $this->assertEquals(false, $validator->isValid()); - $this->assertEquals(4, count($validator->getErrors())); - + $this->assertEquals(5, count($validator->getErrors())); + $parser = $this->generateParser('example.csv'); - $validator = $this->generateValidator($parser); + $validator = $this->generateValidator(); $validator->addFieldConstraint(0, new Email()); $validator->addFieldConstraint(1, new Email()); - $validator->validate(); + $validator->validate($parser->parseFile(__DIR__.'/fixtures/example.csv')); $this->assertEquals(false, $validator->isValid()); - $this->assertEquals(8, count($validator->getErrors())); + $this->assertEquals(7, count($validator->getErrors())); } - protected function generateParser($file) + protected function generateParser() { - return new CsvParser(__DIR__.'/fixtures/'.$file); + return new CsvParser(); } - protected function generateValidator(CsvParser $parser) + protected function generateValidator() { - return new Validator($parser, Validation::createValidator()); + return new Validator(Validation::createValidator()); } }