From e153b3420a3d569ad68af7da7f67e26600bfd8c1 Mon Sep 17 00:00:00 2001 From: Ben Ramsey Date: Tue, 13 Sep 2022 16:39:59 -0500 Subject: [PATCH] refactor: discard unnecessary trait --- src/Nonstandard/UuidV6.php | 40 +++++++++++++++++- src/Rfc4122/UuidV6ConverterTrait.php | 62 ---------------------------- 2 files changed, 38 insertions(+), 64 deletions(-) delete mode 100644 src/Rfc4122/UuidV6ConverterTrait.php diff --git a/src/Nonstandard/UuidV6.php b/src/Nonstandard/UuidV6.php index 62409e5..1a7bbaf 100644 --- a/src/Nonstandard/UuidV6.php +++ b/src/Nonstandard/UuidV6.php @@ -18,10 +18,11 @@ use Ramsey\Uuid\Codec\CodecInterface; use Ramsey\Uuid\Converter\NumberConverterInterface; use Ramsey\Uuid\Converter\TimeConverterInterface; use Ramsey\Uuid\Exception\InvalidArgumentException; +use Ramsey\Uuid\Lazy\LazyUuidFromString; use Ramsey\Uuid\Rfc4122\FieldsInterface as Rfc4122FieldsInterface; use Ramsey\Uuid\Rfc4122\TimeTrait; use Ramsey\Uuid\Rfc4122\UuidInterface; -use Ramsey\Uuid\Rfc4122\UuidV6ConverterTrait; +use Ramsey\Uuid\Rfc4122\UuidV1; use Ramsey\Uuid\Uuid; /** @@ -38,7 +39,6 @@ use Ramsey\Uuid\Uuid; class UuidV6 extends Uuid implements UuidInterface { use TimeTrait; - use UuidV6ConverterTrait; /** * Creates a version 6 (reordered time) UUID @@ -66,4 +66,40 @@ class UuidV6 extends Uuid implements UuidInterface parent::__construct($fields, $numberConverter, $codec, $timeConverter); } + + /** + * Converts this UUID into an instance of a version 1 UUID + */ + public function toUuidV1(): UuidV1 + { + $hex = $this->getHex()->toString(); + $hex = substr($hex, 7, 5) + . substr($hex, 13, 3) + . substr($hex, 3, 4) + . '1' . substr($hex, 0, 3) + . substr($hex, 16); + + /** @var LazyUuidFromString $uuid */ + $uuid = Uuid::fromBytes((string) hex2bin($hex)); + + return $uuid->toUuidV1(); + } + + /** + * Converts a version 1 UUID into an instance of a version 6 UUID + */ + public static function fromUuidV1(UuidV1 $uuidV1): \Ramsey\Uuid\Rfc4122\UuidV6 + { + $hex = $uuidV1->getHex()->toString(); + $hex = substr($hex, 13, 3) + . substr($hex, 8, 4) + . substr($hex, 0, 5) + . '6' . substr($hex, 5, 3) + . substr($hex, 16); + + /** @var LazyUuidFromString $uuid */ + $uuid = Uuid::fromBytes((string) hex2bin($hex)); + + return $uuid->toUuidV6(); + } } diff --git a/src/Rfc4122/UuidV6ConverterTrait.php b/src/Rfc4122/UuidV6ConverterTrait.php deleted file mode 100644 index cca0cff..0000000 --- a/src/Rfc4122/UuidV6ConverterTrait.php +++ /dev/null @@ -1,62 +0,0 @@ - - * @license http://opensource.org/licenses/MIT MIT - */ - -declare(strict_types=1); - -namespace Ramsey\Uuid\Rfc4122; - -use Ramsey\Uuid\Lazy\LazyUuidFromString; -use Ramsey\Uuid\Uuid; - -/** - * Provides functionality to convert between UuidV1 and UuidV6 - * - * @psalm-immutable - */ -trait UuidV6ConverterTrait -{ - /** - * Converts this UUID into an instance of a version 1 UUID - */ - public function toUuidV1(): UuidV1 - { - $hex = $this->getHex()->toString(); - $hex = substr($hex, 7, 5) - . substr($hex, 13, 3) - . substr($hex, 3, 4) - . '1' . substr($hex, 0, 3) - . substr($hex, 16); - - /** @var LazyUuidFromString $uuid */ - $uuid = Uuid::fromBytes((string) hex2bin($hex)); - - return $uuid->toUuidV1(); - } - - /** - * Converts a version 1 UUID into an instance of a version 6 UUID - */ - public static function fromUuidV1(UuidV1 $uuidV1): UuidV6 - { - $hex = $uuidV1->getHex()->toString(); - $hex = substr($hex, 13, 3) - . substr($hex, 8, 4) - . substr($hex, 0, 5) - . '6' . substr($hex, 5, 3) - . substr($hex, 16); - - /** @var LazyUuidFromString $uuid */ - $uuid = Uuid::fromBytes((string) hex2bin($hex)); - - return $uuid->toUuidV6(); - } -}