forked from deblan/csv-validator
PSR2 compliance
This commit is contained in:
parent
d5b0c29c23
commit
7de3c8a9e8
|
@ -10,7 +10,8 @@ use Symfony\Component\Validator\ConstraintViolation;
|
||||||
use Symfony\Component\Validator\Validation;
|
use Symfony\Component\Validator\Validation;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class Validator
|
* Class Validator.
|
||||||
|
*
|
||||||
* @author Simon Vieille <simon@deblan.fr>
|
* @author Simon Vieille <simon@deblan.fr>
|
||||||
*/
|
*/
|
||||||
class Validator
|
class Validator
|
||||||
|
@ -36,7 +37,7 @@ class Validator
|
||||||
protected $dataConstraints = [];
|
protected $dataConstraints = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var boolean
|
* @var bool
|
||||||
*/
|
*/
|
||||||
protected $hasValidate = false;
|
protected $hasValidate = false;
|
||||||
|
|
||||||
|
@ -51,7 +52,7 @@ class Validator
|
||||||
protected $expectedLegend = [];
|
protected $expectedLegend = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor
|
* Constructor.
|
||||||
*
|
*
|
||||||
* @param RecursiveValidator $validator
|
* @param RecursiveValidator $validator
|
||||||
*/
|
*/
|
||||||
|
@ -65,10 +66,11 @@ class Validator
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Append a constraint to a specific column
|
* Append a constraint to a specific column.
|
||||||
*
|
*
|
||||||
* @param integer $key The column number
|
* @param int $key The column number
|
||||||
* @param Constraint $constraint The constraint
|
* @param Constraint $constraint The constraint
|
||||||
|
*
|
||||||
* @return Validator
|
* @return Validator
|
||||||
*/
|
*/
|
||||||
public function addFieldConstraint($key, Constraint $constraint)
|
public function addFieldConstraint($key, Constraint $constraint)
|
||||||
|
@ -83,9 +85,10 @@ class Validator
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Append a constraint to a specific line
|
* Append a constraint to a specific line.
|
||||||
*
|
*
|
||||||
* @param Constraint $constraint The constraint
|
* @param Constraint $constraint The constraint
|
||||||
|
*
|
||||||
* @return Validator
|
* @return Validator
|
||||||
*/
|
*/
|
||||||
public function addDataConstraint(Constraint $constraint)
|
public function addDataConstraint(Constraint $constraint)
|
||||||
|
@ -96,9 +99,10 @@ class Validator
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the expected legend
|
* Set the expected legend.
|
||||||
*
|
*
|
||||||
* @param array $legend Expected legend
|
* @param array $legend Expected legend
|
||||||
|
*
|
||||||
* @return Validator
|
* @return Validator
|
||||||
*/
|
*/
|
||||||
public function setExpectedLegend(array $legend)
|
public function setExpectedLegend(array $legend)
|
||||||
|
@ -109,7 +113,8 @@ class Validator
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Run the validation
|
* Run the validation.
|
||||||
|
*
|
||||||
* @param CsvParser $parser
|
* @param CsvParser $parser
|
||||||
*/
|
*/
|
||||||
public function validate(CsvParser $parser)
|
public function validate(CsvParser $parser)
|
||||||
|
@ -118,8 +123,7 @@ class Validator
|
||||||
$this->parser = $parser;
|
$this->parser = $parser;
|
||||||
$this->parser->parse();
|
$this->parser->parse();
|
||||||
$this->errors = [];
|
$this->errors = [];
|
||||||
}
|
} elseif ($this->hasValidate) {
|
||||||
elseif ($this->hasValidate) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -127,12 +131,11 @@ class Validator
|
||||||
$this->validateDatas();
|
$this->validateDatas();
|
||||||
$this->validateFields();
|
$this->validateFields();
|
||||||
|
|
||||||
|
|
||||||
$this->hasValidate = true;
|
$this->hasValidate = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Validates the legend
|
* Validates the legend.
|
||||||
*/
|
*/
|
||||||
protected function validateLegend()
|
protected function validateLegend()
|
||||||
{
|
{
|
||||||
|
@ -150,7 +153,7 @@ class Validator
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Validates datas
|
* Validates datas.
|
||||||
*/
|
*/
|
||||||
protected function validateDatas()
|
protected function validateDatas()
|
||||||
{
|
{
|
||||||
|
@ -168,7 +171,7 @@ class Validator
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Validates fields
|
* Validates fields.
|
||||||
*/
|
*/
|
||||||
protected function validateFields()
|
protected function validateFields()
|
||||||
{
|
{
|
||||||
|
@ -201,11 +204,11 @@ class Validator
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add violations
|
* Add violations.
|
||||||
*
|
*
|
||||||
* @param ConstraintViolationList $violations
|
* @param ConstraintViolationList $violations
|
||||||
* @param integer $line The line of the violations
|
* @param int $line The line of the violations
|
||||||
* @param integer|null $key The column of the violations
|
* @param int|null $key The column of the violations
|
||||||
*/
|
*/
|
||||||
protected function mergeViolationsMessages(ConstraintViolationList $violations, $line, $key = null)
|
protected function mergeViolationsMessages(ConstraintViolationList $violations, $line, $key = null)
|
||||||
{
|
{
|
||||||
|
@ -219,11 +222,11 @@ class Validator
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create and append a violation from a string error
|
* Create and append a violation from a string error.
|
||||||
*
|
*
|
||||||
* @param string $message The error message
|
* @param string $message The error message
|
||||||
* @param integer $line The line of the violations
|
* @param int $line The line of the violations
|
||||||
* @param integer|null $key The column of the violations
|
* @param int|null $key The column of the violations
|
||||||
*/
|
*/
|
||||||
protected function mergeErrorMessage($message, $line, $key = null)
|
protected function mergeErrorMessage($message, $line, $key = null)
|
||||||
{
|
{
|
||||||
|
@ -232,9 +235,9 @@ class Validator
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the validation status
|
* Returns the validation status.
|
||||||
*
|
*
|
||||||
* @return boolean
|
* @return bool
|
||||||
* @throw RuntimeException No validation yet
|
* @throw RuntimeException No validation yet
|
||||||
*/
|
*/
|
||||||
public function isValid()
|
public function isValid()
|
||||||
|
@ -247,7 +250,7 @@ class Validator
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the errors
|
* Returns the errors.
|
||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
|
@ -257,9 +260,10 @@ class Validator
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generate a ConstraintViolation
|
* Generate a ConstraintViolation.
|
||||||
*
|
*
|
||||||
* @param string $message The error message
|
* @param string $message The error message
|
||||||
|
*
|
||||||
* @return ConstraintViolation
|
* @return ConstraintViolation
|
||||||
*/
|
*/
|
||||||
protected function generateConstraintViolation($message)
|
protected function generateConstraintViolation($message)
|
||||||
|
@ -268,11 +272,12 @@ class Validator
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generate a Violation
|
* Generate a Violation.
|
||||||
*
|
*
|
||||||
* @param string $message The error message
|
* @param string $message The error message
|
||||||
* @param integer $line The line of the violations
|
* @param int $line The line of the violations
|
||||||
* @param integer|null $key The column of the violations
|
* @param int|null $key The column of the violations
|
||||||
|
*
|
||||||
* @return Violation
|
* @return Violation
|
||||||
*/
|
*/
|
||||||
protected function generateViolation($line, $key, ConstraintViolation $violation)
|
protected function generateViolation($line, $key, ConstraintViolation $violation)
|
||||||
|
@ -281,25 +286,27 @@ class Validator
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the true line number of an error
|
* Get the true line number of an error.
|
||||||
*
|
*
|
||||||
* @param integer $line
|
* @param int $line
|
||||||
* @return integer
|
*
|
||||||
|
* @return int
|
||||||
*/
|
*/
|
||||||
protected function getTrueLine($line)
|
protected function getTrueLine($line)
|
||||||
{
|
{
|
||||||
if ($this->parser->getHasLegend()) {
|
if ($this->parser->getHasLegend()) {
|
||||||
$line++;
|
++$line;
|
||||||
}
|
}
|
||||||
|
|
||||||
return ++$line;
|
return ++$line;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the true culumn number of an error
|
* Get the true culumn number of an error.
|
||||||
*
|
*
|
||||||
* @param integer $key
|
* @param int $key
|
||||||
* @return integer
|
*
|
||||||
|
* @return int
|
||||||
*/
|
*/
|
||||||
protected function getTrueColunm($key)
|
protected function getTrueColunm($key)
|
||||||
{
|
{
|
||||||
|
|
|
@ -5,18 +5,19 @@ namespace Deblan\CsvValidator;
|
||||||
use Symfony\Component\Validator\ConstraintViolation;
|
use Symfony\Component\Validator\ConstraintViolation;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class Violation
|
* Class Violation.
|
||||||
|
*
|
||||||
* @author Simon Vieille <simon@deblan.fr>
|
* @author Simon Vieille <simon@deblan.fr>
|
||||||
*/
|
*/
|
||||||
class Violation
|
class Violation
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @var integer
|
* @var int
|
||||||
*/
|
*/
|
||||||
protected $line;
|
protected $line;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var integer
|
* @var int
|
||||||
*/
|
*/
|
||||||
protected $column;
|
protected $column;
|
||||||
|
|
||||||
|
@ -26,10 +27,10 @@ class Violation
|
||||||
protected $violation;
|
protected $violation;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor
|
* Constructor.
|
||||||
*
|
*
|
||||||
* @param integer $line The line of the violation
|
* @param int $line The line of the violation
|
||||||
* @param integer $column The column of the violation
|
* @param int $column The column of the violation
|
||||||
* @param ConstraintViolation $violation The violation
|
* @param ConstraintViolation $violation The violation
|
||||||
*/
|
*/
|
||||||
public function __construct($line, $column, ConstraintViolation $violation)
|
public function __construct($line, $column, ConstraintViolation $violation)
|
||||||
|
@ -41,6 +42,7 @@ class Violation
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param int $line
|
* @param int $line
|
||||||
|
*
|
||||||
* @return Violation
|
* @return Violation
|
||||||
*/
|
*/
|
||||||
public function setLine($line)
|
public function setLine($line)
|
||||||
|
@ -60,6 +62,7 @@ class Violation
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param int $column
|
* @param int $column
|
||||||
|
*
|
||||||
* @return Violation
|
* @return Violation
|
||||||
*/
|
*/
|
||||||
public function setColumn($column)
|
public function setColumn($column)
|
||||||
|
@ -83,6 +86,7 @@ class Violation
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param ConstraintViolation $violation
|
* @param ConstraintViolation $violation
|
||||||
|
*
|
||||||
* @return Violation
|
* @return Violation
|
||||||
*/
|
*/
|
||||||
public function setViolation(ConstraintViolation $violation)
|
public function setViolation(ConstraintViolation $violation)
|
||||||
|
|
Loading…
Reference in a new issue