1 | <?php |
||
12 | final class Format implements ConstraintInterface |
||
13 | { |
||
14 | const KEYWORD = 'format'; |
||
15 | |||
16 | /** |
||
17 | * @see https://tools.ietf.org/html/rfc3339#section-5.6 |
||
18 | */ |
||
19 | const DATE_TIME_PATTERN = |
||
20 | '/^(?<fullyear>\d{4})-(?<month>0[1-9]|1[0-2])-(?<mday>0[1-9]|[12][0-9]|3[01])' . 'T' . |
||
21 | '(?<hour>[01][0-9]|2[0-3]):(?<minute>[0-5][0-9]):(?<second>[0-5][0-9]|60)(?<secfrac>\.[0-9]+)?' . |
||
22 | '(Z|(\+|-)(?<offset_hour>[01][0-9]|2[0-3]):(?<offset_minute>[0-5][0-9]))$/i'; |
||
23 | |||
24 | /** |
||
25 | * @internal |
||
26 | */ |
||
27 | const HOST_NAME_PATTERN = '/^[_a-z]+\.([_a-z]+\.?)+$/i'; |
||
28 | |||
29 | /** |
||
30 | * @internal |
||
31 | * |
||
32 | * @var string[] |
||
33 | */ |
||
34 | const KNOWN_FORMATS = ['date-time', 'uri', 'email', 'ipv4', 'ipv6','hostname']; |
||
35 | |||
36 | /** |
||
37 | * @var \League\JsonGuard\Constraint\DraftFour\Format\FormatExtensionInterface[] |
||
38 | */ |
||
39 | private $extensions = []; |
||
40 | |||
41 | /** |
||
42 | * @var bool |
||
43 | */ |
||
44 | private $ignoreUnknownFormats = true; |
||
45 | |||
46 | /** |
||
47 | * Any custom format extensions to use, indexed by the format name. |
||
48 | * |
||
49 | * @param array \League\JsonGuard\Constraint\DraftFour\Format\FormatExtensionInterface[] $extensions |
||
50 | * @param bool $ignoreUnknownFormats |
||
51 | */ |
||
52 | 82 | public function __construct(array $extensions = [], $ignoreUnknownFormats = true) |
|
60 | |||
61 | /** |
||
62 | * Add a custom format extension. |
||
63 | * |
||
64 | * @param string $format |
||
65 | * @param \League\JsonGuard\Constraint\DraftFour\Format\FormatExtensionInterface $extension |
||
66 | */ |
||
67 | 12 | public function addExtension($format, FormatExtensionInterface $extension) |
|
71 | |||
72 | /** |
||
73 | * Define if unknown formats shall be ignored |
||
74 | * |
||
75 | * @param boolean |
||
76 | */ |
||
77 | 16 | public function setIgnoreUnknownFormats($ignoreUnknownFormats) |
|
81 | |||
82 | /** |
||
83 | * {@inheritdoc} |
||
84 | */ |
||
85 | 82 | public function validate($value, $parameter, Validator $validator) |
|
145 | |||
146 | /** |
||
147 | * @param mixed $value |
||
148 | * @param string $pattern |
||
149 | * @param \League\JsonGuard\Validator $validator |
||
150 | * |
||
151 | * @return \League\JsonGuard\ValidationError|null |
||
152 | */ |
||
153 | 32 | private static function validateRegex($value, $pattern, Validator $validator) |
|
161 | |||
162 | /** |
||
163 | * @param mixed $value |
||
164 | * @param int $filter |
||
165 | * @param mixed $options |
||
166 | * @param \League\JsonGuard\Validator $validator |
||
167 | * |
||
168 | * @return \League\JsonGuard\ValidationError|null |
||
169 | */ |
||
170 | 6 | private static function validateFilter($value, $filter, $options, Validator $validator) |
|
186 | } |
||
187 |