Ulid.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. * A ULID is lexicographically sortable and contains a 48-bit timestamp and 80-bit of crypto-random entropy.
  14. *
  15. * @see https://github.com/ulid/spec
  16. *
  17. * @author Nicolas Grekas <p@tchwork.com>
  18. */
  19. class Ulid extends AbstractUid implements TimeBasedUidInterface
  20. {
  21. protected const NIL = '00000000000000000000000000';
  22. protected const MAX = '7ZZZZZZZZZZZZZZZZZZZZZZZZZ';
  23. private static string $time = '';
  24. private static array $rand = [];
  25. public function __construct(?string $ulid = null)
  26. {
  27. if (null === $ulid) {
  28. $this->uid = static::generate();
  29. } elseif (self::NIL === $ulid) {
  30. $this->uid = self::NIL;
  31. } else {
  32. $this->uid = strtoupper($ulid);
  33. if (self::MAX === $this->uid) {
  34. $this->uid = self::MAX;
  35. } elseif (!self::isValid($ulid)) {
  36. throw new InvalidArgumentException('Invalid ULID.');
  37. }
  38. }
  39. }
  40. public static function isValid(string $ulid): bool
  41. {
  42. if (26 !== \strlen($ulid)) {
  43. return false;
  44. }
  45. if (26 !== strspn($ulid, '0123456789ABCDEFGHJKMNPQRSTVWXYZabcdefghjkmnpqrstvwxyz')) {
  46. return false;
  47. }
  48. return $ulid[0] <= '7';
  49. }
  50. public static function fromString(string $ulid): static
  51. {
  52. if (36 === \strlen($ulid) && preg_match('{^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$}Di', $ulid)) {
  53. $ulid = hex2bin(str_replace('-', '', $ulid));
  54. } elseif (22 === \strlen($ulid) && 22 === strspn($ulid, BinaryUtil::BASE58[''])) {
  55. $ulid = str_pad(BinaryUtil::fromBase($ulid, BinaryUtil::BASE58), 16, "\0", \STR_PAD_LEFT);
  56. }
  57. if (16 !== \strlen($ulid)) {
  58. return match (strtr($ulid, 'z', 'Z')) {
  59. self::NIL => new NilUlid(),
  60. self::MAX => new MaxUlid(),
  61. default => new static($ulid),
  62. };
  63. }
  64. $ulid = bin2hex($ulid);
  65. $ulid = \sprintf('%02s%04s%04s%04s%04s%04s%04s',
  66. base_convert(substr($ulid, 0, 2), 16, 32),
  67. base_convert(substr($ulid, 2, 5), 16, 32),
  68. base_convert(substr($ulid, 7, 5), 16, 32),
  69. base_convert(substr($ulid, 12, 5), 16, 32),
  70. base_convert(substr($ulid, 17, 5), 16, 32),
  71. base_convert(substr($ulid, 22, 5), 16, 32),
  72. base_convert(substr($ulid, 27, 5), 16, 32)
  73. );
  74. if (self::NIL === $ulid) {
  75. return new NilUlid();
  76. }
  77. if (self::MAX === $ulid = strtr($ulid, 'abcdefghijklmnopqrstuv', 'ABCDEFGHJKMNPQRSTVWXYZ')) {
  78. return new MaxUlid();
  79. }
  80. $u = new static(self::NIL);
  81. $u->uid = $ulid;
  82. return $u;
  83. }
  84. public function toBinary(): string
  85. {
  86. $ulid = strtr($this->uid, 'ABCDEFGHJKMNPQRSTVWXYZ', 'abcdefghijklmnopqrstuv');
  87. $ulid = \sprintf('%02s%05s%05s%05s%05s%05s%05s',
  88. base_convert(substr($ulid, 0, 2), 32, 16),
  89. base_convert(substr($ulid, 2, 4), 32, 16),
  90. base_convert(substr($ulid, 6, 4), 32, 16),
  91. base_convert(substr($ulid, 10, 4), 32, 16),
  92. base_convert(substr($ulid, 14, 4), 32, 16),
  93. base_convert(substr($ulid, 18, 4), 32, 16),
  94. base_convert(substr($ulid, 22, 4), 32, 16)
  95. );
  96. return hex2bin($ulid);
  97. }
  98. /**
  99. * Returns the identifier as a base32 case insensitive string.
  100. *
  101. * @see https://tools.ietf.org/html/rfc4648#section-6
  102. *
  103. * @example 09EJ0S614A9FXVG9C5537Q9ZE1 (len=26)
  104. */
  105. public function toBase32(): string
  106. {
  107. return $this->uid;
  108. }
  109. public function getDateTime(): \DateTimeImmutable
  110. {
  111. $time = strtr(substr($this->uid, 0, 10), 'ABCDEFGHJKMNPQRSTVWXYZ', 'abcdefghijklmnopqrstuv');
  112. if (\PHP_INT_SIZE >= 8) {
  113. $time = (string) hexdec(base_convert($time, 32, 16));
  114. } else {
  115. $time = \sprintf('%02s%05s%05s',
  116. base_convert(substr($time, 0, 2), 32, 16),
  117. base_convert(substr($time, 2, 4), 32, 16),
  118. base_convert(substr($time, 6, 4), 32, 16)
  119. );
  120. $time = BinaryUtil::toBase(hex2bin($time), BinaryUtil::BASE10);
  121. }
  122. if (4 > \strlen($time)) {
  123. $time = '000'.$time;
  124. }
  125. return \DateTimeImmutable::createFromFormat('U.u', substr_replace($time, '.', -3, 0));
  126. }
  127. public static function generate(?\DateTimeInterface $time = null): string
  128. {
  129. if (null === $mtime = $time) {
  130. $time = microtime(false);
  131. $time = substr($time, 11).substr($time, 2, 3);
  132. } elseif (0 > $time = $time->format('Uv')) {
  133. throw new InvalidArgumentException('The timestamp must be positive.');
  134. }
  135. if ($time > self::$time || (null !== $mtime && $time !== self::$time)) {
  136. randomize:
  137. $r = unpack('n*', random_bytes(10));
  138. $r[1] |= ($r[5] <<= 4) & 0xF0000;
  139. $r[2] |= ($r[5] <<= 4) & 0xF0000;
  140. $r[3] |= ($r[5] <<= 4) & 0xF0000;
  141. $r[4] |= ($r[5] <<= 4) & 0xF0000;
  142. unset($r[5]);
  143. self::$rand = $r;
  144. self::$time = $time;
  145. } elseif ([1 => 0xFFFFF, 0xFFFFF, 0xFFFFF, 0xFFFFF] === self::$rand) {
  146. if (\PHP_INT_SIZE >= 8 || 10 > \strlen($time = self::$time)) {
  147. $time = (string) (1 + $time);
  148. } elseif ('999999999' === $mtime = substr($time, -9)) {
  149. $time = (1 + substr($time, 0, -9)).'000000000';
  150. } else {
  151. $time = substr_replace($time, str_pad(++$mtime, 9, '0', \STR_PAD_LEFT), -9);
  152. }
  153. goto randomize;
  154. } else {
  155. for ($i = 4; $i > 0 && 0xFFFFF === self::$rand[$i]; --$i) {
  156. self::$rand[$i] = 0;
  157. }
  158. ++self::$rand[$i];
  159. $time = self::$time;
  160. }
  161. if (\PHP_INT_SIZE >= 8) {
  162. $time = base_convert($time, 10, 32);
  163. } else {
  164. $time = str_pad(bin2hex(BinaryUtil::fromBase($time, BinaryUtil::BASE10)), 12, '0', \STR_PAD_LEFT);
  165. $time = \sprintf('%s%04s%04s',
  166. base_convert(substr($time, 0, 2), 16, 32),
  167. base_convert(substr($time, 2, 5), 16, 32),
  168. base_convert(substr($time, 7, 5), 16, 32)
  169. );
  170. }
  171. return strtr(\sprintf('%010s%04s%04s%04s%04s',
  172. $time,
  173. base_convert(self::$rand[1], 10, 32),
  174. base_convert(self::$rand[2], 10, 32),
  175. base_convert(self::$rand[3], 10, 32),
  176. base_convert(self::$rand[4], 10, 32)
  177. ), 'abcdefghijklmnopqrstuv', 'ABCDEFGHJKMNPQRSTVWXYZ');
  178. }
  179. }