You are here

public function RecursiveContextualValidator::validatePropertyValue in Plug 7

Validates a value against the constraints specified for an object's property.

Parameters

object|string $objectOrClass The object or its class name:

string $propertyName The name of the property:

mixed $value The value to validate against the: property's constraints

array|null $groups The validation groups to validate. If: none is given, "Default" is assumed

Return value

ContextualValidatorInterface This validator

Overrides ContextualValidatorInterface::validatePropertyValue

File

lib/Symfony/validator/Symfony/Component/Validator/Validator/RecursiveContextualValidator.php, line 231

Class

RecursiveContextualValidator
Recursive implementation of {@link ContextualValidatorInterface}.

Namespace

Symfony\Component\Validator\Validator

Code

public function validatePropertyValue($objectOrClass, $propertyName, $value, $groups = null) {
  $classMetadata = $this->metadataFactory
    ->getMetadataFor($objectOrClass);
  if (!$classMetadata instanceof ClassMetadataInterface) {

    // Cannot be UnsupportedMetadataException because of BC with
    // Symfony < 2.5
    throw new ValidatorException(sprintf('The metadata factory should return instances of ' . '"\\Symfony\\Component\\Validator\\Mapping\\ClassMetadataInterface", ' . 'got: "%s".', is_object($classMetadata) ? get_class($classMetadata) : gettype($classMetadata)));
  }
  $propertyMetadatas = $classMetadata
    ->getPropertyMetadata($propertyName);
  $groups = $groups ? $this
    ->normalizeGroups($groups) : $this->defaultGroups;
  if (is_object($objectOrClass)) {
    $object = $objectOrClass;
    $cacheKey = spl_object_hash($objectOrClass);
    $propertyPath = PropertyPath::append($this->defaultPropertyPath, $propertyName);
  }
  else {

    // $objectOrClass contains a class name
    $object = null;
    $cacheKey = null;
    $propertyPath = $this->defaultPropertyPath;
  }
  $previousValue = $this->context
    ->getValue();
  $previousObject = $this->context
    ->getObject();
  $previousMetadata = $this->context
    ->getMetadata();
  $previousPath = $this->context
    ->getPropertyPath();
  $previousGroup = $this->context
    ->getGroup();
  foreach ($propertyMetadatas as $propertyMetadata) {
    $this
      ->validateGenericNode($value, $object, $cacheKey . ':' . $propertyName, $propertyMetadata, $propertyPath, $groups, null, TraversalStrategy::IMPLICIT, $this->context);
  }
  $this->context
    ->setNode($previousValue, $previousObject, $previousMetadata, $previousPath);
  $this->context
    ->setGroup($previousGroup);
  return $this;
}