diff --git a/CHANGELOG.md b/CHANGELOG.md index d971471..2e0df4f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,8 @@ * refactor: remove `Countable` from minimal classes * refactor: rename `RestrictedCountStrategy` to `DisabledCountStrategy` * feat: `$offset` parameter type widening for `ArrayAccess` methods +* fix(`DefaultKeyTransformer`): only convert `Stringable` to string for now. +* feat: add `UuidKeyTransformer` ## 0.7.0 diff --git a/packages/collections-common/src/KeyTransformer/DefaultKeyTransformer.php b/packages/collections-common/src/KeyTransformer/DefaultKeyTransformer.php index d103531..f05a16a 100644 --- a/packages/collections-common/src/KeyTransformer/DefaultKeyTransformer.php +++ b/packages/collections-common/src/KeyTransformer/DefaultKeyTransformer.php @@ -14,14 +14,13 @@ namespace Rekalogika\Domain\Collections\Common\KeyTransformer; use Rekalogika\Contracts\Collections\Exception\NotFoundException; -use Symfony\Component\Uid\AbstractUid; class DefaultKeyTransformer implements KeyTransformer { public static function transformToKey(mixed $key): int|string { - if ($key instanceof AbstractUid) { - return $key->toBinary(); + if ($key instanceof \Stringable) { + return (string) $key; } elseif (!\is_string($key) && !\is_int($key)) { throw new NotFoundException(); } diff --git a/packages/collections-common/src/KeyTransformer/UuidKeyTransformer.php b/packages/collections-common/src/KeyTransformer/UuidKeyTransformer.php new file mode 100644 index 0000000..a8fa69b --- /dev/null +++ b/packages/collections-common/src/KeyTransformer/UuidKeyTransformer.php @@ -0,0 +1,40 @@ + + * + * For the full copyright and license information, please view the LICENSE file + * that was distributed with this source code. + */ + +namespace Rekalogika\Domain\Collections\Common\KeyTransformer; + +use Rekalogika\Contracts\Collections\Exception\NotFoundException; +use Symfony\Component\Uid\AbstractUid; +use Symfony\Component\Uid\Uuid; + +class UuidKeyTransformer implements KeyTransformer +{ + public static function transformToKey(mixed $key): int|string + { + if ($key instanceof AbstractUid) { + return $key->toRfc4122(); + } + + if (!\is_string($key)) { + throw new NotFoundException(); + } + + try { + $uuid = new Uuid($key); + + return $key; + } catch (\InvalidArgumentException $e) { + throw new NotFoundException(); + } + } +}