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

315 lines
7.3 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;
2016-05-23 20:19:13 +02:00
use Symfony\Component\Validator\Validation;
2015-03-17 17:54:50 +01:00
2016-05-20 12:03:28 +02:00
/**
2016-12-02 13:50:07 +01:00
* Class Validator.
*
2016-05-20 12:03:28 +02:00
* @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
*/
2016-05-22 18:37:12 +02:00
protected $fieldConstraints = [];
2015-03-17 17:54:50 +01:00
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
/**
2016-12-02 13:50:07 +01:00
* @var bool
2016-05-20 12:03:28 +02:00
*/
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-12-02 13:50:07 +01:00
2016-05-23 14:58:54 +02:00
/**
* @var array
*/
protected $expectedHeaders = [];
2015-03-17 17:54:50 +01:00
2016-05-20 12:03:28 +02:00
/**
2016-12-02 13:50:07 +01:00
* Constructor.
2016-05-20 12:03:28 +02:00
*
* @param RecursiveValidator $validator
*/
2016-05-23 20:19:13 +02:00
public function __construct(RecursiveValidator $validator = null)
2015-03-17 17:54:50 +01:00
{
2016-05-23 20:19:13 +02:00
if ($validator === null) {
$validator = Validation::createValidator();
2016-12-02 13:50:07 +01:00
}
2015-03-17 17:54:50 +01:00
$this->validator = $validator;
}
2016-05-20 12:03:28 +02:00
/**
2016-12-02 13:50:07 +01:00
* Append a constraint to a specific column.
2016-05-20 12:03:28 +02:00
*
2016-12-02 13:50:07 +01:00
* @param int $key The column number
2016-05-20 12:03:28 +02:00
* @param Constraint $constraint The constraint
2016-12-02 13:50:07 +01:00
*
2016-05-20 12:03:28 +02:00
* @return Validator
*/
2015-03-17 17:54:50 +01:00
public function addFieldConstraint($key, Constraint $constraint)
{
2016-05-22 18:37:12 +02:00
if (!array_key_exists($key, $this->fieldConstraints)) {
$this->fieldConstraints[$key] = [];
2015-03-17 17:54:50 +01:00
}
2016-05-22 18:37:12 +02:00
$this->fieldConstraints[$key][] = $constraint;
2015-03-17 17:54:50 +01:00
return $this;
}
2016-05-20 12:03:28 +02:00
/**
2016-12-02 13:50:07 +01:00
* Append a constraint to a specific line.
2016-05-20 12:03:28 +02:00
*
* @param Constraint $constraint The constraint
2016-12-02 13:50:07 +01:00
*
2016-05-20 12:03:28 +02:00
* @return Validator
*/
public function addDataConstraint(Constraint $constraint)
2015-03-17 17:54:50 +01:00
{
$this->dataConstraints[] = $constraint;
return $this;
}
2016-05-22 23:38:17 +02:00
/**
2016-12-02 13:50:07 +01:00
* Set the expected legend.
2016-05-22 23:38:17 +02:00
*
* @param array $legend Expected legend
2016-12-02 13:50:07 +01:00
*
2016-05-22 23:38:17 +02:00
* @return Validator
*/
public function setExpectedHeaders(array $legend)
2016-05-22 23:38:17 +02:00
{
$this->expectedHeaders = $legend;
2016-05-22 23:38:17 +02:00
return $this;
}
2016-05-20 12:03:28 +02:00
/**
2016-12-02 13:50:07 +01:00
* Run the validation.
*
* @param CsvParser $parser
2016-05-20 12:03:28 +02:00
*/
2016-05-23 20:19:13 +02:00
public function validate(CsvParser $parser)
2015-03-17 17:54:50 +01:00
{
2016-05-23 20:19:13 +02:00
if ($this->parser !== $parser) {
$this->parser = $parser;
$this->errors = [];
2016-12-02 13:50:07 +01:00
} elseif ($this->hasValidate) {
2015-03-17 17:54:50 +01:00
return;
}
2016-12-02 13:50:07 +01:00
$this->validateHeaders();
2016-05-22 18:37:12 +02:00
$this->validateDatas();
$this->validateFields();
$this->hasValidate = true;
}
2016-12-02 13:50:07 +01:00
2016-05-23 20:19:13 +02:00
/**
2016-12-02 13:50:07 +01:00
* Validates the legend.
2016-05-23 20:19:13 +02:00
*/
protected function validateHeaders()
2016-05-22 23:38:17 +02:00
{
if (!$this->parser->getHasHeaders()) {
2016-05-22 23:38:17 +02:00
return;
}
if (empty($this->expectedHeaders)) {
2016-05-22 23:38:17 +02:00
return;
}
2016-12-02 13:50:07 +01:00
if ($this->parser->getHeaders() !== $this->expectedHeaders) {
2016-05-22 23:38:17 +02:00
$this->mergeErrorMessage('Invalid legend.', 1);
}
}
2016-05-22 18:37:12 +02:00
/**
2016-12-02 13:50:07 +01:00
* Validates datas.
2016-05-22 18:37:12 +02:00
*/
2016-12-02 13:50:07 +01:00
protected function validateDatas()
2016-05-22 18:37:12 +02:00
{
if (empty($this->dataConstraints)) {
return;
}
2015-03-17 17:54:50 +01:00
foreach ($this->parser->getDatas() as $line => $data) {
foreach ($this->dataConstraints as $constraint) {
$violations = $this->validator->validate($data, $constraint);
2015-03-17 17:54:50 +01:00
2016-05-22 23:22:12 +02:00
$this->mergeViolationsMessages($violations, $this->getTrueLine($line));
2015-03-17 17:54:50 +01:00
}
2016-05-22 18:37:12 +02:00
}
}
2015-03-17 17:54:50 +01:00
2016-05-22 18:37:12 +02:00
/**
2016-12-02 13:50:07 +01:00
* Validates fields.
2016-05-22 18:37:12 +02:00
*/
2016-12-02 13:50:07 +01:00
protected function validateFields()
2016-05-22 18:37:12 +02:00
{
if (empty($this->fieldConstraints)) {
return;
}
2016-12-02 13:50:07 +01:00
2016-05-22 18:37:12 +02:00
foreach ($this->parser->getDatas() as $line => $data) {
foreach ($this->fieldConstraints as $key => $constraints) {
2015-03-17 17:54:50 +01:00
if (!isset($data[$key])) {
2016-05-22 23:22:12 +02:00
$column = $this->getTrueColunm($key);
$this->mergeErrorMessage(
2016-12-02 13:50:07 +01:00
sprintf('Field "%s" does not exist.', $column),
$this->getTrueLine($line),
2016-05-22 23:22:12 +02:00
$column
);
2015-03-17 17:54:50 +01:00
} else {
foreach ($constraints as $constraint) {
$violations = $this->validator->validate($data[$key], $constraint);
2015-03-17 17:54:50 +01:00
2016-05-22 23:22:12 +02:00
$this->mergeViolationsMessages(
2016-12-02 13:50:07 +01:00
$violations,
$this->getTrueLine($line),
2016-05-22 23:22:12 +02:00
$this->getTrueColunm($key)
);
2015-03-17 17:54:50 +01:00
}
}
}
}
}
2016-05-20 12:03:28 +02:00
/**
2016-12-02 13:50:07 +01:00
* Add violations.
2016-05-20 12:03:28 +02:00
*
* @param ConstraintViolationList $violations
2016-12-02 13:50:07 +01:00
* @param int $line The line of the violations
* @param int|null $key The column of the violations
2016-05-20 12:03:28 +02:00
*/
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
foreach ($violations as $violation) {
2016-05-22 23:22:12 +02:00
$this->errors[] = $this->generateViolation($line, $key, $violation);
2015-03-17 17:54:50 +01:00
}
}
2016-05-20 12:03:28 +02:00
/**
2016-12-02 13:50:07 +01:00
* Create and append a violation from a string error.
2016-05-20 12:03:28 +02:00
*
2016-12-02 13:50:07 +01:00
* @param string $message The error message
* @param int $line The line of the violations
* @param int|null $key The column of the violations
2016-05-20 12:03:28 +02:00
*/
2015-03-17 17:54:50 +01:00
protected function mergeErrorMessage($message, $line, $key = null)
{
2016-05-22 18:37:12 +02:00
$violation = $this->generateConstraintViolation($message);
2016-05-22 23:22:12 +02:00
$this->errors[] = $this->generateViolation($line, $key, $violation);
2015-03-17 17:54:50 +01:00
}
2016-05-20 12:03:28 +02:00
/**
2016-12-02 13:50:07 +01:00
* Returns the validation status.
2016-05-20 12:03:28 +02:00
*
2016-12-02 13:50:07 +01:00
* @return bool
2016-05-20 12:03:28 +02:00
* @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
/**
2016-12-02 13:50:07 +01:00
* Returns the errors.
2016-05-20 12:03:28 +02:00
*
* @return array
*/
2015-03-17 17:54:50 +01:00
public function getErrors()
{
return $this->errors;
}
2016-05-22 18:37:12 +02:00
/**
2016-12-02 13:50:07 +01:00
* Generate a ConstraintViolation.
2016-05-22 18:37:12 +02:00
*
* @param string $message The error message
2016-12-02 13:50:07 +01:00
*
2016-05-22 18:37:12 +02:00
* @return ConstraintViolation
*/
2016-12-02 13:50:07 +01:00
protected function generateConstraintViolation($message)
2016-05-22 18:37:12 +02:00
{
return new ConstraintViolation($message, $message, [], null, '', null);
}
2016-12-02 13:50:07 +01:00
2016-05-22 18:37:12 +02:00
/**
2016-12-02 13:50:07 +01:00
* Generate a Violation.
*
* @param string $message The error message
* @param int $line The line of the violations
* @param int|null $key The column of the violations
2016-05-22 18:37:12 +02:00
*
* @return Violation
*/
2016-12-02 13:50:07 +01:00
protected function generateViolation($line, $key, ConstraintViolation $violation)
2016-05-22 18:37:12 +02:00
{
return new Violation($line, $key, $violation);
}
2016-05-22 23:22:12 +02:00
/**
2016-12-02 13:50:07 +01:00
* Get the true line number of an error.
*
* @param int $line
2016-05-22 23:22:12 +02:00
*
2016-12-02 13:50:07 +01:00
* @return int
2016-05-22 23:22:12 +02:00
*/
2016-12-02 13:50:07 +01:00
protected function getTrueLine($line)
2016-05-22 23:22:12 +02:00
{
if ($this->parser->getHasHeaders()) {
2016-12-02 13:50:07 +01:00
++$line;
2016-05-22 23:22:12 +02:00
}
return ++$line;
}
2016-12-02 13:50:07 +01:00
2016-05-22 23:22:12 +02:00
/**
2016-12-02 13:50:07 +01:00
* Get the true culumn number of an error.
*
* @param int $key
2016-05-22 23:22:12 +02:00
*
2016-12-02 13:50:07 +01:00
* @return int
2016-05-22 23:22:12 +02:00
*/
2016-12-02 13:50:07 +01:00
protected function getTrueColunm($key)
2016-05-22 23:22:12 +02:00
{
return ++$key;
}
2015-03-17 17:54:50 +01:00
}