Example improved and refactoring

This commit is contained in:
Simon Vieille 2016-05-22 23:22:12 +02:00
parent 395571a5de
commit cf46799228
4 changed files with 64 additions and 27 deletions

View file

@ -33,11 +33,20 @@ $validator->addDataConstraint(new Callback(function($data, ExecutionContextInter
$validator->validate(); $validator->validate();
if ($validator->isValid() === false) { if ($validator->isValid() === false) {
foreach ($validator->getErrors() as $violation) { foreach ($validator->getErrors() as $error) {
$line = $violation->getLine(); $line = $error->getLine();
$column = $violation->getColumn(); $column = $error->getColumn();
$message = $violation->getViolation()->getMessage(); $message = $error->getViolation()->getMessage();
// Up to you! echo <<<EOF
<ul>
<li>Line: $line</li>
<li>Column: $column</li>
<li>
<p>$message</p>
</li>
</ul>
EOF;
} }
} }

View file

@ -118,7 +118,7 @@ class Validator
foreach ($this->dataConstraints as $constraint) { foreach ($this->dataConstraints as $constraint) {
$violations = $this->validator->validateValue($data, $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->parser->getDatas() as $line => $data) {
foreach ($this->fieldConstraints as $key => $constraints) { foreach ($this->fieldConstraints as $key => $constraints) {
if (!isset($data[$key])) { 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 { } else {
foreach ($constraints as $constraint) { foreach ($constraints as $constraint) {
$violations = $this->validator->validateValue($data[$key], $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; return;
} }
if (is_int($key)) {
$key++;
}
foreach ($violations as $violation) { 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) 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); $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); 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;
}
} }

View file

@ -64,7 +64,11 @@ class Violation
*/ */
public function setColumn($column) public function setColumn($column)
{ {
$this->column = (int) $column; if ($column !== null) {
$column = (int) $column;
}
$this->column = $column;
return $this; return $this;
} }

View file

@ -1,4 +1,5 @@
"foo";"bar" "foo";"bar";""
"foo1";"bar1" "foo1";"1989-07-27";""
"foo2";"bar2" "foo2@bar.com";"bar2";""
"foo3";"bar3" "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.