Mercurial > hg > isophonics-drupal-site
comparison vendor/symfony/validator/Constraints/UuidValidator.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | 1fec387a4317 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 /* | |
4 * This file is part of the Symfony package. | |
5 * | |
6 * (c) Fabien Potencier <fabien@symfony.com> | |
7 * | |
8 * For the full copyright and license information, please view the LICENSE | |
9 * file that was distributed with this source code. | |
10 */ | |
11 | |
12 namespace Symfony\Component\Validator\Constraints; | |
13 | |
14 use Symfony\Component\Validator\Constraint; | |
15 use Symfony\Component\Validator\ConstraintValidator; | |
16 use Symfony\Component\Validator\Exception\UnexpectedTypeException; | |
17 | |
18 /** | |
19 * Validates whether the value is a valid UUID per RFC 4122. | |
20 * | |
21 * @author Colin O'Dell <colinodell@gmail.com> | |
22 * @author Bernhard Schussek <bschussek@gmail.com> | |
23 * | |
24 * @see http://tools.ietf.org/html/rfc4122 | |
25 * @see https://en.wikipedia.org/wiki/Universally_unique_identifier | |
26 */ | |
27 class UuidValidator extends ConstraintValidator | |
28 { | |
29 // The strict pattern matches UUIDs like this: | |
30 // xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx | |
31 | |
32 // Roughly speaking: | |
33 // x = any hexadecimal character | |
34 // M = any allowed version {1..5} | |
35 // N = any allowed variant {8, 9, a, b} | |
36 | |
37 const STRICT_LENGTH = 36; | |
38 const STRICT_FIRST_HYPHEN_POSITION = 8; | |
39 const STRICT_LAST_HYPHEN_POSITION = 23; | |
40 const STRICT_VERSION_POSITION = 14; | |
41 const STRICT_VARIANT_POSITION = 19; | |
42 | |
43 // The loose pattern validates similar yet non-compliant UUIDs. | |
44 // Hyphens are completely optional. If present, they should only appear | |
45 // between every fourth character: | |
46 // xxxx-xxxx-xxxx-xxxx-xxxx-xxxx-xxxx-xxxx | |
47 // xxxxxxxxxxxx-xxxx-xxxx-xxxx-xxxx-xxxx | |
48 // xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx | |
49 | |
50 // The value can also be wrapped with characters like []{}: | |
51 // {xxxx-xxxx-xxxx-xxxx-xxxx-xxxx-xxxx-xxxx} | |
52 | |
53 // Neither the version nor the variant is validated by this pattern. | |
54 | |
55 const LOOSE_MAX_LENGTH = 39; | |
56 const LOOSE_FIRST_HYPHEN_POSITION = 4; | |
57 | |
58 /** | |
59 * {@inheritdoc} | |
60 */ | |
61 public function validate($value, Constraint $constraint) | |
62 { | |
63 if (null === $value || '' === $value) { | |
64 return; | |
65 } | |
66 | |
67 if (!$constraint instanceof Uuid) { | |
68 throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Uuid'); | |
69 } | |
70 | |
71 if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) { | |
72 throw new UnexpectedTypeException($value, 'string'); | |
73 } | |
74 | |
75 $value = (string) $value; | |
76 | |
77 if ($constraint->strict) { | |
78 $this->validateStrict($value, $constraint); | |
79 | |
80 return; | |
81 } | |
82 | |
83 $this->validateLoose($value, $constraint); | |
84 } | |
85 | |
86 private function validateLoose($value, Uuid $constraint) | |
87 { | |
88 // Error priority: | |
89 // 1. ERROR_INVALID_CHARACTERS | |
90 // 2. ERROR_INVALID_HYPHEN_PLACEMENT | |
91 // 3. ERROR_TOO_SHORT/ERROR_TOO_LONG | |
92 | |
93 // Trim any wrapping characters like [] or {} used by some legacy systems | |
94 $trimmed = trim($value, '[]{}'); | |
95 | |
96 // Position of the next expected hyphen | |
97 $h = self::LOOSE_FIRST_HYPHEN_POSITION; | |
98 | |
99 // Expected length | |
100 $l = self::LOOSE_MAX_LENGTH; | |
101 | |
102 for ($i = 0; $i < $l; ++$i) { | |
103 // Check length | |
104 if (!isset($trimmed[$i])) { | |
105 $this->context->buildViolation($constraint->message) | |
106 ->setParameter('{{ value }}', $this->formatValue($value)) | |
107 ->setCode(Uuid::TOO_SHORT_ERROR) | |
108 ->addViolation(); | |
109 | |
110 return; | |
111 } | |
112 | |
113 // Hyphens must occur every fifth position | |
114 // xxxx-xxxx-xxxx-xxxx-xxxx-xxxx-xxxx-xxxx | |
115 // ^ ^ ^ ^ ^ ^ ^ | |
116 if ('-' === $trimmed[$i]) { | |
117 if ($i !== $h) { | |
118 $this->context->buildViolation($constraint->message) | |
119 ->setParameter('{{ value }}', $this->formatValue($value)) | |
120 ->setCode(Uuid::INVALID_HYPHEN_PLACEMENT_ERROR) | |
121 ->addViolation(); | |
122 | |
123 return; | |
124 } | |
125 | |
126 $h += 5; | |
127 | |
128 continue; | |
129 } | |
130 | |
131 // Missing hyphens are ignored | |
132 if ($i === $h) { | |
133 $h += 4; | |
134 --$l; | |
135 } | |
136 | |
137 // Check characters | |
138 if (!ctype_xdigit($trimmed[$i])) { | |
139 $this->context->buildViolation($constraint->message) | |
140 ->setParameter('{{ value }}', $this->formatValue($value)) | |
141 ->setCode(Uuid::INVALID_CHARACTERS_ERROR) | |
142 ->addViolation(); | |
143 | |
144 return; | |
145 } | |
146 } | |
147 | |
148 // Check length again | |
149 if (isset($trimmed[$i])) { | |
150 $this->context->buildViolation($constraint->message) | |
151 ->setParameter('{{ value }}', $this->formatValue($value)) | |
152 ->setCode(Uuid::TOO_LONG_ERROR) | |
153 ->addViolation(); | |
154 } | |
155 } | |
156 | |
157 private function validateStrict($value, Uuid $constraint) | |
158 { | |
159 // Error priority: | |
160 // 1. ERROR_INVALID_CHARACTERS | |
161 // 2. ERROR_INVALID_HYPHEN_PLACEMENT | |
162 // 3. ERROR_TOO_SHORT/ERROR_TOO_LONG | |
163 // 4. ERROR_INVALID_VERSION | |
164 // 5. ERROR_INVALID_VARIANT | |
165 | |
166 // Position of the next expected hyphen | |
167 $h = self::STRICT_FIRST_HYPHEN_POSITION; | |
168 | |
169 for ($i = 0; $i < self::STRICT_LENGTH; ++$i) { | |
170 // Check length | |
171 if (!isset($value[$i])) { | |
172 $this->context->buildViolation($constraint->message) | |
173 ->setParameter('{{ value }}', $this->formatValue($value)) | |
174 ->setCode(Uuid::TOO_SHORT_ERROR) | |
175 ->addViolation(); | |
176 | |
177 return; | |
178 } | |
179 | |
180 // Check hyphen placement | |
181 // xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx | |
182 // ^ ^ ^ ^ | |
183 if ('-' === $value[$i]) { | |
184 if ($i !== $h) { | |
185 $this->context->buildViolation($constraint->message) | |
186 ->setParameter('{{ value }}', $this->formatValue($value)) | |
187 ->setCode(Uuid::INVALID_HYPHEN_PLACEMENT_ERROR) | |
188 ->addViolation(); | |
189 | |
190 return; | |
191 } | |
192 | |
193 // xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx | |
194 // ^ | |
195 if ($h < self::STRICT_LAST_HYPHEN_POSITION) { | |
196 $h += 5; | |
197 } | |
198 | |
199 continue; | |
200 } | |
201 | |
202 // Check characters | |
203 if (!ctype_xdigit($value[$i])) { | |
204 $this->context->buildViolation($constraint->message) | |
205 ->setParameter('{{ value }}', $this->formatValue($value)) | |
206 ->setCode(Uuid::INVALID_CHARACTERS_ERROR) | |
207 ->addViolation(); | |
208 | |
209 return; | |
210 } | |
211 | |
212 // Missing hyphen | |
213 if ($i === $h) { | |
214 $this->context->buildViolation($constraint->message) | |
215 ->setParameter('{{ value }}', $this->formatValue($value)) | |
216 ->setCode(Uuid::INVALID_HYPHEN_PLACEMENT_ERROR) | |
217 ->addViolation(); | |
218 | |
219 return; | |
220 } | |
221 } | |
222 | |
223 // Check length again | |
224 if (isset($value[$i])) { | |
225 $this->context->buildViolation($constraint->message) | |
226 ->setParameter('{{ value }}', $this->formatValue($value)) | |
227 ->setCode(Uuid::TOO_LONG_ERROR) | |
228 ->addViolation(); | |
229 } | |
230 | |
231 // Check version | |
232 if (!in_array($value[self::STRICT_VERSION_POSITION], $constraint->versions)) { | |
233 $this->context->buildViolation($constraint->message) | |
234 ->setParameter('{{ value }}', $this->formatValue($value)) | |
235 ->setCode(Uuid::INVALID_VERSION_ERROR) | |
236 ->addViolation(); | |
237 } | |
238 | |
239 // Check variant - first two bits must equal "10" | |
240 // 0b10xx | |
241 // & 0b1100 (12) | |
242 // = 0b1000 (8) | |
243 if ((hexdec($value[self::STRICT_VARIANT_POSITION]) & 12) !== 8) { | |
244 $this->context->buildViolation($constraint->message) | |
245 ->setParameter('{{ value }}', $this->formatValue($value)) | |
246 ->setCode(Uuid::INVALID_VARIANT_ERROR) | |
247 ->addViolation(); | |
248 } | |
249 } | |
250 } |