forked from deblan/csv-validator
Test the legend
This commit is contained in:
parent
8cb4496806
commit
522e7f9032
|
@ -23,6 +23,9 @@ $validator->addFieldConstraint(0, new Email());
|
||||||
// The second field must contain a date
|
// The second field must contain a date
|
||||||
$validator->addFieldConstraint(1, new Date());
|
$validator->addFieldConstraint(1, new Date());
|
||||||
|
|
||||||
|
// Validate the legend
|
||||||
|
$validator->setExceptedLegend(array('foo', 'bar', 'bim'));
|
||||||
|
|
||||||
// An line must contain 3 columns
|
// An line must contain 3 columns
|
||||||
$validator->addDataConstraint(new Callback(function($data, ExecutionContextInterface $context) {
|
$validator->addDataConstraint(new Callback(function($data, ExecutionContextInterface $context) {
|
||||||
if (count($data) !== 6) { // 6 because of the legend (3 fields * 2)
|
if (count($data) !== 6) { // 6 because of the legend (3 fields * 2)
|
||||||
|
|
|
@ -89,6 +89,19 @@ class Validator
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the excepted legend
|
||||||
|
*
|
||||||
|
* @param array $legend Expected legend
|
||||||
|
* @return Validator
|
||||||
|
*/
|
||||||
|
public function setExceptedLegend(array $legend)
|
||||||
|
{
|
||||||
|
$this->expectedLegend = $legend;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Run the validation
|
* Run the validation
|
||||||
*/
|
*/
|
||||||
|
@ -98,6 +111,7 @@ class Validator
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this->validateLegend();
|
||||||
$this->validateDatas();
|
$this->validateDatas();
|
||||||
$this->validateFields();
|
$this->validateFields();
|
||||||
|
|
||||||
|
@ -105,6 +119,21 @@ class Validator
|
||||||
$this->hasValidate = true;
|
$this->hasValidate = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function validateLegend()
|
||||||
|
{
|
||||||
|
if (!$this->parser->getHasLegend()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (null === $this->expectedLegend) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($this->parser->getLegend() !== $this->expectedLegend) {
|
||||||
|
$this->mergeErrorMessage('Invalid legend.', 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Validates datas
|
* Validates datas
|
||||||
*/
|
*/
|
||||||
|
|
2
tests/fixtures/example.csv
vendored
2
tests/fixtures/example.csv
vendored
|
@ -1,4 +1,4 @@
|
||||||
"foo";"bar";""
|
"foo";"bar";"boo"
|
||||||
"foo1";"1989-07-27";""
|
"foo1";"1989-07-27";""
|
||||||
"foo2@bar.com";"bar2";""
|
"foo2@bar.com";"bar2";""
|
||||||
"foo3@bar.com";"1989-07-27"
|
"foo3@bar.com";"1989-07-27"
|
||||||
|
|
Can't render this file because it has a wrong number of fields in line 4.
|
Loading…
Reference in a new issue