Compare commits

..

5 commits

Author SHA1 Message Date
8ff4db14a2 packaging 2016-05-23 20:59:27 +02:00
a09bb929a0 packaging 2016-05-23 20:43:08 +02:00
0c24bf2e3a packaging 2016-05-23 20:32:12 +02:00
ea8c66512a packaging 2016-05-23 20:31:44 +02:00
a87103518a packaging 2016-05-23 20:31:04 +02:00
4 changed files with 31 additions and 39 deletions

View file

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

View file

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

View file

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

View file

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