|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Uid; |
| 13 | + |
| 14 | +/** |
| 15 | + * A v7 UUID is lexicographically sortable and contains a 48-bit timestamp and 74 extra unique bits. |
| 16 | + * |
| 17 | + * Within the same millisecond, monotonicity is ensured by incrementing the random part by a random increment. |
| 18 | + * |
| 19 | + * @author Nicolas Grekas <p@tchwork.com> |
| 20 | + */ |
| 21 | +class UuidV7 extends Uuid implements TimeBasedUidInterface |
| 22 | +{ |
| 23 | + protected const TYPE = 7; |
| 24 | + |
| 25 | + private static string $time = ''; |
| 26 | + private static array $rand = []; |
| 27 | + private static string $seed; |
| 28 | + private static array $seedParts; |
| 29 | + private static int $seedIndex = 0; |
| 30 | + |
| 31 | + public function __construct(string $uuid = null) |
| 32 | + { |
| 33 | + if (null === $uuid) { |
| 34 | + $this->uid = static::generate(); |
| 35 | + } else { |
| 36 | + parent::__construct($uuid, true); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + public function getDateTime(): \DateTimeImmutable |
| 41 | + { |
| 42 | + $time = substr($this->uid, 0, 8).substr($this->uid, 9, 4); |
| 43 | + $time = \PHP_INT_SIZE >= 8 ? (string) hexdec($time) : BinaryUtil::toBase(hex2bin($time), BinaryUtil::BASE10); |
| 44 | + |
| 45 | + if (4 > \strlen($time)) { |
| 46 | + $time = '000'.$time; |
| 47 | + } |
| 48 | + |
| 49 | + return \DateTimeImmutable::createFromFormat('U.v', substr_replace($time, '.', -3, 0)); |
| 50 | + } |
| 51 | + |
| 52 | + public static function generate(\DateTimeInterface $time = null): string |
| 53 | + { |
| 54 | + if (null === $mtime = $time) { |
| 55 | + $time = microtime(false); |
| 56 | + $time = substr($time, 11).substr($time, 2, 3); |
| 57 | + } elseif (0 > $time = $time->format('Uv')) { |
| 58 | + throw new \InvalidArgumentException('The timestamp must be positive.'); |
| 59 | + } |
| 60 | + |
| 61 | + if ($time > self::$time || (null !== $mtime && $time !== self::$time)) { |
| 62 | + randomize: |
| 63 | + self::$rand = unpack('n*', isset(self::$seed) ? random_bytes(10) : self::$seed = random_bytes(16)); |
| 64 | + self::$rand[1] &= 0x03FF; |
| 65 | + self::$time = $time; |
| 66 | + } else { |
| 67 | + if (!self::$seedIndex) { |
| 68 | + $s = unpack('l*', self::$seed = hash('sha512', self::$seed, true)); |
| 69 | + $s[] = ($s[1] >> 8 & 0xFF0000) | ($s[2] >> 16 & 0xFF00) | ($s[3] >> 24 & 0xFF); |
| 70 | + $s[] = ($s[4] >> 8 & 0xFF0000) | ($s[5] >> 16 & 0xFF00) | ($s[6] >> 24 & 0xFF); |
| 71 | + $s[] = ($s[7] >> 8 & 0xFF0000) | ($s[8] >> 16 & 0xFF00) | ($s[9] >> 24 & 0xFF); |
| 72 | + $s[] = ($s[10] >> 8 & 0xFF0000) | ($s[11] >> 16 & 0xFF00) | ($s[12] >> 24 & 0xFF); |
| 73 | + $s[] = ($s[13] >> 8 & 0xFF0000) | ($s[14] >> 16 & 0xFF00) | ($s[15] >> 24 & 0xFF); |
| 74 | + self::$seedParts = $s; |
| 75 | + self::$seedIndex = 21; |
| 76 | + } |
| 77 | + |
| 78 | + self::$rand[5] = 0xFFFF & $carry = self::$rand[5] + (self::$seedParts[self::$seedIndex--] & 0xFFFFFF); |
| 79 | + self::$rand[4] = 0xFFFF & $carry = self::$rand[4] + ($carry >> 16); |
| 80 | + self::$rand[3] = 0xFFFF & $carry = self::$rand[3] + ($carry >> 16); |
| 81 | + self::$rand[2] = 0xFFFF & $carry = self::$rand[2] + ($carry >> 16); |
| 82 | + self::$rand[1] += $carry >> 16; |
| 83 | + |
| 84 | + if (0xFC00 & self::$rand[1]) { |
| 85 | + if (\PHP_INT_SIZE >= 8 || 10 > \strlen($time = self::$time)) { |
| 86 | + $time = (string) (1 + $time); |
| 87 | + } elseif ('999999999' === $mtime = substr($time, -9)) { |
| 88 | + $time = (1 + substr($time, 0, -9)).'000000000'; |
| 89 | + } else { |
| 90 | + $time = substr_replace($time, str_pad(++$mtime, 9, '0', \STR_PAD_LEFT), -9); |
| 91 | + } |
| 92 | + |
| 93 | + goto randomize; |
| 94 | + } |
| 95 | + |
| 96 | + $time = self::$time; |
| 97 | + } |
| 98 | + |
| 99 | + if (\PHP_INT_SIZE >= 8) { |
| 100 | + $time = base_convert($time, 10, 16); |
| 101 | + } else { |
| 102 | + $time = bin2hex(BinaryUtil::fromBase($time, BinaryUtil::BASE10)); |
| 103 | + } |
| 104 | + |
| 105 | + return substr_replace(sprintf('%012s-%04x-%04x-%04x%04x%04x', |
| 106 | + $time, |
| 107 | + 0x7000 | (self::$rand[1] << 2) | (self::$rand[2] >> 14), |
| 108 | + 0x8000 | (self::$rand[2] & 0x3FFF), |
| 109 | + self::$rand[3], |
| 110 | + self::$rand[4], |
| 111 | + self::$rand[5], |
| 112 | + ), '-', 8, 0); |
| 113 | + } |
| 114 | +} |
0 commit comments