Skip to content

Commit

Permalink
[Validator] Added support for static callbacks to Callback constraint
Browse files Browse the repository at this point in the history
  • Loading branch information
Bernhard Schussek committed Mar 25, 2011
1 parent 64bc211 commit 2a730bf
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions Constraints/CallbackValidator.php
Expand Up @@ -29,7 +29,13 @@ public function isValid($object, Constraint $constraint)
return true;
}

$methods = (array)$constraint->methods;
// has to be an array so that we can differentiate between callables
// and method names
if (!is_array($constraint->methods)) {
throw new UnexpectedTypeException($constraint->methods, 'array');
}

$methods = $constraint->methods;
$context = $this->context;

// save context state
Expand All @@ -39,11 +45,19 @@ public function isValid($object, Constraint $constraint)
$propertyPath = $context->getPropertyPath();

foreach ($methods as $method) {
if (!method_exists($object, $method)) {
throw new ConstraintDefinitionException(sprintf('Method "%s" targeted by Callback constraint does not exist', $method));
}
if (is_array($method)) {
if (!is_callable($method)) {
throw new ConstraintDefinitionException(sprintf('"%s::%s" targeted by Callback constraint is not a valid callable', $method[0], $method[1]));
}

$object->$method($context);
call_user_func($method, $object, $context);
} else {
if (!method_exists($object, $method)) {
throw new ConstraintDefinitionException(sprintf('Method "%s" targeted by Callback constraint does not exist', $method));
}

$object->$method($context);
}

// restore context state
$context->setCurrentClass($currentClass);
Expand Down

0 comments on commit 2a730bf

Please sign in to comment.