UuidValidator.php 13 KB

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