forked from deblan/csv-validator
Example improved and refactoring
This commit is contained in:
parent
395571a5de
commit
cf46799228
19
example.php
19
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 <<<EOF
|
||||
<ul>
|
||||
<li>Line: $line</li>
|
||||
<li>Column: $column</li>
|
||||
<li>
|
||||
<p>$message</p>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
EOF;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
9
tests/fixtures/example.csv
vendored
9
tests/fixtures/example.csv
vendored
|
@ -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";""
|
||||
|
|
Can't render this file because it has a wrong number of fields in line 4.
|
Loading…
Reference in a new issue