diff --git a/src/Lazy/LazyUuidFromString.php b/src/Lazy/LazyUuidFromString.php index 031f0c9..fd15b14 100644 --- a/src/Lazy/LazyUuidFromString.php +++ b/src/Lazy/LazyUuidFromString.php @@ -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() diff --git a/src/Nonstandard/UuidV6.php b/src/Nonstandard/UuidV6.php index 454d04c..05586b3 100644 --- a/src/Nonstandard/UuidV6.php +++ b/src/Nonstandard/UuidV6.php @@ -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(); } } diff --git a/src/Uuid.php b/src/Uuid.php index 091348b..3742caf 100644 --- a/src/Uuid.php +++ b/src/Uuid.php @@ -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)); }