init
This commit is contained in:
parent
ef429702d0
commit
cce1ff1309
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
*.swp
|
||||
tags
|
||||
vendor
|
21
composer.json
Normal file
21
composer.json
Normal file
|
@ -0,0 +1,21 @@
|
|||
{
|
||||
"name": "deblan/csv-validator",
|
||||
"description": "CSV validator library",
|
||||
"license": "BSD-2-Clause",
|
||||
"authors": [
|
||||
{
|
||||
"name": "Simon Vieille",
|
||||
"email": "simon@deblan.fr"
|
||||
}
|
||||
],
|
||||
"autoload": {
|
||||
"psr-0": {
|
||||
"": "src/"
|
||||
}
|
||||
},
|
||||
"minimum-stability": "stable",
|
||||
"require": {
|
||||
"symfony/validator": "2.*",
|
||||
"deblan/csv": "dev-master"
|
||||
}
|
||||
}
|
18
phpunit.xml
Normal file
18
phpunit.xml
Normal file
|
@ -0,0 +1,18 @@
|
|||
<phpunit
|
||||
backupGlobals = "false"
|
||||
backupStaticAttributes = "false"
|
||||
colors = "true"
|
||||
convertErrorsToExceptions = "true"
|
||||
convertNoticesToExceptions = "true"
|
||||
convertWarningsToExceptions = "true"
|
||||
processIsolation = "false"
|
||||
stopOnFailure = "false"
|
||||
syntaxCheck = "false"
|
||||
bootstrap = "vendor/autoload.php" >
|
||||
|
||||
<testsuites>
|
||||
<testsuite name="Deblan CSV Test Suite">
|
||||
<directory>tests/</directory>
|
||||
</testsuite>
|
||||
</testsuites>
|
||||
</phpunit>
|
123
src/Deblan/CsvValidator/Validator.php
Normal file
123
src/Deblan/CsvValidator/Validator.php
Normal file
|
@ -0,0 +1,123 @@
|
|||
<?php
|
||||
|
||||
namespace Deblan\CsvValidator;
|
||||
|
||||
use Deblan\Csv\CsvParser;
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
use Symfony\Component\Validator\Validator\LegacyValidator;
|
||||
use Symfony\Component\Validator\ConstraintViolationList;
|
||||
|
||||
class Validator
|
||||
{
|
||||
protected $parser;
|
||||
|
||||
protected $validator;
|
||||
|
||||
protected $fieldsConstraints = [];
|
||||
|
||||
protected $dataConstraints = [];
|
||||
|
||||
protected $hasValidate = false;
|
||||
|
||||
protected $errors = [];
|
||||
|
||||
public function __construct(CsvParser $parser, LegacyValidator $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 (!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…
Reference in a new issue