Compare commits

..

11 commits

Author SHA1 Message Date
cbd3ce7e69 Mettre à jour 'src/Deblan/CsvValidator/Validator.php' 2016-12-01 11:41:15 +01:00
6be22908dc Packaging 2016-10-13 15:53:40 +02:00
7ef3903863 Packaging 2016-10-13 15:51:11 +02:00
0ee2884b3d Renaming of method 2016-05-30 12:02:49 +02:00
e88a23bb1d packaging 2016-05-24 17:07:50 +02:00
003621e8a7 packaging 2016-05-24 17:07:33 +02:00
7aa3198e3f packaging 2016-05-24 17:06:05 +02:00
45c446bb14 packaging 2016-05-24 17:04:02 +02:00
0f60a1a8b6 packaging 2016-05-24 17:03:10 +02:00
0bb235e8dd packaging 2016-05-24 00:43:14 +02:00
0c5b54c79e Dependency injection 2016-05-23 20:19:13 +02:00
4 changed files with 39 additions and 31 deletions

View file

@ -29,12 +29,8 @@ use Symfony\Component\Validator\Context\ExecutionContextInterface;
require __DIR__.'/vendor/autoload.php';
// Initialisation of the parser
$parser = new CsvParser(__DIR__.'/tests/fixtures/example.csv');
$parser->setHasLegend(true);
// Initialisation of the validator
$validator = new Validator($parser, Validation::createValidator());
$validator = new Validator();
// The first field must contain an email
$validator->addFieldConstraint(0, new Email());
@ -43,7 +39,7 @@ $validator->addFieldConstraint(0, new Email());
$validator->addFieldConstraint(1, new Date());
// Validate the legend
$validator->setExceptedLegend(array('foo', 'bar', 'bim'));
$validator->setExpectedLegend(array('foo', 'bar', 'bim'));
// An line must contain 3 columns
$validator->addDataConstraint(new Callback(function($data, ExecutionContextInterface $context) {
@ -52,7 +48,11 @@ $validator->addDataConstraint(new Callback(function($data, ExecutionContextInter
}
}));
$validator->validate();
// Initialisation of the parser
$parser = new CsvParser(__DIR__.'/tests/fixtures/example.csv');
$parser->setHasLegend(true);
$validator->validate($parser);
if ($validator->isValid() === false) {
foreach ($validator->getErrors() as $error) {
@ -73,5 +73,3 @@ EOF;
}
}
```

View file

@ -17,6 +17,6 @@
"require": {
"php": ">=5.6.0",
"symfony/validator": "2.*",
"deblan/csv": "dev-master"
"deblan/csv": "v1.1"
}
}

View file

@ -10,12 +10,8 @@ use Symfony\Component\Validator\Context\ExecutionContextInterface;
require __DIR__.'/vendor/autoload.php';
// Initialisation of the parser
$parser = new CsvParser(__DIR__.'/tests/fixtures/example.csv');
$parser->setHasLegend(true);
// Initialisation of the validator
$validator = new Validator($parser, Validation::createValidator());
$validator = new Validator();
// The first field must contain an email
$validator->addFieldConstraint(0, new Email());
@ -24,7 +20,7 @@ $validator->addFieldConstraint(0, new Email());
$validator->addFieldConstraint(1, new Date());
// Validate the legend
$validator->setExceptedLegend(array('foo', 'bar', 'bim'));
$validator->setExpectedLegend(array('foo', 'bar', 'bim'));
// An line must contain 3 columns
$validator->addDataConstraint(new Callback(function($data, ExecutionContextInterface $context) {
@ -33,7 +29,11 @@ $validator->addDataConstraint(new Callback(function($data, ExecutionContextInter
}
}));
$validator->validate();
// Initialisation of the parser
$parser = new CsvParser(__DIR__.'/tests/fixtures/example.csv');
$parser->setHasLegend(true);
$validator->validate($parser);
if ($validator->isValid() === false) {
foreach ($validator->getErrors() as $error) {

View file

@ -7,6 +7,7 @@ use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintViolationList;
use Symfony\Component\Validator\Validator\RecursiveValidator;
use Symfony\Component\Validator\ConstraintViolation;
use Symfony\Component\Validator\Validation;
/**
* Class Validator
@ -52,20 +53,21 @@ class Validator
/**
* Constructor
*
* @param CsvParser $parser
* @param RecursiveValidator $validator
*/
public function __construct(CsvParser $parser, RecursiveValidator $validator)
public function __construct(RecursiveValidator $validator = null)
{
$this->parser = $parser;
$this->parser->parse();
if ($validator === null) {
$validator = Validation::createValidator();
}
$this->validator = $validator;
}
/**
* Append a constraint to a specific column
*
* @param $key The column number
* @param integer $key The column number
* @param Constraint $constraint The constraint
* @return Validator
*/
@ -83,7 +85,6 @@ class Validator
/**
* Append a constraint to a specific line
*
* @param $key The column number
* @param Constraint $constraint The constraint
* @return Validator
*/
@ -95,12 +96,12 @@ class Validator
}
/**
* Set the excepted legend
* Set the expected legend
*
* @param array $legend Expected legend
* @return Validator
*/
public function setExceptedLegend(array $legend)
public function setExpectedLegend(array $legend)
{
$this->expectedLegend = $legend;
@ -109,13 +110,19 @@ class Validator
/**
* Run the validation
* @param CsvParser $parser
*/
public function validate()
public function validate(CsvParser $parser)
{
if ($this->hasValidate) {
if ($this->parser !== $parser) {
$this->parser = $parser;
$this->parser->parse();
$this->errors = [];
}
elseif ($this->hasValidate) {
return;
}
$this->validateLegend();
$this->validateDatas();
$this->validateFields();
@ -123,7 +130,10 @@ class Validator
$this->hasValidate = true;
}
/**
* Validates the legend
*/
protected function validateLegend()
{
if (!$this->parser->getHasLegend()) {
@ -150,7 +160,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));
}
@ -177,7 +187,7 @@ class Validator
);
} else {
foreach ($constraints as $constraint) {
$violations = $this->validator->validateValue($data[$key], $constraint);
$violations = $this->validator->validate($data[$key], $constraint);
$this->mergeViolationsMessages(
$violations,