1 | <?php |
||
11 | final class Format implements ConstraintInterface |
||
12 | { |
||
13 | const KEYWORD = 'format'; |
||
14 | |||
15 | // @codingStandardsIgnoreStart |
||
16 | // @see https://www.w3.org/TR/2012/REC-xmlschema11-2-20120405/datatypes.html#dateTime-lexical-mapping |
||
17 | const DATE_TIME_PATTERN = '/^-?([1-9][0-9]{3,}|0[0-9]{3})-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])T(([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9](\.[0-9]+)?|(24:00:00(\.0+)?))(Z|(\+|-)((0[0-9]|1[0-3]):[0-5][0-9]|14:00))?$/'; |
||
18 | // @codingStandardsIgnoreEnd |
||
19 | |||
20 | const HOST_NAME_PATTERN = '/^[_a-z]+\.([_a-z]+\.?)+$/i'; |
||
21 | |||
22 | /** |
||
23 | * @var \League\JsonGuard\Constraint\DraftFour\Format\FormatExtensionInterface[] |
||
24 | */ |
||
25 | private $extensions = []; |
||
26 | |||
27 | /** |
||
28 | * Any custom format extensions to use, indexed by the format name. |
||
29 | * |
||
30 | * @param array \League\JsonGuard\Constraint\DraftFour\Format\FormatExtensionInterface[] |
||
31 | */ |
||
32 | 24 | public function __construct(array $extensions = []) |
|
38 | |||
39 | /** |
||
40 | * Add a custom format extension. |
||
41 | * |
||
42 | * @param string $format |
||
43 | * @param \League\JsonGuard\Constraint\DraftFour\Format\FormatExtensionInterface $extension |
||
44 | */ |
||
45 | 4 | public function addExtension($format, FormatExtensionInterface $extension) |
|
49 | |||
50 | /** |
||
51 | * {@inheritdoc} |
||
52 | */ |
||
53 | 24 | public function validate($value, $parameter, Validator $validator) |
|
54 | { |
||
55 | 24 | Assert::type($parameter, 'string', self::KEYWORD, $validator->getSchemaPath()); |
|
56 | |||
57 | 22 | if (isset($this->extensions[$parameter])) { |
|
58 | 4 | return $this->extensions[$parameter]->validate($value, $validator); |
|
59 | } |
||
60 | |||
61 | switch ($parameter) { |
||
62 | 18 | case 'date-time': |
|
63 | 14 | return self::validateRegex( |
|
64 | 7 | $value, |
|
65 | 14 | self::DATE_TIME_PATTERN, |
|
66 | 7 | $validator |
|
67 | 7 | ); |
|
68 | 6 | case 'uri': |
|
69 | 4 | return self::validateFilter( |
|
70 | 2 | $value, |
|
71 | 4 | FILTER_VALIDATE_URL, |
|
72 | 4 | null, |
|
73 | 2 | $validator |
|
74 | 2 | ); |
|
75 | 4 | case 'email': |
|
76 | 4 | return self::validateFilter( |
|
77 | 2 | $value, |
|
78 | 4 | FILTER_VALIDATE_EMAIL, |
|
79 | 4 | null, |
|
80 | 2 | $validator |
|
81 | 2 | ); |
|
82 | 2 | case 'ipv4': |
|
83 | 2 | return self::validateFilter( |
|
84 | 1 | $value, |
|
85 | 2 | FILTER_VALIDATE_IP, |
|
86 | 2 | FILTER_FLAG_IPV4, |
|
87 | 1 | $validator |
|
88 | 1 | ); |
|
89 | 2 | case 'ipv6': |
|
90 | 2 | return self::validateFilter( |
|
91 | 1 | $value, |
|
92 | 2 | FILTER_VALIDATE_IP, |
|
93 | 2 | FILTER_FLAG_IPV6, |
|
94 | 1 | $validator |
|
95 | 1 | ); |
|
96 | 2 | case 'hostname': |
|
97 | 2 | return self::validateRegex( |
|
98 | 1 | $value, |
|
99 | 2 | self::HOST_NAME_PATTERN, |
|
100 | 1 | $validator |
|
101 | 1 | ); |
|
102 | } |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * @param mixed $value |
||
107 | * @param string $pattern |
||
108 | * @param \League\JsonGuard\Validator $validator |
||
109 | * |
||
110 | * @return \League\JsonGuard\ValidationError|null |
||
111 | * |
||
112 | */ |
||
113 | 14 | private static function validateRegex($value, $pattern, Validator $validator) |
|
121 | |||
122 | /** |
||
123 | * @param mixed $value |
||
124 | * @param int $filter |
||
125 | * @param mixed $options |
||
126 | * @param \League\JsonGuard\Validator $validator |
||
127 | * |
||
128 | * @return \League\JsonGuard\ValidationError|null |
||
129 | * |
||
130 | */ |
||
131 | 6 | private static function validateFilter($value, $filter, $options, Validator $validator) |
|
147 | } |
||
148 |