-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(
DefaultKeyTransformer
): only convert Stringable
to string for…
… now. (#102) * fix(`DefaultKeyTransformer`): only convert `Stringable` to string for now. * feat: add `UuidKeyTransformer`
- Loading branch information
Showing
3 changed files
with
44 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
packages/collections-common/src/KeyTransformer/UuidKeyTransformer.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |