forked from deblan/csv-validator
Dependency injection
This commit is contained in:
parent
a0a4ff15ee
commit
0c5b54c79e
12
README.md
12
README.md
|
@ -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());
|
||||
|
@ -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) {
|
||||
|
|
12
example.php
12
example.php
|
@ -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());
|
||||
|
@ -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) {
|
||||
|
|
|
@ -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
|
||||
|
@ -55,10 +56,12 @@ class Validator
|
|||
* @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;
|
||||
}
|
||||
|
||||
|
@ -110,12 +113,17 @@ class Validator
|
|||
/**
|
||||
* Run the validation
|
||||
*/
|
||||
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 +131,10 @@ class Validator
|
|||
|
||||
$this->hasValidate = true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Validates the legend
|
||||
*/
|
||||
protected function validateLegend()
|
||||
{
|
||||
if (!$this->parser->getHasLegend()) {
|
||||
|
|
Loading…
Reference in a new issue