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