forked from deblan/csv-validator
Compare commits
5 commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 8ff4db14a2 | |||
| a09bb929a0 | |||
| 0c24bf2e3a | |||
| ea8c66512a | |||
| a87103518a |
4 changed files with 31 additions and 39 deletions
16
README.md
16
README.md
|
|
@ -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;
|
|||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -17,6 +17,6 @@
|
|||
"require": {
|
||||
"php": ">=5.6.0",
|
||||
"symfony/validator": "2.*",
|
||||
"deblan/csv": "v1.1"
|
||||
"deblan/csv": "dev-master"
|
||||
}
|
||||
}
|
||||
|
|
|
|||
14
example.php
14
example.php
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue