[Validator] Added getObject() to ExecutionContextInterface

This commit is contained in:
Bernhard Schussek 2014-03-18 17:49:36 +01:00
parent 9b204c9354
commit c5629bb7ee
3 changed files with 42 additions and 0 deletions

View File

@ -232,6 +232,14 @@ class ExecutionContext implements ExecutionContextInterface
return $this->value;
}
/**
* {@inheritdoc}
*/
public function getObject()
{
return $this->object;
}
/**
* {@inheritdoc}
*/

View File

@ -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.
*

View File

@ -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);
}
}