UuidValidator.php 8.4 KB

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