Skip to content

Commit

Permalink
feat: ramsey/uuid support (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
priyadi committed May 3, 2024
1 parent 7380e30 commit d90b473
Show file tree
Hide file tree
Showing 10 changed files with 141 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
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

Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"phpstan/phpstan-phpunit": "^1.3",
"phpunit/phpunit": "^10.5",
"psalm/plugin-phpunit": "^0.19.0",
"ramsey/uuid": "^3.0",
"symfony/framework-bundle": "^6.4 || ^7.0",
"symfony/http-kernel": "^6.4 || ^7.0",
"symfony/runtime": "^6.4 || ^7.0",
Expand Down
5 changes: 5 additions & 0 deletions config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
use Rekalogika\Mapper\Transformer\Implementation\ObjectToObjectTransformer;
use Rekalogika\Mapper\Transformer\Implementation\ObjectToStringTransformer;
use Rekalogika\Mapper\Transformer\Implementation\PresetTransformer;
use Rekalogika\Mapper\Transformer\Implementation\RamseyUuidTransformer;
use Rekalogika\Mapper\Transformer\Implementation\ScalarToScalarTransformer;
use Rekalogika\Mapper\Transformer\Implementation\StringToBackedEnumTransformer;
use Rekalogika\Mapper\Transformer\Implementation\SymfonyUidTransformer;
Expand Down Expand Up @@ -130,6 +131,10 @@
->set(SymfonyUidTransformer::class)
->tag('rekalogika.mapper.transformer', ['priority' => -550]);

$services
->set(RamseyUuidTransformer::class)
->tag('rekalogika.mapper.transformer', ['priority' => -550]);

$services
->set(ObjectToStringTransformer::class)
->tag('rekalogika.mapper.transformer', ['priority' => -600]);
Expand Down
17 changes: 17 additions & 0 deletions src/MapperFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use Psr\Cache\CacheItemPoolInterface;
use Psr\Container\ContainerInterface;
use Ramsey\Uuid\UuidInterface;
use Rekalogika\Mapper\Command\MappingCommand;
use Rekalogika\Mapper\Command\TryCommand;
use Rekalogika\Mapper\Command\TryPropertyCommand;
Expand Down Expand Up @@ -57,6 +58,7 @@
use Rekalogika\Mapper\Transformer\Implementation\ObjectToObjectTransformer;
use Rekalogika\Mapper\Transformer\Implementation\ObjectToStringTransformer;
use Rekalogika\Mapper\Transformer\Implementation\PresetTransformer;
use Rekalogika\Mapper\Transformer\Implementation\RamseyUuidTransformer;
use Rekalogika\Mapper\Transformer\Implementation\ScalarToScalarTransformer;
use Rekalogika\Mapper\Transformer\Implementation\StringToBackedEnumTransformer;
use Rekalogika\Mapper\Transformer\Implementation\SymfonyUidTransformer;
Expand Down Expand Up @@ -130,6 +132,7 @@ class MapperFactory
private ?CopyTransformer $copyTransformer = null;
private ?ClassMethodTransformer $classMethodTransformer = null;
private ?SymfonyUidTransformer $symfonyUidTransformer = null;
private ?RamseyUuidTransformer $ramseyUuidTransformer = null;
private ?PresetTransformer $presetTransformer = null;

private CacheItemPoolInterface $propertyInfoExtractorCache;
Expand Down Expand Up @@ -518,6 +521,15 @@ protected function getSymfonyUidTransformer(): SymfonyUidTransformer
return $this->symfonyUidTransformer;
}

protected function getRamseyUuidTransformer(): RamseyUuidTransformer
{
if (null === $this->ramseyUuidTransformer) {
$this->ramseyUuidTransformer = new RamseyUuidTransformer();
}

return $this->ramseyUuidTransformer;
}

