2015-03-17 16:13:33 +01:00
csv-validator
=============
2018-06-06 15:55:17 +02:00
[![ ](https://phpci.gitnet.fr/build-status/image/1?branch=master&label=PHPCensor&style=flat-square )](https://phpci.gitnet.fr/build-status/view/1)
2016-12-03 18:46:28 +01:00
2016-05-22 19:17:47 +02:00
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 ).
2016-12-02 14:31:27 +01:00
* [Installation ](#installation )
* [Example ](#example )
* [Contributors ](#contributors )
2016-05-22 19:17:47 +02:00
Installation
------------
2016-05-22 19:18:38 +02:00
You need [composer ](https://getcomposer.org/ ):
2016-05-22 19:17:47 +02:00
2016-12-02 14:27:30 +01:00
composer require deblan/csv-validator
2016-05-20 12:01:26 +02:00
2016-05-20 14:45:28 +02:00
Example
-------
2016-05-22 19:17:47 +02:00
```
< ?php
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;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
require __DIR__ .'/vendor/autoload.php';
// Initialisation of the validator
2016-05-23 20:19:13 +02:00
$validator = new Validator();
2016-05-22 19:17:47 +02:00
// The first field must contain an email
$validator->addFieldConstraint(0, new Email());
// The second field must contain a date
$validator->addFieldConstraint(1, new Date());
2016-05-22 23:39:06 +02:00
// Validate the legend
2017-03-12 19:22:52 +01:00
$validator->setExpectedHeaders(['foo', 'bar', 'bim']);
2016-05-22 23:39:06 +02:00
2016-05-22 19:17:47 +02:00
// An line must contain 3 columns
$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');
}
}));
2016-05-23 20:19:13 +02:00
// Initialisation of the parser
2017-03-12 19:22:52 +01:00
$parser = new CsvParser();
$parser->setHasHeaders(true);
2016-05-23 20:19:13 +02:00
2017-03-12 19:22:52 +01:00
$validator->validate($parser->parseFile(__DIR__.'/tests/fixtures/example.csv'));
2016-05-22 19:17:47 +02:00
if ($validator->isValid() === false) {
2016-05-22 23:24:27 +02:00
foreach ($validator->getErrors() as $error) {
2016-12-02 14:27:30 +01:00
$line = $error->getLine();
2016-05-22 23:24:27 +02:00
$column = $error->getColumn();
$message = $error->getViolation()->getMessage();
2016-12-02 14:27:30 +01:00
2016-05-22 23:24:27 +02:00
echo < < < EOF
< ul >
< li > Line: $line< / li >
< li > Column: $column< / li >
< li >
< p > $message< / p >
< / li >
< / ul >
EOF;
2016-05-22 19:17:47 +02:00
}
}
```
2016-12-02 14:31:27 +01:00
2017-03-12 19:22:52 +01:00
Run `example.php` and see results:
```
< ul >
< li > Line: 1< / li >
< li > Column: < / li >
< li >
< p > Invalid legend.< / p >
< / li >
< / ul >
< ul >
< li > Line: 4< / li >
< li > Column: < / li >
< li >
< p > The line must contain 3 columns< / p >
< / li >
< / ul >
< ul >
< li > Line: 2< / li >
< li > Column: 1< / li >
< li >
< p > This value is not a valid email address.< / p >
< / li >
< / ul >
< ul >
< li > Line: 3< / li >
< li > Column: 2< / li >
< li >
< p > This value is not a valid date.< / p >
< / li >
< / ul >
```
2016-12-02 14:31:27 +01:00
Contributors
------------
* Simon Vieille
* Gautier Deruette