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