protected function getPresetTransformer(): PresetTransformer
{
if (null === $this->presetTransformer) {
Expand Down Expand Up @@ -597,6 +609,11 @@ protected function getTransformersIterator(): iterable
=> $this->getSymfonyUidTransformer();
}

if (interface_exists(UuidInterface::class)) {
yield 'RamseyUuidTransformer'
=> $this->getRamseyUuidTransformer();
}

yield 'ObjectToStringTransformer'
=> $this->getObjectToStringTransformer();

Expand Down
104 changes: 104 additions & 0 deletions src/Transformer/Implementation/RamseyUuidTransformer.php
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),
);
}
}
1 change: 1 addition & 0 deletions tests/Fixtures/Uid/ObjectWithStringUids.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ class ObjectWithStringUids
{
public string $uuid = '6ba7b810-9dad-11d1-80b4-00c04fd430c8';
public string $ulid = '01F9Z3ZQZJQJZJZJZJZJZJZJZJ';
public string $ramseyUuid = '6ba7b810-9dad-11d1-80b4-00c04fd430c8';
}
4 changes: 4 additions & 0 deletions tests/Fixtures/Uid/ObjectWithUids.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,21 @@

namespace Rekalogika\Mapper\Tests\Fixtures\Uid;

use Ramsey\Uuid\Uuid as RamseyUuid;
use Ramsey\Uuid\UuidInterface;
use Symfony\Component\Uid\Ulid;
use Symfony\Component\Uid\Uuid;

class ObjectWithUids
{
public Uuid $uuid;
public Ulid $ulid;
public UuidInterface $ramseyUuid;

public function __construct()
{
$this->uuid = Uuid::fromString('c4e0d7e0-7f1a-4b1e-8e3c-2b4b1b9a0b5a');
$this->ulid = Ulid::fromString('01F9Z3ZJZ1QJXZJXZJXZJXZJXZ');
$this->ramseyUuid = RamseyUuid::fromString('c4e0d7e0-7f1a-4b1e-8e3c-2b4b1b9a0b5a');
}
}
1 change: 1 addition & 0 deletions tests/Fixtures/UidDto/ObjectWithStringUidsDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ class ObjectWithStringUidsDto
{
public ?string $uuid = null;
public ?string $ulid = null;
public ?string $ramseyUuid = null;
}
2 changes: 2 additions & 0 deletions tests/Fixtures/UidDto/ObjectWithUidsDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@

namespace Rekalogika\Mapper\Tests\Fixtures\UidDto;

use Ramsey\Uuid\UuidInterface;
use Symfony\Component\Uid\Ulid;
use Symfony\Component\Uid\Uuid;

class ObjectWithUidsDto
{
public ?Uuid $uuid = null;
public ?Ulid $ulid = null;
public ?UuidInterface $ramseyUuid = null;
}
4 changes: 3 additions & 1 deletion tests/IntegrationTest/UidTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public function testUuidToString(): void

$this->assertEquals('c4e0d7e0-7f1a-4b1e-8e3c-2b4b1b9a0b5a', $dto->uuid);
$this->assertEquals('01F9Z3ZJZ1QJXZJXZJXZJXZJXZ', $dto->ulid);
$this->assertEquals('c4e0d7e0-7f1a-4b1e-8e3c-2b4b1b9a0b5a', $dto->ramseyUuid);
}

public function testStringToUuid(): void
Expand All @@ -40,7 +41,7 @@ public function testStringToUuid(): void
$this->assertInstanceOf(ObjectWithUidsDto::class, $dto);
$this->assertEquals(Uuid::fromString('6ba7b810-9dad-11d1-80b4-00c04fd430c8'), $dto->uuid);
$this->assertEquals(Ulid::fromString('01F9Z3ZQZJQJZJZJZJZJZJZJZJ'), $dto->ulid);

$this->assertEquals('6ba7b810-9dad-11d1-80b4-00c04fd430c8', $dto->ramseyUuid?->toString());
}

public function testUuidToUuid(): void
Expand All @@ -51,6 +52,7 @@ public function testUuidToUuid(): void
$this->assertInstanceOf(ObjectWithUidsDto::class, $dto);
$this->assertEquals(Uuid::fromString('c4e0d7e0-7f1a-4b1e-8e3c-2b4b1b9a0b5a'), $dto->uuid);
$this->assertEquals(Ulid::fromString('01F9Z3ZJZ1QJXZJXZJXZJXZJXZ'), $dto->ulid);
$this->assertEquals('c4e0d7e0-7f1a-4b1e-8e3c-2b4b1b9a0b5a', $dto->ramseyUuid?->toString());
}

}

0 comments on commit d90b473

Please sign in to comment.