diff --git a/example.php b/example.php index 9fb13a1..eed4536 100644 --- a/example.php +++ b/example.php @@ -33,11 +33,20 @@ $validator->addDataConstraint(new Callback(function($data, ExecutionContextInter $validator->validate(); if ($validator->isValid() === false) { - foreach ($validator->getErrors() as $violation) { - $line = $violation->getLine(); - $column = $violation->getColumn(); - $message = $violation->getViolation()->getMessage(); + foreach ($validator->getErrors() as $error) { + $line = $error->getLine(); + $column = $error->getColumn(); + $message = $error->getViolation()->getMessage(); - // Up to you! + echo << +
  • Line: $line
  • +
  • Column: $column
  • +
  • +

    $message

    +
  • + + +EOF; } } diff --git a/src/Deblan/CsvValidator/Validator.php b/src/Deblan/CsvValidator/Validator.php index b7024c3..13594ac 100644 --- a/src/Deblan/CsvValidator/Validator.php +++ b/src/Deblan/CsvValidator/Validator.php @@ -118,7 +118,7 @@ class Validator foreach ($this->dataConstraints as $constraint) { $violations = $this->validator->validateValue($data, $constraint); - $this->mergeViolationsMessages($violations, $line); + $this->mergeViolationsMessages($violations, $this->getTrueLine($line)); } } } @@ -135,12 +135,21 @@ class Validator foreach ($this->parser->getDatas() as $line => $data) { foreach ($this->fieldConstraints as $key => $constraints) { if (!isset($data[$key])) { - $this->mergeErrorMessage(sprintf('Field "%s" does not exist.', $key + 1), $line, $key); + $column = $this->getTrueColunm($key); + $this->mergeErrorMessage( + sprintf('Field "%s" does not exist.', $column), + $this->getTrueLine($line), + $column + ); } else { foreach ($constraints as $constraint) { $violations = $this->validator->validateValue($data[$key], $constraint); - $this->mergeViolationsMessages($violations, $line, $key); + $this->mergeViolationsMessages( + $violations, + $this->getTrueLine($line), + $this->getTrueColunm($key) + ); } } } @@ -160,12 +169,8 @@ class Validator return; } - if (is_int($key)) { - $key++; - } - foreach ($violations as $violation) { - $this->errors[] = $this->generateViolation($line + 1, $key, $violation); + $this->errors[] = $this->generateViolation($line, $key, $violation); } } @@ -178,16 +183,8 @@ class Validator */ protected function mergeErrorMessage($message, $line, $key = null) { - if (!array_key_exists($line, $this->errors)) { - $this->errors[$line] = []; - } - - if (is_int($key)) { - $key++; - } - $violation = $this->generateConstraintViolation($message); - $this->errors[] = $this->generateViolation($line + 1, $key, $violation); + $this->errors[] = $this->generateViolation($line, $key, $violation); } /** @@ -238,4 +235,30 @@ class Validator { return new Violation($line, $key, $violation); } + + /** + * Get the true line number of an error + * + * @param integer $line + * @return integer + */ + protected function getTrueLine($line) + { + if ($this->parser->getHasLegend()) { + $line++; + } + + return ++$line; + } + + /** + * Get the true culumn number of an error + * + * @param integer $key + * @return integer + */ + protected function getTrueColunm($key) + { + return ++$key; + } } diff --git a/src/Deblan/CsvValidator/Violation.php b/src/Deblan/CsvValidator/Violation.php index bc4f4ce..1989c8a 100644 --- a/src/Deblan/CsvValidator/Violation.php +++ b/src/Deblan/CsvValidator/Violation.php @@ -64,7 +64,11 @@ class Violation */ public function setColumn($column) { - $this->column = (int) $column; + if ($column !== null) { + $column = (int) $column; + } + + $this->column = $column; return $this; } diff --git a/tests/fixtures/example.csv b/tests/fixtures/example.csv index 7cadbe7..677f622 100644 --- a/tests/fixtures/example.csv +++ b/tests/fixtures/example.csv @@ -1,4 +1,5 @@ -"foo";"bar" -"foo1";"bar1" -"foo2";"bar2" -"foo3";"bar3" +"foo";"bar";"" +"foo1";"1989-07-27";"" +"foo2@bar.com";"bar2";"" +"foo3@bar.com";"1989-07-27" +"foo3@bar.com";"1989-07-27";""