From ad75532dc87a14ada24ab076e0aaf6b17ea28527 Mon Sep 17 00:00:00 2001 From: Ben Ramsey Date: Fri, 21 Feb 2020 12:16:49 -0600 Subject: [PATCH] Add a `Type\TypeInterface` that each of the ramsey/uuid types implements --- CHANGELOG.md | 2 ++ src/Type/Hexadecimal.php | 2 +- src/Type/NumberInterface.php | 5 +---- src/Type/Time.php | 12 +++++++++++- src/Type/TypeInterface.php | 27 +++++++++++++++++++++++++++ tests/Type/TimeTest.php | 6 ++++++ 6 files changed, 48 insertions(+), 6 deletions(-) create mode 100644 src/Type/TypeInterface.php diff --git a/CHANGELOG.md b/CHANGELOG.md index c633464..5c503eb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ### Added +* Add a `Type\TypeInterface` that each of the ramsey/uuid types implements. + ### Changed * Rename `Type\IntegerValue` to `Type\Integer`. It was originally named diff --git a/src/Type/Hexadecimal.php b/src/Type/Hexadecimal.php index 2d22fc6..7e508f9 100644 --- a/src/Type/Hexadecimal.php +++ b/src/Type/Hexadecimal.php @@ -30,7 +30,7 @@ use function substr; * * @psalm-immutable */ -final class Hexadecimal +final class Hexadecimal implements TypeInterface { /** * @var string diff --git a/src/Type/NumberInterface.php b/src/Type/NumberInterface.php index af6e435..fdfa535 100644 --- a/src/Type/NumberInterface.php +++ b/src/Type/NumberInterface.php @@ -19,9 +19,6 @@ namespace Ramsey\Uuid\Type; * * @psalm-immutable */ -interface NumberInterface +interface NumberInterface extends TypeInterface { - public function toString(): string; - - public function __toString(): string; } diff --git a/src/Type/Time.php b/src/Type/Time.php index 3e72d48..b4996fc 100644 --- a/src/Type/Time.php +++ b/src/Type/Time.php @@ -25,7 +25,7 @@ use Ramsey\Uuid\Type\Integer as IntegerObject; * * @psalm-immutable */ -final class Time +final class Time implements TypeInterface { /** * @var IntegerObject @@ -56,4 +56,14 @@ final class Time { return $this->microSeconds; } + + public function toString(): string + { + return $this->seconds->toString() . '.' . $this->microSeconds->toString(); + } + + public function __toString(): string + { + return $this->toString(); + } } diff --git a/src/Type/TypeInterface.php b/src/Type/TypeInterface.php new file mode 100644 index 0000000..afa1e5b --- /dev/null +++ b/src/Type/TypeInterface.php @@ -0,0 +1,27 @@ + + * @license http://opensource.org/licenses/MIT MIT + */ + +declare(strict_types=1); + +namespace Ramsey\Uuid\Type; + +/** + * TypeInterface ensures consistency in typed values returned by ramsey/uuid + * + * @psalm-immutable + */ +interface TypeInterface +{ + public function toString(): string; + + public function __toString(): string; +} diff --git a/tests/Type/TimeTest.php b/tests/Type/TimeTest.php index 42f691b..ee8fb97 100644 --- a/tests/Type/TimeTest.php +++ b/tests/Type/TimeTest.php @@ -18,9 +18,13 @@ class TimeTest extends TestCase public function testTime($seconds, $microSeconds): void { $params = [$seconds]; + $timeString = (string) $seconds; if ($microSeconds !== null) { $params[] = $microSeconds; + $timeString .= ".{$microSeconds}"; + } else { + $timeString .= '.0'; } $time = new Time(...$params); @@ -31,6 +35,8 @@ class TimeTest extends TestCase (string) $microSeconds ?: '0', $time->getMicroSeconds()->toString() ); + + $this->assertSame($timeString, (string) $time); } /**