Skip to content

Commit

Permalink
feature #16909 Allows access to payload in callback validator (conrad…
Browse files Browse the repository at this point in the history
…kleinespel)

This PR was merged into the 3.1-dev branch.

Discussion
----------

Allows access to payload in callback validator

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #15092
| License       | MIT
| Doc PR        | none

Commits
-------

26cd91d allows access to payload in callback validator
  • Loading branch information
fabpot committed Jan 25, 2016
2 parents e23804e + 26cd91d commit 36c2961
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
Expand Up @@ -34,7 +34,7 @@ public function validate($object, Constraint $constraint)

$method = $constraint->callback;
if ($method instanceof \Closure) {
$method($object, $this->context);
$method($object, $this->context, $constraint->payload);
} elseif (is_array($method)) {
if (!is_callable($method)) {
if (isset($method[0]) && is_object($method[0])) {
Expand All @@ -43,7 +43,7 @@ public function validate($object, Constraint $constraint)
throw new ConstraintDefinitionException(sprintf('%s targeted by Callback constraint is not a valid callable', json_encode($method)));
}

call_user_func($method, $object, $this->context);
call_user_func($method, $object, $this->context, $constraint->payload);
} elseif (null !== $object) {
if (!method_exists($object, $method)) {
throw new ConstraintDefinitionException(sprintf('Method "%s" targeted by Callback constraint does not exist in class %s', $method, get_class($object)));
Expand All @@ -52,9 +52,9 @@ public function validate($object, Constraint $constraint)
$reflMethod = new \ReflectionMethod($object, $method);

if ($reflMethod->isStatic()) {
$reflMethod->invoke(null, $object, $this->context);
$reflMethod->invoke(null, $object, $this->context, $constraint->payload);
} else {
$reflMethod->invoke($object, $this->context);
$reflMethod->invoke($object, $this->context, $constraint->payload);
}
}
}
Expand Down
Expand Up @@ -226,4 +226,28 @@ public function testAnnotationInvocationMultiValued()

$this->assertEquals(new Callback(array(__CLASS__.'_Class', 'validateCallback')), $constraint);
}

public function testPayloadIsPassedToCallback()
{
$object = new \stdClass();
$payloadCopy = null;

$constraint = new Callback([
'callback' => function($object, ExecutionContextInterface $constraint, $payload) use (&$payloadCopy) {
$payloadCopy = $payload;
},
'payload' => 'Hello world!',
]);
$this->validator->validate($object, $constraint);
$this->assertEquals('Hello world!', $payloadCopy);

$payloadCopy = null;
$constraint = new Callback([
'callback' => function($object, ExecutionContextInterface $constraint, $payload) use (&$payloadCopy) {
$payloadCopy = $payload;
},
]);
$this->validator->validate($object, $constraint);
$this->assertNull($payloadCopy);
}
}

0 comments on commit 36c2961

Please sign in to comment.