First version
This commit is contained in:
parent
7622c3f5fa
commit
8f6b4afe5b
5 changed files with 13 additions and 7 deletions
25
src/Deblan/CsvValidator/Constraints/Csv.php
Normal file
25
src/Deblan/CsvValidator/Constraints/Csv.php
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
<?php
|
||||
|
||||
namespace Deblan\CsvValidator\Constraints;
|
||||
|
||||
use Symfony\Component\Validator\Constraints\File;
|
||||
|
||||
class Csv extends File
|
||||
{
|
||||
const FIELD_NOT_VALID = 10;
|
||||
const FIELD_NOT_DETECTED = 11;
|
||||
const LINE_NOT_VALID = 12;
|
||||
|
||||
protected static $errorNames = [
|
||||
self::FIELD_NOT_VALID = 'FIELD_NOT_VALID',
|
||||
self::FIELD_NOT_DETECTED = 'FIELD_NOT_DETECTED',
|
||||
self::LINE_NOT_VALID = 'LINE_NOT_VALID',
|
||||
];
|
||||
|
||||
public $fieldsConstraints = [];
|
||||
public $lineConstraints = [];
|
||||
|
||||
public $fieldNotValidMessage = 'The field {{ field }} of the line {{ line }} is not valid. {{ message }}';
|
||||
public $fieldNotDetectedMessage = 'The field {{ field }} of the line {{ line }} is missing.';
|
||||
public $lineNotValidMessage = 'The line {{ line }} is not valid. {{ message }}';
|
||||
}
|
||||
99
src/Deblan/CsvValidator/Constraints/CsvValidator.php
Normal file
99
src/Deblan/CsvValidator/Constraints/CsvValidator.php
Normal file
|
|
@ -0,0 +1,99 @@
|
|||
<?php
|
||||
|
||||
namespace Deblan\CsvValidator\Constraints;
|
||||
|
||||
use Symfony\Component\Validator\Constraints\FileValidator;
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
use Deblan\Csv\CsvParser;
|
||||
|
||||
class CsvValidator extends FileValidator
|
||||
{
|
||||
public function validate($value, Constraint $constraint)
|
||||
{
|
||||
if (!$constraint instanceof Csv) {
|
||||
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Csv');
|
||||
}
|
||||
|
||||
$violations = count($this->context->getViolations());
|
||||
|
||||
parent::validate($value, $constraint);
|
||||
|
||||
if (null === $constraint->parser) {
|
||||
throw new \RuntimeException('You must set a CSV parser.');
|
||||
}
|
||||
|
||||
if (!is_object($constraint->parser) || !($value instanceof CsvParser)) {
|
||||
throw new \RuntimeException('You must be an instance of Deblan\\Csv\\CsvParser.');
|
||||
}
|
||||
|
||||
if (empty($constraint->fieldsConstraints) && empty($constraint->lineConstraints)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$context = $this->context;
|
||||
$parser = $constraint->parser;
|
||||
$parser->setFilename($value);
|
||||
$parser->parse();
|
||||
|
||||
foreach ($parser->getDatas() as $line => $data) {
|
||||
if ($parser->getHasLegend() && $line === 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach ($constraint->lineConstraints as $lineConstraint) {
|
||||
$violations = $this->getConstraintViolations($context, $data, $lineConstraint);
|
||||
|
||||
foreach ($violations as $violation) {
|
||||
$this->buildViolationInContext($context, $constraint->lineNotValidMessage)
|
||||
->setParameter('{{ line }}', $line + 1)
|
||||
->setParameter('{{ message }}', $violation->getMessage())
|
||||
->setInvalidValue(null)
|
||||
->setCode(Csv::LINE_NOT_VALID)
|
||||
->addViolation();
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($constraint->fieldsConstraints as $field => $fieldConstraints) {
|
||||
if (!array_key_exists($field, $line)) {
|
||||
$this->buildViolationInContext($context, $constraint->fieldNotDetectedMessage)
|
||||
->setParameter('{{ field }}', ctype_digit($field) ? $field + 1 : $field)
|
||||
->setParameter('{{ line }}', $line + 1)
|
||||
->setParameter('{{ message }}', $violation->getMessage())
|
||||
->setInvalidValue(null)
|
||||
->setCode(Csv::FIELD_NOT_DETECTED)
|
||||
->addViolation();
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach ($fieldConstraints as $fieldConstraint) {
|
||||
$violations = $this->getConstraintViolations($context, $data[$field], $fieldConstraint);
|
||||
|
||||
foreach ($violations as $violation) {
|
||||
$this->buildViolationInContext($context, $constraint->fieldNotValidMessage)
|
||||
->setParameter('{{ field }}', ctype_digit($field) ? $field + 1 : $field)
|
||||
->setParameter('{{ line }}', $k + 1)
|
||||
->setParameter('{{ message }}', $violation->getMessage())
|
||||
->setInvalidValue(null)
|
||||
->setCode(Csv::FIELD_NOT_VALID)
|
||||
->addViolation();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected function getConstraintViolations($context, $data, Constraint $constraint)
|
||||
{
|
||||
if ($context instanceof ExecutionContextInterface) {
|
||||
$violations = $context->getValidator()
|
||||
->inContext($context)
|
||||
->validate($data, $constraint);
|
||||
} else {
|
||||
// 2.4 API
|
||||
$violations = $context->validateValue($data, $constraint);
|
||||
}
|
||||
|
||||
return $violations;
|
||||
}
|
||||
}
|
||||
127
src/Deblan/CsvValidator/Validator.php
Normal file
127
src/Deblan/CsvValidator/Validator.php
Normal file
|
|
@ -0,0 +1,127 @@
|
|||
<?php
|
||||
|
||||
namespace Deblan\CsvValidator;
|
||||
|
||||
use Deblan\Csv\CsvParser;
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
use Symfony\Component\Validator\ConstraintViolationList;
|
||||
use Symfony\Component\Validator\Validator\RecursiveValidator;
|
||||
|
||||
class Validator
|
||||
{
|
||||
protected $parser;
|
||||
|
||||
protected $validator;
|
||||
|
||||
protected $fieldsConstraints = [];
|
||||
|
||||
protected $dataConstraints = [];
|
||||
|
||||
protected $hasValidate = false;
|
||||
|
||||
protected $errors = [];
|
||||
|
||||
public function __construct(CsvParser $parser, RecursiveValidator $validator)
|
||||
{
|
||||
$this->parser = $parser;
|
||||
$this->parser->parse();
|
||||
$this->validator = $validator;
|
||||
}
|
||||
|
||||
public function addFieldConstraint($key, Constraint $constraint)
|
||||
{
|
||||
if (!array_key_exists($key, $this->fieldsConstraints)) {
|
||||
$this->fieldsConstraints[$key] = [];
|
||||
}
|
||||
|
||||
$this->fieldsConstraints[$key][] = $constraint;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addDataContraint(Constraint $constraint)
|
||||
{
|
||||
$this->dataConstraints[] = $constraint;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
protected function mergeViolationsMessages(ConstraintViolationList $violations, $line, $key = null)
|
||||
{
|
||||
if (count($violations) === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!array_key_exists($line, $this->errors)) {
|
||||
$this->errors[$line] = [];
|
||||
}
|
||||
|
||||
if (is_int($key)) {
|
||||
$key++;
|
||||
}
|
||||
|
||||
foreach ($violations as $violation) {
|
||||
$message = sprintf('Line %d%s: %s', $line + 1, $key !== null ? ', field '.($key) : '', $violation->getMessage());
|
||||
|
||||
$this->errors[$line][] = $message;
|
||||
}
|
||||
}
|
||||
|
||||
protected function mergeErrorMessage($message, $line, $key = null)
|
||||
{
|
||||
if (!array_key_exists($line, $this->errors)) {
|
||||
$this->errors[$line] = [];
|
||||
}
|
||||
|
||||
if (is_int($key)) {
|
||||
$key++;
|
||||
}
|
||||
|
||||
$message = sprintf('Line %d%s: %s', $line + 1, $key !== null ? ', field '.($key) : '', $message);
|
||||
|
||||
$this->errors[$line][] = $message;
|
||||
}
|
||||
|
||||
public function isValid()
|
||||
{
|
||||
if (!$this->hasValidate) {
|
||||
throw new \RuntimeException('You must validate before.');
|
||||
}
|
||||
|
||||
return empty($this->errors);
|
||||
}
|
||||
|
||||
public function getErrors()
|
||||
{
|
||||
return $this->errors;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue