mirror of
https://github.com/ramsey/uuid.git
synced 2026-06-14 15:56:48 +03:00
Optimized Uuid::fromBytes() de-serialization to use LazyUuidFromString where possible
This commit is contained in:
@@ -24,6 +24,7 @@ use Ramsey\Uuid\Type\Hexadecimal;
|
||||
use Ramsey\Uuid\Type\Integer as IntegerObject;
|
||||
use Ramsey\Uuid\Uuid;
|
||||
use Ramsey\Uuid\UuidInterface;
|
||||
use function assert;
|
||||
use function hex2bin;
|
||||
use function str_replace;
|
||||
|
||||
@@ -39,7 +40,7 @@ use function str_replace;
|
||||
*/
|
||||
final class LazyUuidFromString implements UuidInterface
|
||||
{
|
||||
public const VALID_PATTERN = '/\A[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}\z/ms';
|
||||
public const VALID_REGEX = '/\A[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\z/ms';
|
||||
|
||||
/** @var non-empty-string */
|
||||
private $uuid;
|
||||
@@ -401,6 +402,15 @@ final class LazyUuidFromString implements UuidInterface
|
||||
return $instance->toUuidV1();
|
||||
}
|
||||
|
||||
public function toUuidV6(): UuidV6
|
||||
{
|
||||
$instance = $this->unwrap();
|
||||
|
||||
assert($instance instanceof UuidV6);
|
||||
|
||||
return $instance;
|
||||
}
|
||||
|
||||
private function unwrap(): UuidInterface
|
||||
{
|
||||
return Uuid::getFactory()
|
||||
|
||||
@@ -21,6 +21,7 @@ use Ramsey\Uuid\Converter\NumberConverterInterface;
|
||||
use Ramsey\Uuid\Converter\TimeConverterInterface;
|
||||
use Ramsey\Uuid\Exception\DateTimeException;
|
||||
use Ramsey\Uuid\Exception\InvalidArgumentException;
|
||||
use Ramsey\Uuid\Lazy\LazyUuidFromString;
|
||||
use Ramsey\Uuid\Rfc4122\FieldsInterface as Rfc4122FieldsInterface;
|
||||
use Ramsey\Uuid\Rfc4122\UuidInterface;
|
||||
use Ramsey\Uuid\Rfc4122\UuidV1;
|
||||
@@ -106,10 +107,10 @@ final class UuidV6 extends Uuid implements UuidInterface
|
||||
. '1' . substr($hex, 0, 3)
|
||||
. substr($hex, 16);
|
||||
|
||||
/** @var UuidV1 $uuid */
|
||||
/** @var LazyUuidFromString $uuid */
|
||||
$uuid = Uuid::fromBytes((string) hex2bin($hex));
|
||||
|
||||
return $uuid;
|
||||
return $uuid->toUuidV1();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -124,9 +125,9 @@ final class UuidV6 extends Uuid implements UuidInterface
|
||||
. '6' . substr($hex, 5, 3)
|
||||
. substr($hex, 16);
|
||||
|
||||
/** @var UuidV6 $uuid */
|
||||
/** @var LazyUuidFromString $uuid */
|
||||
$uuid = Uuid::fromBytes((string) hex2bin($hex));
|
||||
|
||||
return $uuid;
|
||||
return $uuid->toUuidV6();
|
||||
}
|
||||
}
|
||||
|
||||
+19
-2
@@ -28,7 +28,9 @@ use function bin2hex;
|
||||
use function preg_match;
|
||||
use function str_replace;
|
||||
use function strcmp;
|
||||
use function strlen;
|
||||
use function strtolower;
|
||||
use function substr;
|
||||
|
||||
/**
|
||||
* Uuid provides constants and static methods for working with and generating UUIDs
|
||||
@@ -405,7 +407,22 @@ class Uuid implements UuidInterface
|
||||
*/
|
||||
public static function fromBytes(string $bytes): UuidInterface
|
||||
{
|
||||
// @TODO optimize this endpoint too
|
||||
if (! self::$factoryReplaced && strlen($bytes) === 16) {
|
||||
$base16Uuid = bin2hex($bytes);
|
||||
|
||||
return self::fromString(
|
||||
substr($base16Uuid,0, 8)
|
||||
. '-'
|
||||
. substr($base16Uuid, 8, 4)
|
||||
. '-'
|
||||
. substr($base16Uuid, 12, 4)
|
||||
. '-'
|
||||
. substr($base16Uuid, 16, 4)
|
||||
. '-'
|
||||
. substr($base16Uuid, 20, 12)
|
||||
);
|
||||
}
|
||||
|
||||
return self::getFactory()->fromBytes($bytes);
|
||||
}
|
||||
|
||||
@@ -422,7 +439,7 @@ class Uuid implements UuidInterface
|
||||
*/
|
||||
public static function fromString(string $uuid): UuidInterface
|
||||
{
|
||||
if (! self::$factoryReplaced && preg_match(LazyUuidFromString::VALID_PATTERN, $uuid) === 1) {
|
||||
if (! self::$factoryReplaced && preg_match(LazyUuidFromString::VALID_REGEX, $uuid) === 1) {
|
||||
return new LazyUuidFromString(strtolower($uuid));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user