update patch 1
This commit is contained in:
Vendored
+62
-19
@@ -23,6 +23,13 @@ class Uuid extends AbstractUid
|
||||
public const NAMESPACE_OID = '6ba7b812-9dad-11d1-80b4-00c04fd430c8';
|
||||
public const NAMESPACE_X500 = '6ba7b814-9dad-11d1-80b4-00c04fd430c8';
|
||||
|
||||
public const FORMAT_BINARY = 1;
|
||||
public const FORMAT_BASE_32 = 1 << 1;
|
||||
public const FORMAT_BASE_58 = 1 << 2;
|
||||
public const FORMAT_RFC_4122 = 1 << 3;
|
||||
public const FORMAT_RFC_9562 = self::FORMAT_RFC_4122;
|
||||
public const FORMAT_ALL = -1;
|
||||
|
||||
protected const TYPE = 0;
|
||||
protected const NIL = '00000000-0000-0000-0000-000000000000';
|
||||
protected const MAX = 'ffffffff-ffff-ffff-ffff-ffffffffffff';
|
||||
@@ -32,34 +39,19 @@ class Uuid extends AbstractUid
|
||||
$type = preg_match('{^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$}Di', $uuid) ? (int) $uuid[14] : false;
|
||||
|
||||
if (false === $type || (static::TYPE ?: $type) !== $type) {
|
||||
throw new \InvalidArgumentException(sprintf('Invalid UUID%s: "%s".', static::TYPE ? 'v'.static::TYPE : '', $uuid));
|
||||
throw new \InvalidArgumentException(\sprintf('Invalid UUID%s: "%s".', static::TYPE ? 'v'.static::TYPE : '', $uuid));
|
||||
}
|
||||
|
||||
$this->uid = strtolower($uuid);
|
||||
|
||||
if ($checkVariant && !\in_array($this->uid[19], ['8', '9', 'a', 'b'], true)) {
|
||||
throw new \InvalidArgumentException(sprintf('Invalid UUID%s: "%s".', static::TYPE ? 'v'.static::TYPE : '', $uuid));
|
||||
throw new \InvalidArgumentException(\sprintf('Invalid UUID%s: "%s".', static::TYPE ? 'v'.static::TYPE : '', $uuid));
|
||||
}
|
||||
}
|
||||
|
||||
public static function fromString(string $uuid): static
|
||||
{
|
||||
if (22 === \strlen($uuid) && 22 === strspn($uuid, BinaryUtil::BASE58[''])) {
|
||||
$uuid = str_pad(BinaryUtil::fromBase($uuid, BinaryUtil::BASE58), 16, "\0", \STR_PAD_LEFT);
|
||||
}
|
||||
|
||||
if (16 === \strlen($uuid)) {
|
||||
// don't use uuid_unparse(), it's slower
|
||||
$uuid = bin2hex($uuid);
|
||||
$uuid = substr_replace($uuid, '-', 8, 0);
|
||||
$uuid = substr_replace($uuid, '-', 13, 0);
|
||||
$uuid = substr_replace($uuid, '-', 18, 0);
|
||||
$uuid = substr_replace($uuid, '-', 23, 0);
|
||||
} elseif (26 === \strlen($uuid) && Ulid::isValid($uuid)) {
|
||||
$ulid = new NilUlid();
|
||||
$ulid->uid = strtoupper($uuid);
|
||||
$uuid = $ulid->toRfc4122();
|
||||
}
|
||||
$uuid = self::transformToRfc9562($uuid, self::FORMAT_ALL);
|
||||
|
||||
if (__CLASS__ !== static::class || 36 !== \strlen($uuid)) {
|
||||
return new static($uuid);
|
||||
@@ -130,8 +122,21 @@ class Uuid extends AbstractUid
|
||||
return new UuidV8($uuid);
|
||||
}
|
||||
|
||||
public static function isValid(string $uuid): bool
|
||||
/**
|
||||
* @param int-mask-of<Uuid::FORMAT_*> $format
|
||||
*/
|
||||
public static function isValid(string $uuid /* , int $format = self::FORMAT_RFC_9562 */): bool
|
||||
{
|
||||
$format = 1 < \func_num_args() ? func_get_arg(1) : self::FORMAT_RFC_9562;
|
||||
|
||||
if (36 === \strlen($uuid) && !($format & self::FORMAT_RFC_9562)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (false === $uuid = self::transformToRfc9562($uuid, $format)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (self::NIL === $uuid && \in_array(static::class, [__CLASS__, NilUuid::class], true)) {
|
||||
return true;
|
||||
}
|
||||
@@ -182,4 +187,42 @@ class Uuid extends AbstractUid
|
||||
|
||||
return substr_replace($uuid, '-', 23, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms a binary string, a base-32 string or a base-58 string to a RFC9562 string.
|
||||
*
|
||||
* @param int-mask-of<Uuid::FORMAT_*> $format
|
||||
*
|
||||
* @return string|false The RFC9562 string or false if the format doesn't match the input
|
||||
*/
|
||||
private static function transformToRfc9562(string $uuid, int $format): string|false
|
||||
{
|
||||
$inputUuid = $uuid;
|
||||
$fromBase58 = false;
|
||||
if (22 === \strlen($uuid) && 22 === strspn($uuid, BinaryUtil::BASE58['']) && $format & self::FORMAT_BASE_58) {
|
||||
$uuid = str_pad(BinaryUtil::fromBase($uuid, BinaryUtil::BASE58), 16, "\0", \STR_PAD_LEFT);
|
||||
$fromBase58 = true;
|
||||
}
|
||||
|
||||
// base-58 are always transformed to binary string, but they must only be valid when the format is FORMAT_BASE_58
|
||||
if (16 === \strlen($uuid) && $format & self::FORMAT_BINARY || $fromBase58 && $format & self::FORMAT_BASE_58) {
|
||||
// don't use uuid_unparse(), it's slower
|
||||
$uuid = bin2hex($uuid);
|
||||
$uuid = substr_replace($uuid, '-', 8, 0);
|
||||
$uuid = substr_replace($uuid, '-', 13, 0);
|
||||
$uuid = substr_replace($uuid, '-', 18, 0);
|
||||
$uuid = substr_replace($uuid, '-', 23, 0);
|
||||
} elseif (26 === \strlen($uuid) && Ulid::isValid($uuid) && $format & self::FORMAT_BASE_32) {
|
||||
$ulid = new NilUlid();
|
||||
$ulid->uid = strtoupper($uuid);
|
||||
$uuid = $ulid->toRfc4122();
|
||||
}
|
||||
|
||||
if ($inputUuid === $uuid && !($format & self::FORMAT_RFC_9562)) {
|
||||
// input format doesn't match the input string
|
||||
return false;
|
||||
}
|
||||
|
||||
return $uuid;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user