diff --git a/example.php b/example.php index eed4536..2a475df 100644 --- a/example.php +++ b/example.php @@ -23,6 +23,9 @@ $validator->addFieldConstraint(0, new Email()); // The second field must contain a date $validator->addFieldConstraint(1, new Date()); +// Validate the legend +$validator->setExceptedLegend(array('foo', 'bar', 'bim')); + // An line must contain 3 columns $validator->addDataConstraint(new Callback(function($data, ExecutionContextInterface $context) { if (count($data) !== 6) { // 6 because of the legend (3 fields * 2) diff --git a/src/Deblan/CsvValidator/Validator.php b/src/Deblan/CsvValidator/Validator.php index 13594ac..52459cd 100644 --- a/src/Deblan/CsvValidator/Validator.php +++ b/src/Deblan/CsvValidator/Validator.php @@ -89,6 +89,19 @@ class Validator 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 */ @@ -98,6 +111,7 @@ class Validator return; } + $this->validateLegend(); $this->validateDatas(); $this->validateFields(); @@ -105,6 +119,21 @@ class Validator $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 */ diff --git a/tests/fixtures/example.csv b/tests/fixtures/example.csv index 677f622..a38a583 100644 --- a/tests/fixtures/example.csv +++ b/tests/fixtures/example.csv @@ -1,4 +1,4 @@ -"foo";"bar";"" +"foo";"bar";"boo" "foo1";"1989-07-27";"" "foo2@bar.com";"bar2";"" "foo3@bar.com";"1989-07-27"