diff --git a/CHANGELOG.md b/CHANGELOG.md index 6b4a6f4..953f3da 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ### Changed * Add `fromDateTime()` method to `UuidFactoryInterface`. +* Change `UuidInterface::getHex()` to return a `Ramsey\Uuid\Type\Hexadecimal` instance. +* Change `UuidInterface::getInteger()` to return a `Ramsey\Uuid\Type\IntegerValue` instance. ### Deprecated diff --git a/src/DeprecatedUuidMethodsTrait.php b/src/DeprecatedUuidMethodsTrait.php index 5c1cfcb..d1a3b9a 100644 --- a/src/DeprecatedUuidMethodsTrait.php +++ b/src/DeprecatedUuidMethodsTrait.php @@ -180,7 +180,7 @@ trait DeprecatedUuidMethodsTrait */ public function getLeastSignificantBits(): string { - $leastSignificantHex = substr($this->getHex(), 16); + $leastSignificantHex = substr($this->getHex()->toString(), 16); return $this->numberConverter->fromHex($leastSignificantHex); } @@ -192,7 +192,7 @@ trait DeprecatedUuidMethodsTrait */ public function getLeastSignificantBitsHex(): string { - return substr($this->getHex(), 16); + return substr($this->getHex()->toString(), 16); } /** @@ -202,7 +202,7 @@ trait DeprecatedUuidMethodsTrait */ public function getMostSignificantBits(): string { - $mostSignificantHex = substr($this->getHex(), 0, 16); + $mostSignificantHex = substr($this->getHex()->toString(), 0, 16); return $this->numberConverter->fromHex($mostSignificantHex); } @@ -214,7 +214,7 @@ trait DeprecatedUuidMethodsTrait */ public function getMostSignificantBitsHex(): string { - return substr($this->getHex(), 0, 16); + return substr($this->getHex()->toString(), 0, 16); } /** diff --git a/src/Uuid.php b/src/Uuid.php index 10de927..40527e6 100644 --- a/src/Uuid.php +++ b/src/Uuid.php @@ -271,7 +271,7 @@ class Uuid implements UuidInterface public function compareTo(UuidInterface $other): int { - $compare = strcmp($this->getInteger(), $other->getInteger()); + $compare = strcmp($this->getInteger()->toString(), $other->getInteger()->toString()); if ($compare < 0) { return -1; @@ -304,20 +304,14 @@ class Uuid implements UuidInterface return $this->fields; } - /** - * @psalm-return non-empty-string - * @psalm-suppress MoreSpecificReturnType we know that the retrieved `string` is never empty - * @psalm-suppress LessSpecificReturnStatement we know that the retrieved `string` is never empty - */ - public function getHex(): string + public function getHex(): Hexadecimal { - return str_replace('-', '', $this->toString()); + return new Hexadecimal(str_replace('-', '', $this->toString())); } - /** @psalm-return non-empty-string */ - public function getInteger(): string + public function getInteger(): IntegerValue { - return $this->numberConverter->fromHex($this->getHex()); + return new IntegerValue($this->numberConverter->fromHex($this->getHex()->toString())); } /** @psalm-return non-empty-string */ diff --git a/src/UuidInterface.php b/src/UuidInterface.php index 0296e7d..901aa60 100644 --- a/src/UuidInterface.php +++ b/src/UuidInterface.php @@ -16,6 +16,8 @@ namespace Ramsey\Uuid; use JsonSerializable; use Ramsey\Uuid\Fields\FieldsInterface; +use Ramsey\Uuid\Type\Hexadecimal; +use Ramsey\Uuid\Type\IntegerValue; use Serializable; /** @@ -73,17 +75,13 @@ interface UuidInterface extends /** * Returns the hexadecimal string representation of the UUID - * - * @psalm-return non-empty-string */ - public function getHex(): string; + public function getHex(): Hexadecimal; /** * Returns the integer value of the UUID as a string - * - * @psalm-return non-empty-string */ - public function getInteger(): string; + public function getInteger(): IntegerValue; /** * Returns a string representation of the UUID diff --git a/tests/ExpectedBehaviorTest.php b/tests/ExpectedBehaviorTest.php index d50344b..c0a70ca 100644 --- a/tests/ExpectedBehaviorTest.php +++ b/tests/ExpectedBehaviorTest.php @@ -48,7 +48,7 @@ class ExpectedBehaviorTest extends TestCase $this->assertTrue($uuid->equals(clone $uuid)); $this->assertIsString($uuid->getBytes()); $this->assertInstanceOf('Ramsey\Uuid\Converter\NumberConverterInterface', $uuid->getNumberConverter()); - $this->assertIsString($uuid->getHex()); + $this->assertIsString((string) $uuid->getHex()); $this->assertIsArray($uuid->getFieldsHex()); $this->assertArrayHasKey('time_low', $uuid->getFieldsHex()); $this->assertArrayHasKey('time_mid', $uuid->getFieldsHex()); @@ -68,11 +68,11 @@ class ExpectedBehaviorTest extends TestCase $this->assertSame($uuid->getFieldsHex()['clock_seq_hi_and_reserved'], $uuid->getClockSeqHiAndReservedHex()); $this->assertSame($uuid->getFieldsHex()['clock_seq_low'], $uuid->getClockSeqLowHex()); $this->assertSame($uuid->getFieldsHex()['node'], $uuid->getNodeHex()); - $this->assertSame(substr($uuid->getHex(), 16), $uuid->getLeastSignificantBitsHex()); - $this->assertSame(substr($uuid->getHex(), 0, 16), $uuid->getMostSignificantBitsHex()); + $this->assertSame(substr((string) $uuid->getHex(), 16), $uuid->getLeastSignificantBitsHex()); + $this->assertSame(substr((string) $uuid->getHex(), 0, 16), $uuid->getMostSignificantBitsHex()); $this->assertSame( - $uuid->getHex(), + (string) $uuid->getHex(), $uuid->getTimeLowHex() . $uuid->getTimeMidHex() . $uuid->getTimeHiAndVersionHex() @@ -82,7 +82,7 @@ class ExpectedBehaviorTest extends TestCase ); $this->assertSame( - $uuid->getHex(), + (string) $uuid->getHex(), $uuid->getFieldsHex()['time_low'] . $uuid->getFieldsHex()['time_mid'] . $uuid->getFieldsHex()['time_hi_and_version'] @@ -93,9 +93,9 @@ class ExpectedBehaviorTest extends TestCase $this->assertIsString($uuid->getUrn()); $this->assertStringStartsWith('urn:uuid:', $uuid->getUrn()); - $this->assertSame('urn:uuid:' . $uuid->getHex(), str_replace('-', '', $uuid->getUrn())); - $this->assertSame($uuid->getHex(), str_replace('-', '', $uuid->toString())); - $this->assertSame($uuid->getHex(), str_replace('-', '', (string) $uuid)); + $this->assertSame('urn:uuid:' . (string) $uuid->getHex(), str_replace('-', '', $uuid->getUrn())); + $this->assertSame((string) $uuid->getHex(), str_replace('-', '', $uuid->toString())); + $this->assertSame((string) $uuid->getHex(), str_replace('-', '', (string) $uuid)); $this->assertSame( $uuid->toString(), @@ -622,6 +622,6 @@ class ExpectedBehaviorTest extends TestCase $this->assertSame(2, $uuid->getVariant()); $this->assertSame(4, $uuid->getVersion()); $this->assertSame($expectedBytes, $uuid->getBytes()); - $this->assertSame($expectedHex, $uuid->getHex()); + $this->assertSame($expectedHex, (string) $uuid->getHex()); } } diff --git a/tests/UuidTest.php b/tests/UuidTest.php index 28a6e6b..d3347f4 100644 --- a/tests/UuidTest.php +++ b/tests/UuidTest.php @@ -976,7 +976,7 @@ class UuidTest extends TestCase public function testFromInteger(): void { $uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66'); - $integer = $uuid->getInteger(); + $integer = $uuid->getInteger()->toString(); $fromIntegerUuid = Uuid::fromInteger($integer); @@ -1029,9 +1029,9 @@ class UuidTest extends TestCase /** @var UuidInterface $uuid */ foreach ($uuids as $uuid) { $this->assertSame($string, $uuid->toString()); - $this->assertSame($hex, $uuid->getHex()); + $this->assertSame($hex, $uuid->getHex()->toString()); $this->assertSame(base64_decode($bytes), $uuid->getBytes()); - $this->assertSame($int, $uuid->getInteger()); + $this->assertSame($int, $uuid->getInteger()->toString()); $this->assertSame($fields, $uuid->getFieldsHex()); $this->assertSame($fields['time_low'], $uuid->getTimeLowHex()); $this->assertSame($fields['time_mid'], $uuid->getTimeMidHex()); diff --git a/tests/static-analysis/UuidIsImmutable.php b/tests/static-analysis/UuidIsImmutable.php index 9fbea29..6b5a246 100644 --- a/tests/static-analysis/UuidIsImmutable.php +++ b/tests/static-analysis/UuidIsImmutable.php @@ -85,7 +85,7 @@ final class UuidIsImmutable return [ Uuid::fromBytes($id->getBytes()), - Uuid::fromInteger($id->getInteger()), + Uuid::fromInteger($id->getInteger()->toString()), Uuid::isValid('ff6f8cb0-c57d-11e1-9b21-0800200c9a66'), ]; } diff --git a/tests/static-analysis/UuidIsNeverEmpty.php b/tests/static-analysis/UuidIsNeverEmpty.php index 524eb5b..0ff395f 100644 --- a/tests/static-analysis/UuidIsNeverEmpty.php +++ b/tests/static-analysis/UuidIsNeverEmpty.php @@ -28,18 +28,6 @@ final class UuidIsNeverEmpty return $uuid->getBytes(); } - /** @psalm-return non-empty-string */ - public function hexIsNeverEmpty(UuidInterface $uuid): string - { - return $uuid->getHex(); - } - - /** @psalm-return non-empty-string */ - public function integerIsNeverEmpty(UuidInterface $uuid): string - { - return $uuid->getInteger(); - } - /** @psalm-return non-empty-string */ public function stringIsNeverEmpty(UuidInterface $uuid): string {