You are here

public function ValidationVisitor::validate in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/symfony/validator/ValidationVisitor.php \Symfony\Component\Validator\ValidationVisitor::validate()

Validates a value.

If the value is an array or a traversable object, you can set the parameter <tt>$traverse</tt> to <tt>true</tt> in order to run through the collection and validate each element. If these elements can be collections again and you want to traverse them recursively, set the parameter <tt>$deep</tt> to <tt>true</tt> as well.

If you set <tt>$traversable</tt> to <tt>true</tt>, the visitor will nevertheless try to find metadata for the collection and validate its constraints. If no such metadata is found, the visitor ignores that and only iterates the collection.

If you don't set <tt>$traversable</tt> to <tt>true</tt> and the visitor does not find metadata for the given value, it will fail with an exception.

Parameters

mixed $value The value to validate.:

string $group The validation group to validate.:

string $propertyPath The current property path in the validation graph.:

bool $traverse Whether to traverse the value if it is traversable.:

bool $deep Whether to traverse nested traversable values recursively.:

Throws

Exception\NoSuchMetadataException If no metadata can be found for the given value.

Overrides ValidationVisitorInterface::validate

File

vendor/symfony/validator/ValidationVisitor.php, line 120

Class

ValidationVisitor
Default implementation of {@link ValidationVisitorInterface} and {@link GlobalExecutionContextInterface}.

Namespace

Symfony\Component\Validator

Code

public function validate($value, $group, $propertyPath, $traverse = false, $deep = false) {
  if (null === $value) {
    return;
  }
  if (is_object($value)) {
    $hash = spl_object_hash($value);

    // Exit, if the object is already validated for the current group
    if (isset($this->validatedObjects[$hash][$group])) {
      return;
    }

    // Initialize if the object wasn't initialized before
    if (!isset($this->validatedObjects[$hash])) {
      foreach ($this->objectInitializers as $initializer) {
        if (!$initializer instanceof ObjectInitializerInterface) {
          throw new \LogicException('Validator initializers must implement ObjectInitializerInterface.');
        }
        $initializer
          ->initialize($value);
      }
    }

    // Remember validating this object before starting and possibly
    // traversing the object graph
    $this->validatedObjects[$hash][$group] = true;
  }

  // Validate arrays recursively by default, otherwise every driver needs
  // to implement special handling for arrays.
  // https://github.com/symfony/symfony/issues/6246
  if (is_array($value) || $traverse && $value instanceof \Traversable) {
    foreach ($value as $key => $element) {

      // Ignore any scalar values in the collection
      if (is_object($element) || is_array($element)) {

        // Only repeat the traversal if $deep is set
        $this
          ->validate($element, $group, $propertyPath . '[' . $key . ']', $deep, $deep);
      }
    }
    try {
      $this->metadataFactory
        ->getMetadataFor($value)
        ->accept($this, $value, $group, $propertyPath);
    } catch (NoSuchMetadataException $e) {

      // Metadata doesn't necessarily have to exist for
      // traversable objects, because we know how to validate
      // them anyway. Optionally, additional metadata is supported.
    }
  }
  else {
    $this->metadataFactory
      ->getMetadataFor($value)
      ->accept($this, $value, $group, $propertyPath);
  }
}