[Validator] Added getObject() to ExecutionContextInterface
This commit is contained in:
parent
9b204c9354
commit
c5629bb7ee
@ -232,6 +232,14 @@ class ExecutionContext implements ExecutionContextInterface
|
||||
return $this->value;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getObject()
|
||||
{
|
||||
return $this->object;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
|
@ -99,6 +99,20 @@ interface ExecutionContextInterface extends LegacyExecutionContextInterface
|
||||
*/
|
||||
public function getValidator();
|
||||
|
||||
/**
|
||||
* Returns the currently validated object.
|
||||
*
|
||||
* If the validator is currently validating a class constraint, the
|
||||
* object of that class is returned. If it is a validating a property or
|
||||
* getter constraint, the object that the property/getter belongs to is
|
||||
* returned.
|
||||
*
|
||||
* In other cases, null is returned.
|
||||
*
|
||||
* @return object|null The currently validated object or null.
|
||||
*/
|
||||
public function getObject();
|
||||
|
||||
/**
|
||||
* Sets the currently validated value.
|
||||
*
|
||||
|
@ -658,4 +658,24 @@ abstract class Abstract2Dot5ApiTest extends AbstractValidatorTest
|
||||
{
|
||||
$this->validate('Foobar');
|
||||
}
|
||||
|
||||
public function testAccessCurrentObject()
|
||||
{
|
||||
$test = $this;
|
||||
$called = false;
|
||||
$entity = new Entity();
|
||||
$entity->firstName = 'Bernhard';
|
||||
|
||||
$callback = function ($value, ExecutionContextInterface $context) use ($test, $entity, &$called) {
|
||||
$called = true;
|
||||
$test->assertSame($entity, $context->getObject());
|
||||
};
|
||||
|
||||
$this->metadata->addConstraint(new Callback($callback));
|
||||
$this->metadata->addPropertyConstraint('firstName', new Callback($callback));
|
||||
|
||||
$this->validator->validate($entity);
|
||||
|
||||
$this->assertTrue($called);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user