Pull merge.
[yaffs-website] / web / core / lib / Drupal / Core / Validation / Plugin / Validation / Constraint / ComplexDataConstraintValidator.php
1 <?php
2
3 namespace Drupal\Core\Validation\Plugin\Validation\Constraint;
4
5 use Drupal\Core\TypedData\ComplexDataInterface;
6 use Drupal\Core\TypedData\TypedDataInterface;
7 use Drupal\Core\TypedData\Validation\TypedDataAwareValidatorTrait;
8 use Symfony\Component\Validator\Constraint;
9 use Symfony\Component\Validator\ConstraintValidator;
10 use Symfony\Component\Validator\Exception\UnexpectedTypeException;
11
12 /**
13  * Validates complex data.
14  */
15 class ComplexDataConstraintValidator extends ConstraintValidator {
16
17   use TypedDataAwareValidatorTrait;
18
19   /**
20    * {@inheritdoc}
21    */
22   public function validate($data, Constraint $constraint) {
23
24     // If un-wrapped data has been passed, fetch the typed data object first.
25     if (!$data instanceof TypedDataInterface) {
26       $data = $this->getTypedData();
27     }
28     if (!$data instanceof ComplexDataInterface) {
29       throw new UnexpectedTypeException($data, 'ComplexData');
30     }
31
32     foreach ($constraint->properties as $name => $constraints) {
33       $this->context->getValidator()
34         ->inContext($this->context)
35         // Specifically pass along FALSE as $root_call, as we validate the data
36         // as part of the typed data tree.
37         ->validate($data->get($name), $constraints, NULL, FALSE);
38     }
39   }
40
41 }