Php doc
This commit is contained in:
parent
8f6b4afe5b
commit
93e94be9c4
|
@ -0,0 +1,20 @@
|
|||
<?php
|
||||
|
||||
use Deblan\Csv\CsvParser;
|
||||
use Deblan\CsvValidator\Validator;
|
||||
use Symfony\Component\Validator\Validation;
|
||||
use Symfony\Component\Validator\Constraints\Email;
|
||||
use Symfony\Component\Validator\Constraints\Date;
|
||||
|
||||
chdir(__DIR__);
|
||||
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
$parser = new CsvParser('lqdn.txt', ';', '');
|
||||
|
||||
$validator = new Validator($parser, Validation::createValidator());
|
||||
$validator->addFieldConstraint(1, new Email());
|
||||
$validator->validate();
|
||||
|
||||
var_dump($validator->isValid());
|
||||
var_dump($validator->getErrors());
|
|
@ -0,0 +1,47 @@
|
|||
ANDERSSON Max;max.andersson europarl.europa.eu
|
||||
BERGERON Joëlle;joelle.bergeron@europarl.europa.eu
|
||||
BOUTONNET Marie Christine;marie-christine.boutonnet@europarl.europa.eu
|
||||
CAVADA Jean Marie;jean-marie.cavada@europarl.europa.eu
|
||||
CHRYSOGONOS Kostas;kostas.chrysogonos@europarl.europa.eu
|
||||
COMODINI CACHIA Therese;therese.comodinicachia@europarl.europa.eu
|
||||
DELVAUX STEHRES Mady;mady.delvaux-stehres@europarl.europa.eu
|
||||
DUDA Andrzej Sebastian;andrzej.duda@europarl.europa.eu
|
||||
ESTARÀS FERRAGUT Rosa;rosa.estaras@europarl.europa.eu
|
||||
FERRARA Laura;laura.ferrara@europarl.europa.eu
|
||||
GASBARRA Enrico;enrico.gasbarra@europarl.europa.eu
|
||||
GERINGER de OEDENBERG Lidia Joannavv;lidiajoanna.geringerdeoedenberg@europarl.europa.eu
|
||||
HONEYBALL Mary;mary.honeyball@europarl.europa.eu
|
||||
KARIM Sajjad;sajjad.karim@europarl.europa.eu
|
||||
KÖSTER Dietmar;dietmar.koester@europarl.europa.eu
|
||||
LEBRETON Gilles;gilles.lebreton@europarl.europa.eu
|
||||
MARINHO E PINTO António;antonio.marinhoepinto@europarl.europa.eu
|
||||
MAŠTÁLKA Jiří;jiri.mastalka@europarl.europa.eu
|
||||
RADEV Emil;emil.radev@europarl.europa.eu
|
||||
REDA Julia;julia.reda@europarl.europa.eu
|
||||
REGNER Evelyn;evelyn.regner@europarl.europa.eu
|
||||
SVOBODA Pavel;pavel.svoboda@europarl.europa.eu
|
||||
SZÁJER József;jozsef.szajer@europarl.europa.eu
|
||||
VOSS Axel;axel.voss@europarl.europa.eu
|
||||
ZWIEFKA Tadeusz;tadeusz.zwiefka@europarl.europa.eu
|
||||
BORGHESIO Mario;mario.borghezio@europarl.europa.eu
|
||||
BUDA Daniel;daniel.buda@europarl.europa.eu
|
||||
CASTALDO Fabio Massimo;fabiomassimo.castaldo@europarl.europa.eu
|
||||
COFFERATI Sergio Gaetano;sergio.cofferati@europarl.europa.eu
|
||||
CORRAO Ignazio;ignazio.corrao@europarl.europa.eu
|
||||
CROWLEY Brian;brian.crowley@europarl.europa.eu
|
||||
DURAND Pascal;pascal.durand@europarl.europa.eu
|
||||
DZHAMBAZKI Angel;angel.dzhambazki@europarl.europa.eu
|
||||
GEBHARDT Evelyne;evelyne.gebhardt@europarl.europa.eu
|
||||
de GRANDES PASCUAL Luis;luis.degrandespascual@europarl.europa.eu
|
||||
GUTELAND Jytte;jytte.guteland@europarl.europa.eu
|
||||
HAUTALA Heidi;heidi.hautala@europarl.europa.eu
|
||||
KAUFMANN Sylvie-Yvonne;sylvia-yvonne.kaufmann@europarl.europa.eu
|
||||
LE GRIP Constance;constance.legrip@europarl.europa.eu
|
||||
NEGRESCU Victor;victor.negrescu@europarl.europa.eu
|
||||
NIEBLER Angelika;angelika.niebler@europarl.europa.eu
|
||||
ROZIÈRE Virginie;virginie.roziere@europarl.europa.eu
|
||||
TOTI Giovanni;giovanni.toti@europarl.europa.eu
|
||||
USPASKICH Viktor;viktor.uspaskich@europarl.europa.eu
|
||||
WIELAND Rainer;rainer.wieland@europarl.europa.eu
|
||||
WIKSTRÖM Cecilia;cecilia.wikstrom@europarl.europa.eu
|
||||
ŻÓŁTEK Stanisław;stanislawjozef.zoltek@europarl.europa.eu
|
|
@ -1,25 +0,0 @@
|
|||
<?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 }}';
|
||||
}
|
|
@ -1,99 +0,0 @@
|
|||
<?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;
|
||||
}
|
||||
}
|
|
@ -6,21 +6,50 @@ use Deblan\Csv\CsvParser;
|
|||
use Symfony\Component\Validator\Constraint;
|
||||
use Symfony\Component\Validator\ConstraintViolationList;
|
||||
use Symfony\Component\Validator\Validator\RecursiveValidator;
|
||||
use Symfony\Component\Validator\ConstraintViolation;
|
||||
|
||||
/**
|
||||
* Class Validator
|
||||
* @author Simon Vieille <simon@deblan.fr>
|
||||
*/
|
||||
class Validator
|
||||
{
|
||||
/**
|
||||
* @var CsvParser
|
||||
*/
|
||||
protected $parser;
|
||||
|
||||
/**
|
||||
* @var RecursiveValidator
|
||||
*/
|
||||
protected $validator;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $fieldsConstraints = [];
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $dataConstraints = [];
|
||||
|
||||
/**
|
||||
* @var boolean
|
||||
*/
|
||||
protected $hasValidate = false;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $errors = [];
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param CsvParser $parser
|
||||
* @param RecursiveValidator $validator
|
||||
*/
|
||||
public function __construct(CsvParser $parser, RecursiveValidator $validator)
|
||||
{
|
||||
$this->parser = $parser;
|
||||
|
@ -28,6 +57,13 @@ class Validator
|
|||
$this->validator = $validator;
|
||||
}
|
||||
|
||||
/**
|
||||
* Append a constraint to a specific column
|
||||
*
|
||||
* @param $key The column number
|
||||
* @param Constraint $constraint The constraint
|
||||
* @return Validator
|
||||
*/
|
||||
public function addFieldConstraint($key, Constraint $constraint)
|
||||
{
|
||||
if (!array_key_exists($key, $this->fieldsConstraints)) {
|
||||
|
@ -39,13 +75,23 @@ class Validator
|
|||
return $this;
|
||||
}
|
||||
|
||||
public function addDataContraint(Constraint $constraint)
|
||||
/**
|
||||
* Append a constraint to a specific line
|
||||
*
|
||||
* @param $key The column number
|
||||
* @param Constraint $constraint The constraint
|
||||
* @return Validator
|
||||
*/
|
||||
public function addDataConstraint(Constraint $constraint)
|
||||
{
|
||||
$this->dataConstraints[] = $constraint;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Run the validation
|
||||
*/
|
||||
public function validate()
|
||||
{
|
||||
if ($this->hasValidate) {
|
||||
|
@ -75,27 +121,35 @@ class Validator
|
|||
$this->hasValidate = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add violations
|
||||
*
|
||||
* @param ConstraintViolationList $violations
|
||||
* @param integer $line The line of the violations
|
||||
* @param integer|null $key The column of the violations
|
||||
*/
|
||||
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;
|
||||
$this->errors[] = new Violation($line + 1, $key, $violation);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
protected function mergeErrorMessage($message, $line, $key = null)
|
||||
{
|
||||
if (!array_key_exists($line, $this->errors)) {
|
||||
|
@ -106,11 +160,16 @@ class Validator
|
|||
$key++;
|
||||
}
|
||||
|
||||
$message = sprintf('Line %d%s: %s', $line + 1, $key !== null ? ', field '.($key) : '', $message);
|
||||
|
||||
$this->errors[$line][] = $message;
|
||||
$violation = new ConstraintViolation($message, $message, [], null, '', null);
|
||||
$this->errors[] = new Violation($line + 1, $key, $violation);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the validation status
|
||||
*
|
||||
* @return boolean
|
||||
* @throw RuntimeException No validation yet
|
||||
*/
|
||||
public function isValid()
|
||||
{
|
||||
if (!$this->hasValidate) {
|
||||
|
@ -120,6 +179,11 @@ class Validator
|
|||
return empty($this->errors);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the errors
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getErrors()
|
||||
{
|
||||
return $this->errors;
|
||||
|
|
|
@ -0,0 +1,100 @@
|
|||
<?php
|
||||
|
||||
namespace Deblan\CsvValidator;
|
||||
|
||||
use Symfony\Component\Validator\ConstraintViolation;
|
||||
|
||||
/**
|
||||
* Class Violation
|
||||
* @author Simon Vieille <simon@deblan.fr>
|
||||
*/
|
||||
class Violation
|
||||
{
|
||||
/**
|
||||
* @var integer
|
||||
*/
|
||||
protected $line;
|
||||
|
||||
/**
|
||||
* @var integer
|
||||
*/
|
||||
protected $column;
|
||||
|
||||
/**
|
||||
* @var ConstraintViolation
|
||||
*/
|
||||
protected $violation;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param integer $line The line of the violation
|
||||
* @param integer $column The column of the violation
|
||||
* @param ConstraintViolation $violation The violation
|
||||
*/
|
||||
public function __construct($line, $column, ConstraintViolation $violation)
|
||||
{
|
||||
$this->setLine($line)
|
||||
->setColumn($column)
|
||||
->setViolation($violation);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $line
|
||||
* @return Violation
|
||||
*/
|
||||
public function setLine($line)
|
||||
{
|
||||
$this->line = (int) $line;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int $line
|
||||
*/
|
||||
public function getLine()
|
||||
{
|
||||
return $this->line;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $column
|
||||
* @return Violation
|
||||
*/
|
||||
public function setColumn($column)
|
||||
{
|
||||
$this->column = (int) $column;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int $column
|
||||
*/
|
||||
public function getColumn()
|
||||
{
|
||||
return $this->column;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ConstraintViolation $violation
|
||||
* @return
|
||||
*/
|
||||
public function setViolation(ConstraintViolation $violation)
|
||||
{
|
||||
$this->violation = $violation;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ConstraintViolation $violation
|
||||
*/
|
||||
public function getViolation()
|
||||
{
|
||||
return $this->violation;
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue