PSR2 compliance

This commit is contained in:
Simon Vieille 2016-12-02 13:50:07 +01:00
parent d5b0c29c23
commit 7de3c8a9e8
2 changed files with 75 additions and 64 deletions

View file

@ -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;
@ -44,14 +45,14 @@ class Validator
* @var array * @var array
*/ */
protected $errors = []; protected $errors = [];
/** /**
* @var array * @var array
*/ */
protected $expectedLegend = []; protected $expectedLegend = [];
/** /**
* Constructor * Constructor.
* *
* @param RecursiveValidator $validator * @param RecursiveValidator $validator
*/ */
@ -59,16 +60,17 @@ class Validator
{ {
if ($validator === null) { if ($validator === null) {
$validator = Validation::createValidator(); $validator = Validation::createValidator();
} }
$this->validator = $validator; $this->validator = $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,12 +99,13 @@ 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)
{ {
$this->expectedLegend = $legend; $this->expectedLegend = $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,23 +123,21 @@ 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;
} }
$this->validateLegend(); $this->validateLegend();
$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()
{ {
if (!$this->parser->getHasLegend()) { if (!$this->parser->getHasLegend()) {
return; return;
@ -143,16 +146,16 @@ class Validator
if (empty($this->expectedLegend)) { if (empty($this->expectedLegend)) {
return; return;
} }
if ($this->parser->getLegend() !== $this->expectedLegend) { if ($this->parser->getLegend() !== $this->expectedLegend) {
$this->mergeErrorMessage('Invalid legend.', 1); $this->mergeErrorMessage('Invalid legend.', 1);
} }
} }
/** /**
* Validates datas * Validates datas.
*/ */
protected function validateDatas() protected function validateDatas()
{ {
if (empty($this->dataConstraints)) { if (empty($this->dataConstraints)) {
return; return;
@ -168,21 +171,21 @@ class Validator
} }
/** /**
* Validates fields * Validates fields.
*/ */
protected function validateFields() protected function validateFields()
{ {
if (empty($this->fieldConstraints)) { if (empty($this->fieldConstraints)) {
return; return;
} }
foreach ($this->parser->getDatas() as $line => $data) { foreach ($this->parser->getDatas() as $line => $data) {
foreach ($this->fieldConstraints as $key => $constraints) { foreach ($this->fieldConstraints as $key => $constraints) {
if (!isset($data[$key])) { if (!isset($data[$key])) {
$column = $this->getTrueColunm($key); $column = $this->getTrueColunm($key);
$this->mergeErrorMessage( $this->mergeErrorMessage(
sprintf('Field "%s" does not exist.', $column), sprintf('Field "%s" does not exist.', $column),
$this->getTrueLine($line), $this->getTrueLine($line),
$column $column
); );
} else { } else {
@ -190,8 +193,8 @@ class Validator
$violations = $this->validator->validate($data[$key], $constraint); $violations = $this->validator->validate($data[$key], $constraint);
$this->mergeViolationsMessages( $this->mergeViolationsMessages(
$violations, $violations,
$this->getTrueLine($line), $this->getTrueLine($line),
$this->getTrueColunm($key) $this->getTrueColunm($key)
); );
} }
@ -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,51 +260,55 @@ 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)
{ {
return new ConstraintViolation($message, $message, [], null, '', null); return new ConstraintViolation($message, $message, [], null, '', null);
} }
/** /**
* Generate a Violation * 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
* *
* @param string $message The error message
* @param integer $line The line of the violations
* @param integer|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)
{ {
return new Violation($line, $key, $violation); return new Violation($line, $key, $violation);
} }
/** /**
* 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)
{ {
return ++$key; return ++$key;
} }

View file

@ -5,31 +5,32 @@ 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;
/** /**
* @var ConstraintViolation * @var ConstraintViolation
*/ */
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)