diff --git a/CHANGELOG.md b/CHANGELOG.md index afb5669..baf8bad 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -34,6 +34,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. * `getTimeLowHex()` * `getTimeMid()` * `getTimeMidHex()` + * `getTimestamp()` + * `getTimestampHex()` * Remove dependency on ramsey/collection package. diff --git a/phpstan.neon.dist b/phpstan.neon.dist index aec6144..777e304 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -14,7 +14,7 @@ parameters: - # Legacy methods of `Ramsey\Uuid` use interface methods that are NOT defined on `FieldsInterface` message: '#^Call to an undefined method Ramsey\\Uuid\\Fields\\FieldsInterface::get.*$#' - count: 6 + count: 4 path: ./src/Lazy/LazyUuidFromString.php - message: '#^Result of \|\| is always false\.$#' diff --git a/src/DeprecatedUuidInterface.php b/src/DeprecatedUuidInterface.php index 5a1c710..a3469df 100644 --- a/src/DeprecatedUuidInterface.php +++ b/src/DeprecatedUuidInterface.php @@ -53,14 +53,6 @@ interface DeprecatedUuidInterface */ public function getNodeHex(): string; - /** - * @deprecated Use {@see UuidInterface::getFields()} to get a - * {@see FieldsInterface} instance. If it is a - * {@see \Ramsey\Uuid\Rfc4122\FieldsInterface} instance, you may call - * {@see \Ramsey\Uuid\Rfc4122\FieldsInterface::getTimestamp()}. - */ - public function getTimestampHex(): string; - /** * @deprecated In ramsey/uuid version 5.0.0, this will be removed from this * interface. It has moved to {@see \Ramsey\Uuid\Rfc4122\UuidInterface::getUrn()}. diff --git a/src/DeprecatedUuidMethodsTrait.php b/src/DeprecatedUuidMethodsTrait.php index 75df242..72fbaf3 100644 --- a/src/DeprecatedUuidMethodsTrait.php +++ b/src/DeprecatedUuidMethodsTrait.php @@ -164,36 +164,6 @@ trait DeprecatedUuidMethodsTrait return $this->fields->getNode()->toString(); } - /** - * @deprecated Use {@see UuidInterface::getFields()} to get a - * {@see FieldsInterface} instance. If it is a {@see Rfc4122FieldsInterface} - * instance, you may call {@see Rfc4122FieldsInterface::getTimestamp()} - * and use the arbitrary-precision math library of your choice to - * convert it to a string integer. - */ - public function getTimestamp(): string - { - if ($this->fields->getVersion() !== 1) { - throw new UnsupportedOperationException('Not a time-based UUID'); - } - - return $this->numberConverter->fromHex($this->fields->getTimestamp()->toString()); - } - - /** - * @deprecated Use {@see UuidInterface::getFields()} to get a - * {@see FieldsInterface} instance. If it is a {@see Rfc4122FieldsInterface} - * instance, you may call {@see Rfc4122FieldsInterface::getTimestamp()}. - */ - public function getTimestampHex(): string - { - if ($this->fields->getVersion() !== 1) { - throw new UnsupportedOperationException('Not a time-based UUID'); - } - - return $this->fields->getTimestamp()->toString(); - } - /** * @deprecated This has moved to {@see Rfc4122FieldsInterface::getUrn()} and * is available on {@see \Ramsey\Uuid\Rfc4122\UuidV1}, diff --git a/src/Lazy/LazyUuidFromString.php b/src/Lazy/LazyUuidFromString.php index 45a9733..92f724b 100644 --- a/src/Lazy/LazyUuidFromString.php +++ b/src/Lazy/LazyUuidFromString.php @@ -16,7 +16,6 @@ namespace Ramsey\Uuid\Lazy; use DateTimeInterface; use Ramsey\Uuid\Converter\NumberConverterInterface; -use Ramsey\Uuid\Exception\UnsupportedOperationException; use Ramsey\Uuid\Fields\FieldsInterface; use Ramsey\Uuid\Nonstandard\UuidV6; use Ramsey\Uuid\Rfc4122\UuidV1; @@ -162,13 +161,6 @@ final class LazyUuidFromString implements UuidInterface ->getNodeHex(); } - /** @psalm-suppress DeprecatedMethod */ - public function getTimestampHex(): string - { - return ($this->unwrapped ?? $this->unwrap()) - ->getTimestampHex(); - } - /** @psalm-suppress DeprecatedMethod */ public function getUrn(): string { @@ -347,31 +339,6 @@ final class LazyUuidFromString implements UuidInterface ); } - /** - * @deprecated Use {@see UuidInterface::getFields()} to get a - * {@see FieldsInterface} instance. If it is a {@see Rfc4122FieldsInterface} - * instance, you may call {@see Rfc4122FieldsInterface::getTimestamp()} - * and use the arbitrary-precision math library of your choice to - * convert it to a string integer. - * - * @psalm-suppress UndefinedInterfaceMethod - * @psalm-suppress DeprecatedMethod - * @psalm-suppress MixedArgument - * @psalm-suppress MixedMethodCall - */ - public function getTimestamp(): string - { - $instance = ($this->unwrapped ?? $this->unwrap()); - $fields = $instance->getFields(); - - if ($fields->getVersion() !== 1) { - throw new UnsupportedOperationException('Not a time-based UUID'); - } - - return $instance->getNumberConverter() - ->fromHex($fields->getTimestamp()->toString()); - } - public function toUuidV1(): UuidV1 { $instance = ($this->unwrapped ?? $this->unwrap()); diff --git a/tests/ExpectedBehaviorTest.php b/tests/ExpectedBehaviorTest.php index 6ebede2..1e5d91b 100644 --- a/tests/ExpectedBehaviorTest.php +++ b/tests/ExpectedBehaviorTest.php @@ -156,7 +156,6 @@ class ExpectedBehaviorTest extends TestCase $this->assertSame('281474976710655', (string) $uuid->getNode()); $this->assertSame('3fff', $uuid->getFields()->getClockSeq()->toString()); $this->assertSame('16383', (string) $uuid->getClockSequence()); - $this->assertTrue(ctype_digit((string) $uuid->getTimestamp())); } /** diff --git a/tests/UuidTest.php b/tests/UuidTest.php index 518fb1d..25c32f4 100644 --- a/tests/UuidTest.php +++ b/tests/UuidTest.php @@ -338,51 +338,28 @@ class UuidTest extends TestCase $this->assertSame('c57d', $uuid->getFields()->getTimeMid()->toString()); } - public function testGetTimestamp(): void - { - // Check for a recent date - /** @var Uuid $uuid */ - $uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66'); - $this->assertSame('135606608744910000', $uuid->getTimestamp()); - - // Check for an old date - /** @var Uuid $uuid */ - $uuid = Uuid::fromString('0901e600-0154-1000-9b21-0800200c9a66'); - $this->assertSame('1460440000000', $uuid->getTimestamp()); - } - public function testGetTimestampHex(): void { // Check for a recent date + /** @var \Ramsey\Uuid\Rfc4122\UuidInterface $uuid */ $uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66'); - $this->assertSame('1e1c57dff6f8cb0', $uuid->getTimestampHex()); + $this->assertSame('1e1c57dff6f8cb0', $uuid->getFields()->getTimestamp()->toString()); // Check for an old date + /** @var \Ramsey\Uuid\Rfc4122\UuidInterface $uuid */ $uuid = Uuid::fromString('0901e600-0154-1000-9b21-0800200c9a66'); - $this->assertSame('00001540901e600', $uuid->getTimestampHex()); - } - - public function testGetTimestampFromNonVersion1Uuid(): void - { - // Using a version 4 UUID to test - /** @var Uuid $uuid */ - $uuid = Uuid::fromString('bf17b594-41f2-474f-bf70-4c90220f75de'); - - $this->expectException(UnsupportedOperationException::class); - $this->expectExceptionMessage('Not a time-based UUID'); - - $uuid->getTimestamp(); + $this->assertSame('00001540901e600', $uuid->getFields()->getTimestamp()->toString()); } public function testGetTimestampHexFromNonVersion1Uuid(): void { // Using a version 4 UUID to test + /** @var \Ramsey\Uuid\Rfc4122\UuidInterface $uuid */ $uuid = Uuid::fromString('bf17b594-41f2-474f-bf70-4c90220f75de'); - $this->expectException(UnsupportedOperationException::class); - $this->expectExceptionMessage('Not a time-based UUID'); - - $uuid->getTimestampHex(); + // This threw an exception in versions of ramsey/uuid earlier than 5.0.0. + // The test is here now to ensure we don't throw an exception. + $this->assertSame('74f41f2bf17b594', $uuid->getFields()->getTimestamp()->toString()); } public function testGetUrn(): void @@ -1188,9 +1165,7 @@ class UuidTest extends TestCase ); $this->assertSame($fields['node'], $uuid->getNodeHex()); $this->assertSame($urn, $uuid->getUrn()); - if ($uuid->getVersion() === 1) { - $this->assertSame($time, $uuid->getTimestampHex()); - } + $this->assertSame($time, $uuid->getFields()->getTimestamp()->toString()); $this->assertSame($clockSeq, $uuid->getFields()->getClockSeq()->toString()); $this->assertSame($variant, $uuid->getVariant()); $this->assertSame($version, $uuid->getVersion()); @@ -1222,7 +1197,7 @@ class UuidTest extends TestCase 'node' => '000000000000', ], 'urn' => 'urn:uuid:00000000-0000-0000-0000-000000000000', - 'time' => '0', + 'time' => '000000000000000', 'clock_seq' => '0000', 'variant' => Uuid::RESERVED_NCS, 'version' => null, diff --git a/tests/static-analysis/UuidIsImmutable.php b/tests/static-analysis/UuidIsImmutable.php index 6d421b5..b53982f 100644 --- a/tests/static-analysis/UuidIsImmutable.php +++ b/tests/static-analysis/UuidIsImmutable.php @@ -57,7 +57,6 @@ final class UuidIsImmutable $a->getDateTime(), $a->getInteger(), $a->getNodeHex(), - $a->getTimestampHex(), $a->getUrn(), $a->getVariant(), $a->getVersion(),