Skip to content

Commit

Permalink
fix(DefaultKeyTransformer): only convert Stringable to string for…
Browse files Browse the repository at this point in the history
… now. (#102)

* fix(`DefaultKeyTransformer`): only convert `Stringable` to string for now.

* feat: add `UuidKeyTransformer`
  • Loading branch information
priyadi authored Jul 6, 2024
1 parent 36a21c3 commit 5bf0992
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

/*
* This file is part of rekalogika/collections package.
*
* (c) Priyadi Iman Nurcahyo <https://rekalogika.dev>
*
* 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();
}
}
}

0 comments on commit 5bf0992

Please sign in to comment.