-
-
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.
- Loading branch information
Showing
10 changed files
with
141 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
# CHANGELOG | ||
|
||
## 1.4.0 | ||
|
||
* feat: `ramsey/uuid` support | ||
|
||
## 1.3.0 | ||
|
||
|
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
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
104 changes: 104 additions & 0 deletions
104
src/Transformer/Implementation/RamseyUuidTransformer.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,104 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of rekalogika/mapper 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\Mapper\Transformer\Implementation; | ||
|
||
use Ramsey\Uuid\Uuid; | ||
use Ramsey\Uuid\UuidInterface; | ||
use Rekalogika\Mapper\Context\Context; | ||
use Rekalogika\Mapper\Exception\InvalidArgumentException; | ||
use Rekalogika\Mapper\Transformer\TransformerInterface; | ||
use Rekalogika\Mapper\Transformer\TypeMapping; | ||
use Rekalogika\Mapper\Util\TypeFactory; | ||
use Rekalogika\Mapper\Util\TypeUtil; | ||
use Symfony\Component\PropertyInfo\Type; | ||
|
||
final readonly class RamseyUuidTransformer implements TransformerInterface | ||
{ | ||
public function transform( | ||
mixed $source, | ||
mixed $target, | ||
?Type $sourceType, | ||
?Type $targetType, | ||
Context $context | ||
): mixed { | ||
if ($targetType === null) { | ||
throw new InvalidArgumentException( | ||
sprintf( | ||
'Target type is null when trying to transform type "%s" to "%s", using source "%s".', | ||
TypeUtil::getDebugType($sourceType), | ||
TypeUtil::getDebugType($targetType), | ||
\get_debug_type($source) | ||
) | ||
); | ||
} | ||
|
||
// wants to convert string to uuid | ||
|
||
if (is_string($source)) { | ||
$targetClass = $targetType->getClassName(); | ||
|
||
if ($targetClass === null) { | ||
throw new InvalidArgumentException( | ||
sprintf( | ||
'Target class is null when trying to transform type "%s" to "%s", using source "%s".', | ||
TypeUtil::getDebugType($sourceType), | ||
TypeUtil::getDebugType($targetType), | ||
\get_debug_type($source) | ||
) | ||
); | ||
} | ||
|
||
return Uuid::fromString($source); | ||
} | ||
|
||
// wants to convert uuid to string | ||
|
||
if ($source instanceof UuidInterface) { | ||
if ($targetType->getBuiltinType() === Type::BUILTIN_TYPE_STRING) { | ||
return $source->toString(); | ||
} elseif ($targetType->getClassName() === UuidInterface::class) { | ||
return $source; | ||
} | ||
|
||
return $source->toString(); | ||
} | ||
|
||
throw new InvalidArgumentException( | ||
sprintf( | ||
'Trying to transform type "%s" to "%s", using source "%s".', | ||
TypeUtil::getDebugType($sourceType), | ||
TypeUtil::getDebugType($targetType), | ||
\get_debug_type($source) | ||
) | ||
); | ||
} | ||
|
||
public function getSupportedTransformation(): iterable | ||
{ | ||
yield new TypeMapping( | ||
TypeFactory::objectOfClass(UuidInterface::class), | ||
TypeFactory::string(), | ||
); | ||
|
||
yield new TypeMapping( | ||
TypeFactory::string(), | ||
TypeFactory::objectOfClass(UuidInterface::class), | ||
); | ||
|
||
yield new TypeMapping( | ||
TypeFactory::objectOfClass(UuidInterface::class), | ||
TypeFactory::objectOfClass(UuidInterface::class), | ||
); | ||
} | ||
} |
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
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
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