f71e2e11420391ab229b908e388d4b006399998c
[yaffs-website] / web / core / lib / Drupal / Core / Validation / Plugin / Validation / Constraint / PrimitiveTypeConstraintValidator.php
1 <?php
2
3 namespace Drupal\Core\Validation\Plugin\Validation\Constraint;
4
5 use Drupal\Core\TypedData\Type\BinaryInterface;
6 use Drupal\Core\TypedData\Type\BooleanInterface;
7 use Drupal\Core\TypedData\Type\DateTimeInterface;
8 use Drupal\Core\TypedData\Type\DurationInterface;
9 use Drupal\Core\TypedData\Type\FloatInterface;
10 use Drupal\Core\TypedData\Type\IntegerInterface;
11 use Drupal\Core\TypedData\Type\StringInterface;
12 use Drupal\Core\TypedData\Type\UriInterface;
13 use Drupal\Core\TypedData\Validation\TypedDataAwareValidatorTrait;
14 use Drupal\Component\Render\MarkupInterface;
15 use Symfony\Component\Validator\Constraint;
16 use Symfony\Component\Validator\ConstraintValidator;
17
18 /**
19  * Validates the PrimitiveType constraint.
20  */
21 class PrimitiveTypeConstraintValidator extends ConstraintValidator {
22
23   use TypedDataAwareValidatorTrait;
24
25   /**
26    * {@inheritdoc}
27    */
28   public function validate($value, Constraint $constraint) {
29
30     if (!isset($value)) {
31       return;
32     }
33
34     $typed_data = $this->getTypedData();
35     $valid = TRUE;
36     if ($typed_data instanceof BinaryInterface && !is_resource($value)) {
37       $valid = FALSE;
38     }
39     if ($typed_data instanceof BooleanInterface && !(is_bool($value) || $value === 0 || $value === '0' || $value === 1 || $value == '1')) {
40       $valid = FALSE;
41     }
42     if ($typed_data instanceof FloatInterface && filter_var($value, FILTER_VALIDATE_FLOAT) === FALSE) {
43       $valid = FALSE;
44     }
45     if ($typed_data instanceof IntegerInterface && filter_var($value, FILTER_VALIDATE_INT) === FALSE) {
46       $valid = FALSE;
47     }
48     if ($typed_data instanceof StringInterface && !is_scalar($value) && !($value instanceof MarkupInterface)) {
49       $valid = FALSE;
50     }
51     // Ensure that URIs comply with http://tools.ietf.org/html/rfc3986, which
52     // requires:
53     // - That it is well formed (parse_url() returns FALSE if not).
54     // - That it contains a scheme (parse_url(, PHP_URL_SCHEME) returns NULL if
55     //   not).
56     if ($typed_data instanceof UriInterface && in_array(parse_url($value, PHP_URL_SCHEME), [NULL, FALSE], TRUE)) {
57       $valid = FALSE;
58     }
59     // @todo: Move those to separate constraint validators.
60     try {
61       if ($typed_data instanceof DateTimeInterface && $typed_data->getDateTime() && $typed_data->getDateTime()->hasErrors()) {
62         $valid = FALSE;
63       }
64       if ($typed_data instanceof DurationInterface && $typed_data->getDuration() && !($typed_data->getDuration() instanceof \DateInterval)) {
65         $valid = FALSE;
66       }
67     }
68     catch (\Exception $e) {
69       // Invalid durations or dates might throw exceptions.
70       $valid = FALSE;
71     }
72
73     if (!$valid) {
74       // @todo: Provide a good violation message for each problem.
75       $this->context->addViolation($constraint->message, [
76         '%value' => is_object($value) ? get_class($value) : (is_array($value) ? 'Array' : (string) $value)
77       ]);
78     }
79   }
80
81 }