[Validator] Reduced number of method calls on the execution context

This commit is contained in:
Bernhard Schussek 2014-03-17 17:55:15 +01:00
parent 73c9cc5806
commit 2f23d9725b
4 changed files with 29 additions and 74 deletions

View File

@ -139,24 +139,10 @@ class ExecutionContext implements ExecutionContextInterface
/**
* {@inheritdoc}
*/
public function setValue($value)
public function setNode($value, MetadataInterface $metadata = null, $propertyPath)
{
$this->value = $value;
}
/**
* {@inheritdoc}
*/
public function setMetadata(MetadataInterface $metadata = null)
{
$this->metadata = $metadata;
}
/**
* {@inheritdoc}
*/
public function setPropertyPath($propertyPath)
{
$this->propertyPath = (string) $propertyPath;
}

View File

@ -102,32 +102,14 @@ interface ExecutionContextInterface extends LegacyExecutionContextInterface
/**
* Sets the currently validated value.
*
* @param mixed $value The validated value
* @param mixed $value The validated value
* @param MetadataInterface $metadata The validation metadata
* @param string $propertyPath The property path to the current value
*
* @internal Used by the validator engine. Should not be called by user
* code.
*/
public function setValue($value);
/**
* Sets the current validation metadata.
*
* @param MetadataInterface $metadata The validation metadata
*
* @internal Used by the validator engine. Should not be called by user
* code.
*/
public function setMetadata(MetadataInterface $metadata = null);
/**
* Sets the property path leading to the current value.
*
* @param string $propertyPath The property path to the current value
*
* @internal Used by the validator engine. Should not be called by user
* code.
*/
public function setPropertyPath($propertyPath);
public function setNode($value, MetadataInterface $metadata = null, $propertyPath);
/**
* Sets the currently validated group.

View File

@ -69,9 +69,7 @@ class NodeValidationVisitor extends AbstractVisitor
return true;
}
$context->setValue($node->value);
$context->setMetadata($node->metadata);
$context->setPropertyPath($node->propertyPath);
$context->setNode($node->value, $node->metadata, $node->propertyPath);
if ($node instanceof ClassNode) {
$this->replaceDefaultGroup($node);
@ -171,43 +169,34 @@ class NodeValidationVisitor extends AbstractVisitor
*/
private function validateNodeForGroup(Node $node, $group, ExecutionContextInterface $context, $objectHash)
{
try {
$context->setGroup($group);
$context->setGroup($group);
foreach ($node->metadata->findConstraints($group) as $constraint) {
// Prevent duplicate validation of constraints, in the case
// that constraints belong to multiple validated groups
if (null !== $objectHash) {
$constraintHash = spl_object_hash($constraint);
foreach ($node->metadata->findConstraints($group) as $constraint) {
// Prevent duplicate validation of constraints, in the case
// that constraints belong to multiple validated groups
if (null !== $objectHash) {
$constraintHash = spl_object_hash($constraint);
if ($node instanceof ClassNode) {
if ($context->isClassConstraintValidated($objectHash, $constraintHash)) {
continue;
}
$context->markClassConstraintAsValidated($objectHash, $constraintHash);
} elseif ($node instanceof PropertyNode) {
$propertyName = $node->metadata->getPropertyName();
if ($context->isPropertyConstraintValidated($objectHash, $propertyName, $constraintHash)) {
continue;
}
$context->markPropertyConstraintAsValidated($objectHash, $propertyName, $constraintHash);
if ($node instanceof ClassNode) {
if ($context->isClassConstraintValidated($objectHash, $constraintHash)) {
continue;
}
}
$validator = $this->validatorFactory->getInstance($constraint);
$validator->initialize($context);
$validator->validate($node->value, $constraint);
$context->markClassConstraintAsValidated($objectHash, $constraintHash);
} elseif ($node instanceof PropertyNode) {
$propertyName = $node->metadata->getPropertyName();
if ($context->isPropertyConstraintValidated($objectHash, $propertyName, $constraintHash)) {
continue;
}
$context->markPropertyConstraintAsValidated($objectHash, $propertyName, $constraintHash);
}
}
$context->setGroup(null);
} catch (\Exception $e) {
// Should be put into a finally block once we switch to PHP 5.5
$context->setGroup(null);
throw $e;
$validator = $this->validatorFactory->getInstance($constraint);
$validator->initialize($context);
$validator->validate($node->value, $constraint);
}
}

View File

@ -581,9 +581,7 @@ class RecursiveContextualValidator implements ContextualValidatorInterface
*/
public function validateNode($value, $valueHash, $container, $containerHash, MetadataInterface $metadata = null, $propertyPath, array $groups, $traversalStrategy, ExecutionContextInterface $context)
{
$context->setValue($value);
$context->setMetadata($metadata);
$context->setPropertyPath($propertyPath);
$context->setNode($value, $metadata, $propertyPath);
// if group (=[<G1,G2>,G3,G4]) contains group sequence (=<G1,G2>)
// then call traverse() with each entry of the group sequence and abort