csv-validator/src/Deblan/CsvValidator/Validator.php

192 lines
4.5 KiB
PHP
Raw Normal View History

2015-03-17 17:54:50 +01:00
<?php
namespace Deblan\CsvValidator;
use Deblan\Csv\CsvParser;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintViolationList;
2016-05-20 12:01:26 +02:00
use Symfony\Component\Validator\Validator\RecursiveValidator;
2016-05-20 12:03:28 +02:00
use Symfony\Component\Validator\ConstraintViolation;
2015-03-17 17:54:50 +01:00
2016-05-20 12:03:28 +02:00
/**
* Class Validator
* @author Simon Vieille <simon@deblan.fr>
*/
2015-03-17 17:54:50 +01:00
class Validator
{
2016-05-20 12:03:28 +02:00
/**
* @var CsvParser
*/
2015-03-17 17:54:50 +01:00
protected $parser;
2016-05-20 12:03:28 +02:00
/**
* @var RecursiveValidator
*/
2015-03-17 17:54:50 +01:00
protected $validator;
2016-05-20 12:03:28 +02:00
/**
* @var array
*/
2015-03-17 17:54:50 +01:00
protected $fieldsConstraints = [];
2016-05-20 12:03:28 +02:00
/**
* @var array
*/
2015-03-17 17:54:50 +01:00
protected $dataConstraints = [];
2016-05-20 12:03:28 +02:00
/**
* @var boolean
*/
2015-03-17 17:54:50 +01:00
protected $hasValidate = false;
2016-05-20 12:03:28 +02:00
/**
* @var array
*/
2015-03-17 17:54:50 +01:00
protected $errors = [];
2016-05-20 12:03:28 +02:00
/**
* Constructor
*
* @param CsvParser $parser
* @param RecursiveValidator $validator
*/
2016-05-20 12:01:26 +02:00
public function __construct(CsvParser $parser, RecursiveValidator $validator)
2015-03-17 17:54:50 +01:00
{
$this->parser = $parser;
$this->parser->parse();
$this->validator = $validator;
}
2016-05-20 12:03:28 +02:00
/**
* Append a constraint to a specific column
*
* @param $key The column number
* @param Constraint $constraint The constraint
* @return Validator
*/
2015-03-17 17:54:50 +01:00
public function addFieldConstraint($key, Constraint $constraint)
{
if (!array_key_exists($key, $this->fieldsConstraints)) {
$this->fieldsConstraints[$key] = [];
}
$this->fieldsConstraints[$key][] = $constraint;
return $this;
}
2016-05-20 12:03:28 +02:00
/**
* Append a constraint to a specific line
*
* @param $key The column number
* @param Constraint $constraint The constraint
* @return Validator
*/
public function addDataConstraint(Constraint $constraint)
2015-03-17 17:54:50 +01:00
{
$this->dataConstraints[] = $constraint;
return $this;
}
2016-05-20 12:03:28 +02:00
/**
* Run the validation
*/
2015-03-17 17:54:50 +01:00
public function validate()
{
if ($this->hasValidate) {
return;
}
foreach ($this->parser->getDatas() as $line => $data) {
foreach ($this->dataConstraints as $constraint) {
$violations = $this->validator->validateValue($data, $constraint);
$this->mergeViolationsMessages($violations, $line);
}
foreach ($this->fieldsConstraints as $key => $constraints) {
if (!isset($data[$key])) {
$this->mergeErrorMessage(sprintf('Field "%s" does not exist.', $key + 1), $line, $key);
} else {
foreach ($constraints as $constraint) {
$violations = $this->validator->validateValue($data[$key], $constraint);
$this->mergeViolationsMessages($violations, $line, $key);
}
}
}
}
$this->hasValidate = true;
}
2016-05-20 12:03:28 +02:00
/**
* Add violations
*
* @param ConstraintViolationList $violations
* @param integer $line The line of the violations
* @param integer|null $key The column of the violations
*/
2015-03-17 17:54:50 +01:00
protected function mergeViolationsMessages(ConstraintViolationList $violations, $line, $key = null)
{
2016-05-20 12:01:26 +02:00
if (count($violations) === 0) {
return;
}
2015-03-17 17:54:50 +01:00
if (is_int($key)) {
$key++;
}
foreach ($violations as $violation) {
2016-05-20 12:03:28 +02:00
$this->errors[] = new Violation($line + 1, $key, $violation);
2015-03-17 17:54:50 +01:00
}
}
2016-05-20 12:03:28 +02:00
/**
* Create and append a violation from a string error
*
* @param string $message The error message
* @param integer $line The line of the violations
* @param integer|null $key The column of the violations
*/
2015-03-17 17:54:50 +01:00
protected function mergeErrorMessage($message, $line, $key = null)
{
if (!array_key_exists($line, $this->errors)) {
$this->errors[$line] = [];
}
if (is_int($key)) {
$key++;
}
2016-05-20 12:03:28 +02:00
$violation = new ConstraintViolation($message, $message, [], null, '', null);
$this->errors[] = new Violation($line + 1, $key, $violation);
2015-03-17 17:54:50 +01:00
}
2016-05-20 12:03:28 +02:00
/**
* Returns the validation status
*
* @return boolean
* @throw RuntimeException No validation yet
*/
2015-03-17 17:54:50 +01:00
public function isValid()
{
if (!$this->hasValidate) {
throw new \RuntimeException('You must validate before.');
}
return empty($this->errors);
}
2016-05-20 12:03:28 +02:00
/**
* Returns the errors
*
* @return array
*/
2015-03-17 17:54:50 +01:00
public function getErrors()
{
return $this->errors;
}
}