Created
May 9, 2017 07:08
-
-
Save ebuildy/32416d5bbd5b26ef82b15849c9616716 to your computer and use it in GitHub Desktop.
SupplyDataChain
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use Bizlunch\HelperBundle\Exception\ValidationException; | |
use Symfony\Component\Validator\Constraint; | |
use Symfony\Component\Validator\Exception\ValidatorException; | |
class SupplyDataChain | |
{ | |
private $value; | |
private $name; | |
/** @var \Symfony\Component\Validator\Validator\ValidatorInterface */ | |
private $validator; | |
public function __construct($validator) | |
{ | |
$this->validator = $validator; | |
} | |
public function setValue($bag, $key = null) | |
{ | |
$this->name = $key; | |
if (empty($key)) { | |
$this->value = $bag; | |
} else { | |
$this->value = $bag->get($key); | |
} | |
return $this; | |
} | |
public function transform($callable) | |
{ | |
$this->value = $callable($this->value); | |
return $this; | |
} | |
public function validate(Constraint $constraint, $name = null) | |
{ | |
$errors = $this->validator->validate($this->value, $constraint); | |
if (count($errors) > 0) | |
{ | |
throw new ValidationException($errors, empty($name) ? $this->name : $name); | |
} | |
return $this; | |
} | |
public function getValue() | |
{ | |
return $this->value; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment