Uuid.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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\Uid;
  11. use Symfony\Component\Uid\Exception\InvalidArgumentException;
  12. /**
  13. * @author Grégoire Pineau <lyrixx@lyrixx.info>
  14. *
  15. * @see https://datatracker.ietf.org/doc/html/rfc9562/#section-6.6 for details about namespaces
  16. */
  17. class Uuid extends AbstractUid
  18. {
  19. public const NAMESPACE_DNS = '6ba7b810-9dad-11d1-80b4-00c04fd430c8';
  20. public const NAMESPACE_URL = '6ba7b811-9dad-11d1-80b4-00c04fd430c8';
  21. public const NAMESPACE_OID = '6ba7b812-9dad-11d1-80b4-00c04fd430c8';
  22. public const NAMESPACE_X500 = '6ba7b814-9dad-11d1-80b4-00c04fd430c8';
  23. public const FORMAT_BINARY = 1;
  24. public const FORMAT_BASE_32 = 1 << 1;
  25. public const FORMAT_BASE_58 = 1 << 2;
  26. public const FORMAT_RFC_4122 = 1 << 3;
  27. public const FORMAT_RFC_9562 = self::FORMAT_RFC_4122;
  28. public const FORMAT_ALL = -1;
  29. protected const TYPE = 0;
  30. protected const NIL = '00000000-0000-0000-0000-000000000000';
  31. protected const MAX = 'ffffffff-ffff-ffff-ffff-ffffffffffff';
  32. public function __construct(string $uuid, bool $checkVariant = false)
  33. {
  34. $type = preg_match('{^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$}Di', $uuid) ? (int) $uuid[14] : false;
  35. if (false === $type || (static::TYPE ?: $type) !== $type) {
  36. throw new InvalidArgumentException(\sprintf('Invalid UUID%s.', static::TYPE ? 'v'.static::TYPE : ''));
  37. }
  38. $this->uid = strtolower($uuid);
  39. if ($checkVariant && !\in_array($this->uid[19], ['8', '9', 'a', 'b'], true)) {
  40. throw new InvalidArgumentException(\sprintf('Invalid UUID%s.', static::TYPE ? 'v'.static::TYPE : ''));
  41. }
  42. }
  43. public static function fromString(string $uuid): static
  44. {
  45. $uuid = self::transformToRfc9562($uuid, self::FORMAT_ALL);
  46. if (__CLASS__ !== static::class || 36 !== \strlen($uuid)) {
  47. return new static($uuid);
  48. }
  49. if (self::NIL === $uuid) {
  50. return new NilUuid();
  51. }
  52. if (self::MAX === $uuid = strtr($uuid, 'F', 'f')) {
  53. return new MaxUuid();
  54. }
  55. if (!\in_array($uuid[19], ['8', '9', 'a', 'b', 'A', 'B'], true)) {
  56. return new self($uuid);
  57. }
  58. return match ((int) $uuid[14]) {
  59. UuidV1::TYPE => new UuidV1($uuid),
  60. UuidV3::TYPE => new UuidV3($uuid),
  61. UuidV4::TYPE => new UuidV4($uuid),
  62. UuidV5::TYPE => new UuidV5($uuid),
  63. UuidV6::TYPE => new UuidV6($uuid),
  64. UuidV7::TYPE => new UuidV7($uuid),
  65. UuidV8::TYPE => new UuidV8($uuid),
  66. default => new self($uuid),
  67. };
  68. }
  69. final public static function v1(): UuidV1
  70. {
  71. return new UuidV1();
  72. }
  73. final public static function v3(self $namespace, string $name): UuidV3
  74. {
  75. // don't use uuid_generate_md5(), some versions are buggy
  76. $uuid = md5(hex2bin(str_replace('-', '', $namespace->uid)).$name, true);
  77. return new UuidV3(self::format($uuid, '-3'));
  78. }
  79. final public static function v4(): UuidV4
  80. {
  81. return new UuidV4();
  82. }
  83. final public static function v5(self $namespace, string $name): UuidV5
  84. {
  85. // don't use uuid_generate_sha1(), some versions are buggy
  86. $uuid = substr(sha1(hex2bin(str_replace('-', '', $namespace->uid)).$name, true), 0, 16);
  87. return new UuidV5(self::format($uuid, '-5'));
  88. }
  89. final public static function v6(): UuidV6
  90. {
  91. return new UuidV6();
  92. }
  93. final public static function v7(): UuidV7
  94. {
  95. return new UuidV7();
  96. }
  97. final public static function v8(string $uuid): UuidV8
  98. {
  99. return new UuidV8($uuid);
  100. }
  101. /**
  102. * @param int-mask-of<Uuid::FORMAT_*> $format
  103. */
  104. public static function isValid(string $uuid /* , int $format = self::FORMAT_RFC_9562 */): bool
  105. {
  106. $format = 1 < \func_num_args() ? func_get_arg(1) : self::FORMAT_RFC_9562;
  107. if (36 === \strlen($uuid) && !($format & self::FORMAT_RFC_9562)) {
  108. return false;
  109. }
  110. if (false === $uuid = self::transformToRfc9562($uuid, $format)) {
  111. return false;
  112. }
  113. if (self::NIL === $uuid && \in_array(static::class, [__CLASS__, NilUuid::class], true)) {
  114. return true;
  115. }
  116. if (self::MAX === strtr($uuid, 'F', 'f') && \in_array(static::class, [__CLASS__, MaxUuid::class], true)) {
  117. return true;
  118. }
  119. if (!preg_match('{^[0-9a-f]{8}(?:-[0-9a-f]{4}){2}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$}Di', $uuid)) {
  120. return false;
  121. }
  122. return __CLASS__ === static::class || static::TYPE === (int) $uuid[14];
  123. }
  124. public function toBinary(): string
  125. {
  126. return hex2bin(str_replace('-', '', $this->uid));
  127. }
  128. /**
  129. * Returns the identifier as a RFC 9562/4122 case insensitive string.
  130. *
  131. * @see https://datatracker.ietf.org/doc/html/rfc9562/#section-4
  132. *
  133. * @example 09748193-048a-4bfb-b825-8528cf74fdc1 (len=36)
  134. */
  135. public function toRfc4122(): string
  136. {
  137. return $this->uid;
  138. }
  139. public function compare(AbstractUid $other): int
  140. {
  141. if (false !== $cmp = uuid_compare($this->uid, $other->uid)) {
  142. return $cmp;
  143. }
  144. return parent::compare($other);
  145. }
  146. private static function format(string $uuid, string $version): string
  147. {
  148. $uuid[8] = $uuid[8] & "\x3F" | "\x80";
  149. $uuid = substr_replace(bin2hex($uuid), '-', 8, 0);
  150. $uuid = substr_replace($uuid, $version, 13, 1);
  151. $uuid = substr_replace($uuid, '-', 18, 0);
  152. return substr_replace($uuid, '-', 23, 0);
  153. }
  154. /**
  155. * Transforms a binary string, a base-32 string or a base-58 string to a RFC9562 string.
  156. *
  157. * @param int-mask-of<Uuid::FORMAT_*> $format
  158. *
  159. * @return string|false The RFC9562 string or false if the format doesn't match the input
  160. */
  161. private static function transformToRfc9562(string $uuid, int $format): string|false
  162. {
  163. $inputUuid = $uuid;
  164. $fromBase58 = false;
  165. if (22 === \strlen($uuid) && 22 === strspn($uuid, BinaryUtil::BASE58['']) && $format & self::FORMAT_BASE_58) {
  166. $uuid = str_pad(BinaryUtil::fromBase($uuid, BinaryUtil::BASE58), 16, "\0", \STR_PAD_LEFT);
  167. $fromBase58 = true;
  168. }
  169. // base-58 are always transformed to binary string, but they must only be valid when the format is FORMAT_BASE_58
  170. if (16 === \strlen($uuid) && $format & self::FORMAT_BINARY || $fromBase58 && $format & self::FORMAT_BASE_58) {
  171. // don't use uuid_unparse(), it's slower
  172. $uuid = bin2hex($uuid);
  173. $uuid = substr_replace($uuid, '-', 8, 0);
  174. $uuid = substr_replace($uuid, '-', 13, 0);
  175. $uuid = substr_replace($uuid, '-', 18, 0);
  176. $uuid = substr_replace($uuid, '-', 23, 0);
  177. } elseif (26 === \strlen($uuid) && Ulid::isValid($uuid) && $format & self::FORMAT_BASE_32) {
  178. $ulid = new NilUlid();
  179. $ulid->uid = strtoupper($uuid);
  180. $uuid = $ulid->toRfc4122();
  181. }
  182. if ($inputUuid === $uuid && !($format & self::FORMAT_RFC_9562)) {
  183. // input format doesn't match the input string
  184. return false;
  185. }
  186. return $uuid;
  187. }
  188. }