d025a560d28aa5b70bb44d1236f6af453a43e5bc
[yaffs-website] / vendor / symfony / validator / Constraints / UuidValidator.php
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\Context\ExecutionContextInterface;
15 use Symfony\Component\Validator\Constraint;
16 use Symfony\Component\Validator\ConstraintValidator;
17 use Symfony\Component\Validator\Constraints\Deprecated\UuidValidator as Deprecated;
18 use Symfony\Component\Validator\Exception\UnexpectedTypeException;
19
20 /**
21  * Validates whether the value is a valid UUID per RFC 4122.
22  *
23  * @author Colin O'Dell <colinodell@gmail.com>
24  * @author Bernhard Schussek <bschussek@gmail.com>
25  *
26  * @see http://tools.ietf.org/html/rfc4122
27  * @see https://en.wikipedia.org/wiki/Universally_unique_identifier
28  */
29 class UuidValidator extends ConstraintValidator
30 {
31     // The strict pattern matches UUIDs like this:
32     // xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx
33
34     // Roughly speaking:
35     // x = any hexadecimal character
36     // M = any allowed version {1..5}
37     // N = any allowed variant {8, 9, a, b}
38
39     const STRICT_LENGTH = 36;
40     const STRICT_FIRST_HYPHEN_POSITION = 8;
41     const STRICT_LAST_HYPHEN_POSITION = 23;
42     const STRICT_VERSION_POSITION = 14;
43     const STRICT_VARIANT_POSITION = 19;
44
45     // The loose pattern validates similar yet non-compliant UUIDs.
46     // Hyphens are completely optional. If present, they should only appear
47     // between every fourth character:
48     // xxxx-xxxx-xxxx-xxxx-xxxx-xxxx-xxxx-xxxx
49     // xxxxxxxxxxxx-xxxx-xxxx-xxxx-xxxx-xxxx
50     // xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
51
52     // The value can also be wrapped with characters like []{}:
53     // {xxxx-xxxx-xxxx-xxxx-xxxx-xxxx-xxxx-xxxx}
54
55     // Neither the version nor the variant is validated by this pattern.
56
57     const LOOSE_MAX_LENGTH = 39;
58     const LOOSE_FIRST_HYPHEN_POSITION = 4;
59
60     /**
61      * @deprecated since version 2.6, to be removed in 3.0
62      */
63     const STRICT_PATTERN = '/^[a-f0-9]{8}-[a-f0-9]{4}-[%s][a-f0-9]{3}-[89ab][a-f0-9]{3}-[a-f0-9]{12}$/i';
64
65     /**
66      * @deprecated since version 2.6, to be removed in 3.0
67      */
68     const LOOSE_PATTERN = '/^[a-f0-9]{4}(?:-?[a-f0-9]{4}){7}$/i';
69
70     /**
71      * @deprecated since version 2.6, to be removed in 3.0
72      */
73     const STRICT_UUID_LENGTH = 36;
74
75     /**
76      * {@inheritdoc}
77      */
78     public function validate($value, Constraint $constraint)
79     {
80         if (null === $value || '' === $value) {
81             return;
82         }
83
84         if (!$constraint instanceof Uuid) {
85             throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Uuid');
86         }
87
88         if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
89             throw new UnexpectedTypeException($value, 'string');
90         }
91
92         $value = (string) $value;
93
94         if ($constraint->strict) {
95             $this->validateStrict($value, $constraint);
96
97             return;
98         }
99
100         $this->validateLoose($value, $constraint);
101     }
102
103     private function validateLoose($value, Uuid $constraint)
104     {
105         // Error priority:
106         // 1. ERROR_INVALID_CHARACTERS
107         // 2. ERROR_INVALID_HYPHEN_PLACEMENT
108         // 3. ERROR_TOO_SHORT/ERROR_TOO_LONG
109
110         // Trim any wrapping characters like [] or {} used by some legacy systems
111         $trimmed = trim($value, '[]{}');
112
113         // Position of the next expected hyphen
114         $h = self::LOOSE_FIRST_HYPHEN_POSITION;
115
116         // Expected length
117         $l = self::LOOSE_MAX_LENGTH;
118
119         for ($i = 0; $i < $l; ++$i) {
120             // Check length
121             if (!isset($trimmed[$i])) {
122                 if ($this->context instanceof ExecutionContextInterface) {
123                     $this->context->buildViolation($constraint->message)
124                         ->setParameter('{{ value }}', $this->formatValue($value))
125                         ->setCode(Uuid::TOO_SHORT_ERROR)
126                         ->addViolation();
127                 } else {
128                     $this->buildViolation($constraint->message)
129                         ->setParameter('{{ value }}', $this->formatValue($value))
130                         ->setCode(Uuid::TOO_SHORT_ERROR)
131                         ->addViolation();
132                 }
133
134                 return;
135             }
136
137             // Hyphens must occur every fifth position
138             // xxxx-xxxx-xxxx-xxxx-xxxx-xxxx-xxxx-xxxx
139             //     ^    ^    ^    ^    ^    ^    ^
140             if ('-' === $trimmed[$i]) {
141                 if ($i !== $h) {
142                     if ($this->context instanceof ExecutionContextInterface) {
143                         $this->context->buildViolation($constraint->message)
144                             ->setParameter('{{ value }}', $this->formatValue($value))
145                             ->setCode(Uuid::INVALID_HYPHEN_PLACEMENT_ERROR)
146                             ->addViolation();
147                     } else {
148                         $this->buildViolation($constraint->message)
149                             ->setParameter('{{ value }}', $this->formatValue($value))
150                             ->setCode(Uuid::INVALID_HYPHEN_PLACEMENT_ERROR)
151                             ->addViolation();
152                     }
153
154                     return;
155                 }
156
157                 $h += 5;
158
159                 continue;
160             }
161
162             // Missing hyphens are ignored
163             if ($i === $h) {
164                 $h += 4;
165                 --$l;
166             }
167
168             // Check characters
169             if (!ctype_xdigit($trimmed[$i])) {
170                 if ($this->context instanceof ExecutionContextInterface) {
171                     $this->context->buildViolation($constraint->message)
172                         ->setParameter('{{ value }}', $this->formatValue($value))
173                         ->setCode(Uuid::INVALID_CHARACTERS_ERROR)
174                         ->addViolation();
175                 } else {
176                     $this->buildViolation($constraint->message)
177                         ->setParameter('{{ value }}', $this->formatValue($value))
178                         ->setCode(Uuid::INVALID_CHARACTERS_ERROR)
179                         ->addViolation();
180                 }
181
182                 return;
183             }
184         }
185
186         // Check length again
187         if (isset($trimmed[$i])) {
188             if ($this->context instanceof ExecutionContextInterface) {
189                 $this->context->buildViolation($constraint->message)
190                     ->setParameter('{{ value }}', $this->formatValue($value))
191                     ->setCode(Uuid::TOO_LONG_ERROR)
192                     ->addViolation();
193             } else {
194                 $this->buildViolation($constraint->message)
195                     ->setParameter('{{ value }}', $this->formatValue($value))
196                     ->setCode(Uuid::TOO_LONG_ERROR)
197                     ->addViolation();
198             }
199         }
200     }
201
202     private function validateStrict($value, Uuid $constraint)
203     {
204         // Error priority:
205         // 1. ERROR_INVALID_CHARACTERS
206         // 2. ERROR_INVALID_HYPHEN_PLACEMENT
207         // 3. ERROR_TOO_SHORT/ERROR_TOO_LONG
208         // 4. ERROR_INVALID_VERSION
209         // 5. ERROR_INVALID_VARIANT
210
211         // Position of the next expected hyphen
212         $h = self::STRICT_FIRST_HYPHEN_POSITION;
213
214         for ($i = 0; $i < self::STRICT_LENGTH; ++$i) {
215             // Check length
216             if (!isset($value[$i])) {
217                 if ($this->context instanceof ExecutionContextInterface) {
218                     $this->context->buildViolation($constraint->message)
219                         ->setParameter('{{ value }}', $this->formatValue($value))
220                         ->setCode(Uuid::TOO_SHORT_ERROR)
221                         ->addViolation();
222                 } else {
223                     $this->buildViolation($constraint->message)
224                         ->setParameter('{{ value }}', $this->formatValue($value))
225                         ->setCode(Uuid::TOO_SHORT_ERROR)
226                         ->addViolation();
227                 }
228
229                 return;
230             }
231
232             // Check hyphen placement
233             // xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
234             //         ^    ^    ^    ^
235             if ('-' === $value[$i]) {
236                 if ($i !== $h) {
237                     if ($this->context instanceof ExecutionContextInterface) {
238                         $this->context->buildViolation($constraint->message)
239                             ->setParameter('{{ value }}', $this->formatValue($value))
240                             ->setCode(Uuid::INVALID_HYPHEN_PLACEMENT_ERROR)
241                             ->addViolation();
242                     } else {
243                         $this->buildViolation($constraint->message)
244                             ->setParameter('{{ value }}', $this->formatValue($value))
245                             ->setCode(Uuid::INVALID_HYPHEN_PLACEMENT_ERROR)
246                             ->addViolation();
247                     }
248
249                     return;
250                 }
251
252                 // xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
253                 //                        ^
254                 if ($h < self::STRICT_LAST_HYPHEN_POSITION) {
255                     $h += 5;
256                 }
257
258                 continue;
259             }
260
261             // Check characters
262             if (!ctype_xdigit($value[$i])) {
263                 if ($this->context instanceof ExecutionContextInterface) {
264                     $this->context->buildViolation($constraint->message)
265                         ->setParameter('{{ value }}', $this->formatValue($value))
266                         ->setCode(Uuid::INVALID_CHARACTERS_ERROR)
267                         ->addViolation();
268                 } else {
269                     $this->buildViolation($constraint->message)
270                         ->setParameter('{{ value }}', $this->formatValue($value))
271                         ->setCode(Uuid::INVALID_CHARACTERS_ERROR)
272                         ->addViolation();
273                 }
274
275                 return;
276             }
277
278             // Missing hyphen
279             if ($i === $h) {
280                 if ($this->context instanceof ExecutionContextInterface) {
281                     $this->context->buildViolation($constraint->message)
282                         ->setParameter('{{ value }}', $this->formatValue($value))
283                         ->setCode(Uuid::INVALID_HYPHEN_PLACEMENT_ERROR)
284                         ->addViolation();
285                 } else {
286                     $this->buildViolation($constraint->message)
287                         ->setParameter('{{ value }}', $this->formatValue($value))
288                         ->setCode(Uuid::INVALID_HYPHEN_PLACEMENT_ERROR)
289                         ->addViolation();
290                 }
291
292                 return;
293             }
294         }
295
296         // Check length again
297         if (isset($value[$i])) {
298             if ($this->context instanceof ExecutionContextInterface) {
299                 $this->context->buildViolation($constraint->message)
300                     ->setParameter('{{ value }}', $this->formatValue($value))
301                     ->setCode(Uuid::TOO_LONG_ERROR)
302                     ->addViolation();
303             } else {
304                 $this->buildViolation($constraint->message)
305                     ->setParameter('{{ value }}', $this->formatValue($value))
306                     ->setCode(Uuid::TOO_LONG_ERROR)
307                     ->addViolation();
308             }
309         }
310
311         // Check version
312         if (!in_array($value[self::STRICT_VERSION_POSITION], $constraint->versions)) {
313             if ($this->context instanceof ExecutionContextInterface) {
314                 $this->context->buildViolation($constraint->message)
315                     ->setParameter('{{ value }}', $this->formatValue($value))
316                     ->setCode(Uuid::INVALID_VERSION_ERROR)
317                     ->addViolation();
318             } else {
319                 $this->buildViolation($constraint->message)
320                     ->setParameter('{{ value }}', $this->formatValue($value))
321                     ->setCode(Uuid::INVALID_VERSION_ERROR)
322                     ->addViolation();
323             }
324         }
325
326         // Check variant - first two bits must equal "10"
327         //   0b10xx
328         // & 0b1100 (12)
329         // = 0b1000 (8)
330         if ((hexdec($value[self::STRICT_VARIANT_POSITION]) & 12) !== 8) {
331             if ($this->context instanceof ExecutionContextInterface) {
332                 $this->context->buildViolation($constraint->message)
333                     ->setParameter('{{ value }}', $this->formatValue($value))
334                     ->setCode(Uuid::INVALID_VARIANT_ERROR)
335                     ->addViolation();
336             } else {
337                 $this->buildViolation($constraint->message)
338                     ->setParameter('{{ value }}', $this->formatValue($value))
339                     ->setCode(Uuid::INVALID_VARIANT_ERROR)
340                     ->addViolation();
341             }
342         }
343     }
344 }