Skip to content
This repository has been archived by the owner on Sep 22, 2019. It is now read-only.

Commit

Permalink
Updated VO.
Browse files Browse the repository at this point in the history
  • Loading branch information
cn0047 committed Oct 19, 2016
1 parent 51607d1 commit b56a869
Showing 1 changed file with 33 additions and 3 deletions.
36 changes: 33 additions & 3 deletions src/ValueObject/ValueObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace ValueObject;

use Symfony\Component\Validator\ConstraintViolation;
use Symfony\Component\Validator\ConstraintViolationList;
use Symfony\Component\Validator\Validation;
use ValueObject\Exception\ValidationException;

Expand All @@ -19,6 +21,7 @@ abstract protected function getRules();
public function __construct(array $params)
{
$this->validate($params);
$this->afterValidation($params);

if (0 !== count($this->errors)) {
throw new ValidationException($this->errors);
Expand Down Expand Up @@ -47,18 +50,45 @@ final private function validate(array $params)
$constraints[] = new $constraintClassName($options);
}

$violations = $validator->validate($params[$paramName], $constraints);
if (array_key_exists($paramName, $params)) {

$violations = $validator->validate($params[$paramName], $constraints);

if (0 !== count($violations)) {
$this->errors[$paramName] = $violations;
}

} else {

$this->setError($paramName, 'This parameter is required.');

if (0 !== count($violations)) {
$this->errors[$paramName] = $violations;
}

}
}

public function setError($paramName, $message)
{
$violation = new ConstraintViolation($message, '', [], '', $paramName, null);
if (array_key_exists($paramName, $this->errors)) {
$this->errors[$paramName]->add($violation);
} else {
$this->errors[$paramName] = new ConstraintViolationList([$violation]);
}
}

public function afterValidation(array $params)
{}

public function __call(string $name, array $arguments )
{
$paramName = lcfirst(substr($name, 3));

return $this->params[$paramName];
}

public function toArray()
{
return $this->params;
}
}

0 comments on commit b56a869

Please sign in to comment.