You are here

protected function RecursiveContextualValidator::validateNode in Drupal 8

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/TypedData/Validation/RecursiveContextualValidator.php \Drupal\Core\TypedData\Validation\RecursiveContextualValidator::validateNode()

Validates a Typed Data node in the validation tree.

If no constraints are passed, the data is validated against the constraints specified in its data definition. If the data is complex or a list and no constraints are passed, the contained properties or list items are validated recursively.

Parameters

\Drupal\Core\TypedData\TypedDataInterface $data: The data to validated.

\Symfony\Component\Validator\Constraint[]|null $constraints: (optional) If set, an array of constraints to validate.

bool $is_root_call: (optional) Whether its the most upper call in the type data tree.

Return value

$this

2 calls to RecursiveContextualValidator::validateNode()
RecursiveContextualValidator::validate in core/lib/Drupal/Core/TypedData/Validation/RecursiveContextualValidator.php
Validates a value against a constraint or a list of constraints.
RecursiveContextualValidator::validateProperty in core/lib/Drupal/Core/TypedData/Validation/RecursiveContextualValidator.php
Validates a property of an object against the constraints specified for this property.

File

core/lib/Drupal/Core/TypedData/Validation/RecursiveContextualValidator.php, line 121

Class

RecursiveContextualValidator
Defines a recursive contextual validator for Typed Data.

Namespace

Drupal\Core\TypedData\Validation

Code

protected function validateNode(TypedDataInterface $data, $constraints = NULL, $is_root_call = FALSE) {
  $previous_value = $this->context
    ->getValue();
  $previous_object = $this->context
    ->getObject();
  $previous_metadata = $this->context
    ->getMetadata();
  $previous_path = $this->context
    ->getPropertyPath();
  $metadata = $this->metadataFactory
    ->getMetadataFor($data);
  $cache_key = spl_object_hash($data);
  $property_path = $is_root_call ? '' : PropertyPath::append($previous_path, $data
    ->getName());

  // Prefer a specific instance of the typed data manager stored by the data
  // if it is available. This is necessary for specialized typed data objects,
  // for example those using the typed config subclass of the manager.
  $typed_data_manager = method_exists($data, 'getTypedDataManager') ? $data
    ->getTypedDataManager() : $this->typedDataManager;

  // Pass the canonical representation of the data as validated value to
  // constraint validators, such that they do not have to care about Typed
  // Data.
  $value = $typed_data_manager
    ->getCanonicalRepresentation($data);
  $constraints_given = isset($constraints);
  $this->context
    ->setNode($value, $data, $metadata, $property_path);
  if (isset($constraints) || !$this->context
    ->isGroupValidated($cache_key, Constraint::DEFAULT_GROUP)) {
    if (!isset($constraints)) {
      $this->context
        ->markGroupAsValidated($cache_key, Constraint::DEFAULT_GROUP);
      $constraints = $metadata
        ->findConstraints(Constraint::DEFAULT_GROUP);
    }
    $this
      ->validateConstraints($value, $cache_key, $constraints);
  }

  // If the data is a list or complex data, validate the contained list items
  // or properties. However, do not recurse if the data is empty.
  // Next, we do not recurse if given constraints are validated against an
  // entity, since we should determine whether the entity matches the
  // constraints and not whether the entity validates.
  if (($data instanceof ListInterface || $data instanceof ComplexDataInterface) && !$data
    ->isEmpty() && !($data instanceof EntityAdapter && $constraints_given)) {
    foreach ($data as $name => $property) {
      $this
        ->validateNode($property);
    }
  }
  $this->context
    ->setNode($previous_value, $previous_object, $previous_metadata, $previous_path);
  return $this;
}