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