generated from spiral-packages/package-skeleton
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from spiral-packages/feature/uuid-normalizer
Adding Ramsey UUID normalizer
- Loading branch information
Showing
8 changed files
with
178 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\Serializer\Symfony\Normalizer; | ||
|
||
use Ramsey\Uuid\Uuid; | ||
use Ramsey\Uuid\UuidInterface; | ||
use Symfony\Component\Serializer\Exception\NotNormalizableValueException; | ||
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; | ||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; | ||
|
||
final class RamseyUuidNormalizer implements NormalizerInterface, DenormalizerInterface | ||
{ | ||
/** | ||
* @param UuidInterface $object | ||
* @psalm-suppress MoreSpecificImplementedParamType | ||
*/ | ||
public function normalize(mixed $object, string $format = null, array $context = []): string | ||
{ | ||
return $object->toString(); | ||
} | ||
|
||
public function supportsNormalization(mixed $data, string $format = null): bool | ||
{ | ||
return $data instanceof UuidInterface; | ||
} | ||
|
||
public function denormalize(mixed $data, string $type, string $format = null, array $context = []): UuidInterface | ||
{ | ||
try { | ||
return Uuid::fromString($data); | ||
} catch (\InvalidArgumentException) { | ||
throw new NotNormalizableValueException( | ||
\sprintf('The data is not a valid "%s" string representation.', $type) | ||
); | ||
} | ||
} | ||
|
||
public function supportsDenormalization(mixed $data, string $type, string $format = null): bool | ||
{ | ||
return \is_string($data) && \is_a($type, UuidInterface::class, true) && Uuid::isValid($data); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\Serializer\Symfony\Tests\App\Object; | ||
|
||
use Ramsey\Uuid\UuidInterface; | ||
|
||
class Author | ||
{ | ||
public function __construct( | ||
public UuidInterface $uuid, | ||
public string $name | ||
) { | ||
} | ||
} |
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,14 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\Serializer\Symfony\Tests\App\Object; | ||
|
||
class User | ||
{ | ||
public function __construct( | ||
public int $id, | ||
public \DateTimeInterface $registeredAt | ||
) { | ||
} | ||
} |
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,55 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\Serializer\Symfony\Tests\Feature\Normalizer; | ||
|
||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use Ramsey\Uuid\Uuid; | ||
use Spiral\Serializer\SerializerManager; | ||
use Spiral\Serializer\Symfony\Tests\App\Object\Author; | ||
use Spiral\Serializer\Symfony\Tests\Feature\TestCase; | ||
|
||
final class RamseyUuidNormalizerTest extends TestCase | ||
{ | ||
#[DataProvider('serializeDataProvider')] | ||
public function testSerialize(string $expected, mixed $payload, string $format): void | ||
{ | ||
$manager = $this->getContainer()->get(SerializerManager::class); | ||
|
||
$this->assertSame($expected, preg_replace('/\s+/', '', $manager->serialize($payload, $format))); | ||
} | ||
|
||
public function testUnserialize(): void | ||
{ | ||
$manager = $this->getContainer()->get(SerializerManager::class); | ||
|
||
$result = $manager->unserialize( | ||
'{"uuid":"1d96a152-9838-43a0-a189-159befc9e38f","name":"some"}', | ||
Author::class, | ||
'symfony-json' | ||
); | ||
|
||
$this->assertInstanceOf(Author::class, $result); | ||
$this->assertSame('1d96a152-9838-43a0-a189-159befc9e38f', $result->uuid->toString()); | ||
} | ||
|
||
public static function serializeDataProvider(): \Traversable | ||
{ | ||
yield [ | ||
'{"uuid":"1d96a152-9838-43a0-a189-159befc9e38f","name":"some"}', | ||
new Author(Uuid::fromString('1d96a152-9838-43a0-a189-159befc9e38f'), 'some'), | ||
'symfony-json' | ||
]; | ||
yield [ | ||
'uuid,name1d96a152-9838-43a0-a189-159befc9e38f,some', | ||
new Author(Uuid::fromString('1d96a152-9838-43a0-a189-159befc9e38f'), 'some'), | ||
'symfony-csv' | ||
]; | ||
yield [ | ||
'{uuid:1d96a152-9838-43a0-a189-159befc9e38f,name:some}', | ||
new Author(Uuid::fromString('1d96a152-9838-43a0-a189-159befc9e38f'), 'some'), | ||
'symfony-yaml' | ||
]; | ||
} | ||
} |
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