From 1d54bcd1fb80f34075da9f836b6e0416b6bd7da2 Mon Sep 17 00:00:00 2001 From: Christopher Gammie Date: Sat, 24 May 2025 14:18:10 +0100 Subject: [PATCH 1/3] feat!: improve guid make method to parse uuids to guids --- CHANGELOG.md | 5 +++++ src/Toolkit/Identifiers/Guid.php | 16 +++++++------ tests/Unit/Toolkit/Identifiers/GuidTest.php | 25 +++++++++++++++++++++ 3 files changed, 39 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 55fe5b3..6e8e466 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file. This projec ## Next Major Release +### Changed + +- **BREAKING**: The `Guid::make()` method will now convert a string that is a UUID to a UUID GUID. Previously it would + use a string id. + ### Removed - Package no longer supports PHP 8.1. The minimum supported version is now PHP 8.2. diff --git a/src/Toolkit/Identifiers/Guid.php b/src/Toolkit/Identifiers/Guid.php index d684727..bdd872a 100644 --- a/src/Toolkit/Identifiers/Guid.php +++ b/src/Toolkit/Identifiers/Guid.php @@ -15,6 +15,7 @@ use CloudCreativity\Modules\Contracts\Toolkit\Identifiers\Identifier; use CloudCreativity\Modules\Toolkit\ContractException; use CloudCreativity\Modules\Toolkit\Contracts; +use Ramsey\Uuid\Uuid as RamseyUuid; use Ramsey\Uuid\UuidInterface; use UnitEnum; @@ -76,16 +77,17 @@ public static function fromUuid(UnitEnum|string $type, Uuid|UuidInterface|string * Create a GUID. * * @param UnitEnum|string $type - * @param string|int $id + * @param Uuid|UuidInterface|string|int $id * @return self */ - public static function make(UnitEnum|string $type, string|int $id): self + public static function make(UnitEnum|string $type, Uuid|UuidInterface|string|int $id): self { - if (is_int($id)) { - return self::fromInteger($type, $id); - } - - return self::fromString($type, $id); + return match (true) { + $id instanceof Uuid, $id instanceof UuidInterface, is_string($id) && RamseyUuid::isValid($id) + => self::fromUuid($type, $id), + is_string($id) => self::fromString($type, $id), + is_int($id) => self::fromInteger($type, $id), + }; } /** diff --git a/tests/Unit/Toolkit/Identifiers/GuidTest.php b/tests/Unit/Toolkit/Identifiers/GuidTest.php index 3d561ce..2ce113b 100644 --- a/tests/Unit/Toolkit/Identifiers/GuidTest.php +++ b/tests/Unit/Toolkit/Identifiers/GuidTest.php @@ -12,6 +12,7 @@ namespace CloudCreativity\Modules\Tests\Unit\Toolkit\Identifiers; +use CloudCreativity\Modules\Contracts\Toolkit\Identifiers\Identifier; use CloudCreativity\Modules\Tests\TestBackedEnum; use CloudCreativity\Modules\Tests\TestUnitEnum; use CloudCreativity\Modules\Toolkit\ContractException; @@ -22,6 +23,7 @@ use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; use Ramsey\Uuid\Uuid as BaseUuid; +use Ramsey\Uuid\UuidInterface; use UnitEnum; class GuidTest extends TestCase @@ -135,6 +137,29 @@ public function testUuid(UnitEnum|string $type, string $value, UnitEnum|string $ $this->assertObjectEquals($guid, Guid::fromUuid($type, $uuid->value)); } + /** + * @return array + */ + public static function makeProvider(): array + { + $uuid = Uuid::random(); + + return [ + 'integer' => [123, new IntegerId(123)], + 'string' => ['some-slug', new StringId('some-slug')], + 'ramsey uuid' => [$uuid->value, $uuid], + 'uuid object' => [$uuid, $uuid], + 'string uuid' => [$uuid->toString(), $uuid], + ]; + } + + #[DataProvider('makeProvider')] + public function testItMakesGuid(Uuid|UuidInterface|string|int $value, Identifier $expected): void + { + $guid = Guid::make('SomeType', $value); + $this->assertObjectEquals($expected, $guid->id); + } + /** * @return void */ From 33b681db30b4ab4e574acbf424d38d42c83e926c Mon Sep 17 00:00:00 2001 From: Christopher Gammie Date: Sat, 24 May 2025 14:33:05 +0100 Subject: [PATCH 2/3] feat!: update guid type map to support latest guid features --- CHANGELOG.md | 2 + src/Toolkit/Identifiers/GuidTypeMap.php | 39 ++++++++++++++----- .../Toolkit/Identifiers/GuidTypeMapTest.php | 27 +++++++++++-- 3 files changed, 56 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6e8e466..4a31b94 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,8 @@ All notable changes to this project will be documented in this file. This projec - **BREAKING**: The `Guid::make()` method will now convert a string that is a UUID to a UUID GUID. Previously it would use a string id. +- **BREAKING**: Updated the `GuidTypeMap` class so that it now supports enum aliases, enum types, and UUID identifiers. + This means the `type()` method now returns a string or enum, whereas previously it returned just a string. ### Removed diff --git a/src/Toolkit/Identifiers/GuidTypeMap.php b/src/Toolkit/Identifiers/GuidTypeMap.php index 4eaf8d2..cc6c904 100644 --- a/src/Toolkit/Identifiers/GuidTypeMap.php +++ b/src/Toolkit/Identifiers/GuidTypeMap.php @@ -12,25 +12,44 @@ namespace CloudCreativity\Modules\Toolkit\Identifiers; -final readonly class GuidTypeMap +use Ramsey\Uuid\UuidInterface; +use UnitEnum; + +use function CloudCreativity\Modules\Toolkit\enum_string; + +final class GuidTypeMap { /** * GuidTypeMap constructor * * @param array $map */ - public function __construct(private array $map) + public function __construct(private array $map = []) + { + } + + /** + * Define an alias to type mapping. + * + * @param UnitEnum|string $alias + * @param UnitEnum|string $type + * @return void + */ + public function define(UnitEnum|string $alias, UnitEnum|string $type): void { + $alias = enum_string($alias); + + $this->map[$alias] = $type; } /** * Get the GUID for the specified alias and id. * - * @param string $alias - * @param string|int $id + * @param UnitEnum|string $alias + * @param Uuid|UuidInterface|string|int $id * @return Guid */ - public function guidFor(string $alias, string|int $id): Guid + public function guidFor(UnitEnum|string $alias, Uuid|UuidInterface|string|int $id): Guid { return Guid::make($this->typeFor($alias), $id); } @@ -38,11 +57,13 @@ public function guidFor(string $alias, string|int $id): Guid /** * Get the GUID type for the specified alias. * - * @param string $alias - * @return string + * @param UnitEnum|string $alias + * @return UnitEnum|string */ - public function typeFor(string $alias): string + public function typeFor(UnitEnum|string $alias): UnitEnum|string { + $alias = enum_string($alias); + assert( isset($this->map[$alias]), sprintf('Alias "%s" is not defined in the type map.', $alias), @@ -51,7 +72,7 @@ public function typeFor(string $alias): string $type = $this->map[$alias] ?? null; assert( - is_string($type) && !empty($type), + (is_string($type) || $type instanceof UnitEnum) && !empty($type), sprintf('Expecting type for alias "%s" to be a non-empty string.', $alias), ); diff --git a/tests/Unit/Toolkit/Identifiers/GuidTypeMapTest.php b/tests/Unit/Toolkit/Identifiers/GuidTypeMapTest.php index 44411ae..2125322 100644 --- a/tests/Unit/Toolkit/Identifiers/GuidTypeMapTest.php +++ b/tests/Unit/Toolkit/Identifiers/GuidTypeMapTest.php @@ -13,16 +13,17 @@ namespace CloudCreativity\Modules\Tests\Unit\Toolkit\Identifiers; use AssertionError; +use CloudCreativity\Modules\Tests\TestBackedEnum; +use CloudCreativity\Modules\Tests\TestBackedIntEnum; +use CloudCreativity\Modules\Tests\TestUnitEnum; use CloudCreativity\Modules\Toolkit\Identifiers\Guid; use CloudCreativity\Modules\Toolkit\Identifiers\GuidTypeMap; +use CloudCreativity\Modules\Toolkit\Identifiers\Uuid; use PHPUnit\Framework\Attributes\Depends; use PHPUnit\Framework\TestCase; class GuidTypeMapTest extends TestCase { - /** - * @return GuidTypeMap - */ public function testItReturnsExpectedType(): GuidTypeMap { $map = new GuidTypeMap([ @@ -80,4 +81,24 @@ public function testItThrowsIfTypeIsNotDefined(GuidTypeMap $map): void $map->typeFor('NotDefined'); } + + public function testItReturnsExpectedTypeWithEnums(): void + { + $map = new GuidTypeMap(); + $map->define(TestBackedEnum::Foo, 'SomeFooType'); + $map->define(TestBackedEnum::Bar, TestBackedIntEnum::FooBar); + $map->define(TestUnitEnum::Baz, 'SomeBazType'); + $map->define(TestUnitEnum::Bat, TestBackedIntEnum::BazBat); + + $id = Uuid::random(); + + $this->assertSame('SomeFooType', $map->typeFor(TestBackedEnum::Foo)); + $this->assertObjectEquals(Guid::fromUuid('SomeFooType', $id), $map->guidFor(TestBackedEnum::Foo, $id)); + $this->assertSame(TestBackedIntEnum::FooBar, $map->typeFor(TestBackedEnum::Bar)); + $this->assertObjectEquals(Guid::fromUuid(TestBackedIntEnum::FooBar, $id), $map->guidFor(TestBackedEnum::Bar, $id)); + $this->assertSame('SomeBazType', $map->typeFor(TestUnitEnum::Baz)); + $this->assertObjectEquals(Guid::fromUuid('SomeBazType', $id), $map->guidFor(TestUnitEnum::Baz, $id)); + $this->assertSame(TestBackedIntEnum::BazBat, $map->typeFor(TestUnitEnum::Bat)); + $this->assertObjectEquals(Guid::fromUuid(TestBackedIntEnum::BazBat, $id), $map->guidFor(TestUnitEnum::Bat, $id)); + } } From 56e59f92597c16fe7d657559adec47ed85db6c3a Mon Sep 17 00:00:00 2001 From: Christopher Gammie Date: Sat, 24 May 2025 14:36:24 +0100 Subject: [PATCH 3/3] refactor!: remove guid type method --- CHANGELOG.md | 1 + src/Toolkit/Identifiers/Guid.php | 12 ------------ tests/Unit/Toolkit/Identifiers/GuidTest.php | 1 - 3 files changed, 1 insertion(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4a31b94..8d5338b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ All notable changes to this project will be documented in this file. This projec ### Removed - Package no longer supports PHP 8.1. The minimum supported version is now PHP 8.2. +- **BREAKING**: Remove the `Guid::type()` method - use `enum_value($guid->type)` or `enum_string($guid->type)` instead. ## Unreleased diff --git a/src/Toolkit/Identifiers/Guid.php b/src/Toolkit/Identifiers/Guid.php index bdd872a..de1cf34 100644 --- a/src/Toolkit/Identifiers/Guid.php +++ b/src/Toolkit/Identifiers/Guid.php @@ -20,7 +20,6 @@ use UnitEnum; use function CloudCreativity\Modules\Toolkit\enum_string; -use function CloudCreativity\Modules\Toolkit\enum_value; final readonly class Guid implements Identifier { @@ -202,15 +201,4 @@ enum_string($this->type), return $this; } - - /** - * Get the type expressed as a string or an integer. - * - * @return string|int - */ - public function type(): string|int - { - // TODO 4.0 use enum_string() instead - return enum_value($this->type); - } } diff --git a/tests/Unit/Toolkit/Identifiers/GuidTest.php b/tests/Unit/Toolkit/Identifiers/GuidTest.php index 2ce113b..ce70059 100644 --- a/tests/Unit/Toolkit/Identifiers/GuidTest.php +++ b/tests/Unit/Toolkit/Identifiers/GuidTest.php @@ -63,7 +63,6 @@ public function testStringId(UnitEnum|string $type, string $value, UnitEnum|stri $this->assertInstanceOf(\Stringable::class, $guid); $this->assertSame($type, $guid->type); - $this->assertSame($value, $guid->type()); $this->assertObjectEquals(new StringId('123'), $guid->id); $this->assertSame($value . ':123', $guid->toString()); $this->assertSame($value . ':123', (string) $guid);