| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
namespace Symfony\Component\Uid; |
| 13 |
|
| 14 |
use Symfony\Component\Uid\Exception\InvalidArgumentException; |
| 15 |
|
| 16 |
|
| 17 |
> |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
class Uuid extends AbstractUid |
| 22 |
{ |
| 23 |
public const NAMESPACE_DNS = '6ba7b810-9dad-11d1-80b4-00c04fd430c8'; |
| 24 |
public const NAMESPACE_URL = '6ba7b811-9dad-11d1-80b4-00c04fd430c8'; |
| 25 |
public const NAMESPACE_OID = '6ba7b812-9dad-11d1-80b4-00c04fd430c8'; |
| 26 |
public const NAMESPACE_X500 = '6ba7b814-9dad-11d1-80b4-00c04fd430c8'; |
| 27 |
|
| 28 |
public const FORMAT_BINARY = 1; |
| 29 |
public const FORMAT_BASE_32 = 1 << 1; |
| 30 |
public const FORMAT_BASE_58 = 1 << 2; |
| 31 |
public const FORMAT_RFC_4122 = 1 << 3; |
| 32 |
public const FORMAT_RFC_9562 = self::FORMAT_RFC_4122; |
| 33 |
public const FORMAT_ALL = -1; |
| 34 |
|
| 35 |
protected const TYPE = 0; |
| 36 |
protected const NIL = '00000000-0000-0000-0000-000000000000'; |
| 37 |
protected const MAX = 'ffffffff-ffff-ffff-ffff-ffffffffffff'; |
| 38 |
|
| 39 |
public function __construct(string $uuid, bool $checkVariant = false) |
| 40 |
{ |
| 41 |
$type = preg_match('{^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$}Di', $uuid) ? (int) $uuid[14] : false; |
| 42 |
|
| 43 |
if (false === $type || (static::TYPE ?: $type) !== $type) { |
| 44 |
throw new InvalidArgumentException(\sprintf('Invalid UUID%s.', static::TYPE ? 'v'.static::TYPE : '')); |
| 45 |
} |
| 46 |
|
| 47 |
$this->uid = strtolower($uuid); |
| 48 |
|
| 49 |
if ($checkVariant && !\in_array($this->uid[19], ['8', '9', 'a', 'b'], true)) { |
| 50 |
throw new InvalidArgumentException(\sprintf('Invalid UUID%s.', static::TYPE ? 'v'.static::TYPE : '')); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
public static function fromString(string $uuid): static |
| 55 |
{ |
| 56 |
$uuid = self::transformToRfc9562($uuid, self::FORMAT_ALL); |
| 57 |
|
| 58 |
if (__CLASS__ !== static::class || 36 !== \strlen($uuid)) { |
| 59 |
return new static($uuid); |
| 60 |
} |
| 61 |
|
| 62 |
if (self::NIL === $uuid) { |
| 63 |
return new NilUuid(); |
| 64 |
} |
| 65 |
|
| 66 |
if (self::MAX === $uuid = strtr($uuid, 'F', 'f')) { |
| 67 |
return new MaxUuid(); |
| 68 |
} |
| 69 |
|
| 70 |
if (!\in_array($uuid[19], ['8', '9', 'a', 'b', 'A', 'B'], true)) { |
| 71 |
return new self($uuid); |
| 72 |
} |
| 73 |
|
| 74 |
return match ((int) $uuid[14]) { |
| 75 |
UuidV1::TYPE => new UuidV1($uuid), |
| 76 |
UuidV3::TYPE => new UuidV3($uuid), |
| 77 |
UuidV4::TYPE => new UuidV4($uuid), |
| 78 |
UuidV5::TYPE => new UuidV5($uuid), |
| 79 |
UuidV6::TYPE => new UuidV6($uuid), |
| 80 |
UuidV7::TYPE => new UuidV7($uuid), |
| 81 |
UuidV8::TYPE => new UuidV8($uuid), |
| 82 |
default => new self($uuid), |
| 83 |
}; |
| 84 |
} |
| 85 |
|
| 86 |
final public static function v1(): UuidV1 |
| 87 |
{ |
| 88 |
return new UuidV1(); |
| 89 |
} |
| 90 |
|
| 91 |
final public static function v3(self $namespace, string $name): UuidV3 |
| 92 |
{ |
| 93 |
|
| 94 |
$uuid = md5(hex2bin(str_replace('-', '', $namespace->uid)).$name, true); |
| 95 |
|
| 96 |
return new UuidV3(self::format($uuid, '-3')); |
| 97 |
} |
| 98 |
|
| 99 |
final public static function v4(): UuidV4 |
| 100 |
{ |
| 101 |
return new UuidV4(); |
| 102 |
} |
| 103 |
|
| 104 |
final public static function v5(self $namespace, string $name): UuidV5 |
| 105 |
{ |
| 106 |
|
| 107 |
$uuid = substr(sha1(hex2bin(str_replace('-', '', $namespace->uid)).$name, true), 0, 16); |
| 108 |
|
| 109 |
return new UuidV5(self::format($uuid, '-5')); |
| 110 |
} |
| 111 |
|
| 112 |
final public static function v6(): UuidV6 |
| 113 |
{ |
| 114 |
return new UuidV6(); |
| 115 |
} |
| 116 |
|
| 117 |
final public static function v7(): UuidV7 |
| 118 |
{ |
| 119 |
return new UuidV7(); |
| 120 |
} |
| 121 |
|
| 122 |
final public static function v8(string $uuid): UuidV8 |
| 123 |
{ |
| 124 |
return new UuidV8($uuid); |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
public static function isValid(string $uuid ): bool |
| 131 |
{ |
| 132 |
$format = 1 < \func_num_args() ? func_get_arg(1) : self::FORMAT_RFC_9562; |
| 133 |
|
| 134 |
if (36 === \strlen($uuid) && !($format & self::FORMAT_RFC_9562)) { |
| 135 |
return false; |
| 136 |
} |
| 137 |
|
| 138 |
if (false === $uuid = self::transformToRfc9562($uuid, $format)) { |
| 139 |
return false; |
| 140 |
} |
| 141 |
|
| 142 |
if (self::NIL === $uuid && \in_array(static::class, [__CLASS__, NilUuid::class], true)) { |
| 143 |
return true; |
| 144 |
} |
| 145 |
|
| 146 |
if (self::MAX === strtr($uuid, 'F', 'f') && \in_array(static::class, [__CLASS__, MaxUuid::class], true)) { |
| 147 |
return true; |
| 148 |
} |
| 149 |
|
| 150 |
if (!preg_match('{^[0-9a-f]{8}(?:-[0-9a-f]{4}){2}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$}Di', $uuid)) { |
| 151 |
return false; |
| 152 |
} |
| 153 |
|
| 154 |
return __CLASS__ === static::class || static::TYPE === (int) $uuid[14]; |
| 155 |
} |
| 156 |
|
| 157 |
public function toBinary(): string |
| 158 |
{ |
| 159 |
return hex2bin(str_replace('-', '', $this->uid)); |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
public function toRfc4122(): string |
| 170 |
{ |
| 171 |
return $this->uid; |
| 172 |
} |
| 173 |
|
| 174 |
public function compare(AbstractUid $other): int |
| 175 |
{ |
| 176 |
if (false !== $cmp = uuid_compare($this->uid, $other->uid)) { |
| 177 |
return $cmp; |
| 178 |
} |
| 179 |
|
| 180 |
return parent::compare($other); |
| 181 |
} |
| 182 |
|
| 183 |
private static function format(string $uuid, string $version): string |
| 184 |
{ |
| 185 |
$uuid[8] = $uuid[8] & "\x3F" | "\x80"; |
| 186 |
$uuid = substr_replace(bin2hex($uuid), '-', 8, 0); |
| 187 |
$uuid = substr_replace($uuid, $version, 13, 1); |
| 188 |
$uuid = substr_replace($uuid, '-', 18, 0); |
| 189 |
|
| 190 |
return substr_replace($uuid, '-', 23, 0); |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
private static function transformToRfc9562(string $uuid, int $format): string|false |
| 201 |
{ |
| 202 |
$inputUuid = $uuid; |
| 203 |
$fromBase58 = false; |
| 204 |
if (22 === \strlen($uuid) && 22 === strspn($uuid, BinaryUtil::BASE58['']) && $format & self::FORMAT_BASE_58) { |
| 205 |
$uuid = str_pad(BinaryUtil::fromBase($uuid, BinaryUtil::BASE58), 16, "\0", \STR_PAD_LEFT); |
| 206 |
$fromBase58 = true; |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
if (16 === \strlen($uuid) && $format & self::FORMAT_BINARY || $fromBase58 && $format & self::FORMAT_BASE_58) { |
| 211 |
|
| 212 |
$uuid = bin2hex($uuid); |
| 213 |
$uuid = substr_replace($uuid, '-', 8, 0); |
| 214 |
$uuid = substr_replace($uuid, '-', 13, 0); |
| 215 |
$uuid = substr_replace($uuid, '-', 18, 0); |
| 216 |
$uuid = substr_replace($uuid, '-', 23, 0); |
| 217 |
} elseif (26 === \strlen($uuid) && Ulid::isValid($uuid) && $format & self::FORMAT_BASE_32) { |
| 218 |
$ulid = new NilUlid(); |
| 219 |
$ulid->uid = strtoupper($uuid); |
| 220 |
$uuid = $ulid->toRfc4122(); |
| 221 |
} |
| 222 |
|
| 223 |
if ($inputUuid === $uuid && !($format & self::FORMAT_RFC_9562)) { |
| 224 |
|
| 225 |
return false; |
| 226 |
} |
| 227 |
|
| 228 |
return $uuid; |
| 229 |
} |
| 230 |
} |