Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ebuildy/32416d5bbd5b26ef82b15849c9616716 to your computer and use it in GitHub Desktop.
Save ebuildy/32416d5bbd5b26ef82b15849c9616716 to your computer and use it in GitHub Desktop.
SupplyDataChain
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