Uuid.php 5.7 KB

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