Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Serializer] add UidNormalizer #36406

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions 1 src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ CHANGELOG
* Made `BrowserKitAssertionsTrait` report the original error message in case of a failure
* Added ability for `config:dump-reference` and `debug:config` to dump and debug kernel container extension configuration.
* Deprecated `session.attribute_bag` service and `session.flash_bag` service.
* Added `UidNormalizer` to the framework serializer.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be removed


5.0.0
-----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@
<tag name="serializer.normalizer" priority="1000" />
</service>

<service id="serializer.normalizer.uid" class="Symfony\Component\Serializer\Normalizer\UidNormalizer">
<!-- Run before serializer.normalizer.object -->
<tag name="serializer.normalizer" priority="-880" />
</service>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be moved to the new PHP config file.
For the priority, I would use -915 to be consistent with the other very specialized normalizers.


<service id="serializer.normalizer.object" class="Symfony\Component\Serializer\Normalizer\ObjectNormalizer">
<argument type="service" id="serializer.mapping.class_metadata_factory" />
<argument type="service" id="serializer.name_converter.metadata_aware" />
Expand Down
1 change: 1 addition & 0 deletions 1 src/Symfony/Component/Serializer/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ CHANGELOG
* added support for `\stdClass` to `ObjectNormalizer`
* added the ability to ignore properties using metadata (e.g. `@Symfony\Component\Serializer\Annotation\Ignore`)
* added an option to serialize constraint violations payloads (e.g. severity)
* added `UidNormalizer`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be moved to the 5.2.0 section.


5.0.0
-----
Expand Down
80 changes: 80 additions & 0 deletions 80 src/Symfony/Component/Serializer/Normalizer/UidNormalizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Serializer\Normalizer;

use Symfony\Component\Serializer\Exception\InvalidArgumentException;
use Symfony\Component\Serializer\Exception\LogicException;
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
use Symfony\Component\Uid\AbstractUid;
use Symfony\Component\Uid\Ulid;
use Symfony\Component\Uid\Uuid;

final class UidNormalizer implements NormalizerInterface, DenormalizerInterface, CacheableSupportsMethodInterface
{
/**
* {@inheritdoc}
*
* @throws InvalidArgumentException
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be removed

*/
public function normalize($object, string $format = null, array $context = [])
{
if (!$object instanceof AbstractUid) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The condition can be removed as the supports method garuantees that already.

throw new InvalidArgumentException('The object must be an instance of "\Symfony\Component\Uid\AbstractUid".');
}

return (string) $object;
}

/**
* {@inheritdoc}
*/
public function supportsNormalization($data, string $format = null)
{
return $data instanceof AbstractUid;
}

/**
* {@inheritdoc}
*
* @throws NotNormalizableValueException
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be removed

*/
public function denormalize($data, string $type, string $format = null, array $context = [])
{
if (!class_exists(AbstractUid::class)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be removed

throw new LogicException('You cannot use the "Symfony\Component\Serializer\Normalizer\UidNormalizer" as the Symfony Uid Component is not installed. Try running "composer require symfony/uid".');
}

try {
$uid = Ulid::class === $type ? Ulid::fromString($data) : Uuid::fromString($data);
guillbdx marked this conversation as resolved.
Show resolved Hide resolved
} catch (\InvalidArgumentException $exception) {
throw new NotNormalizableValueException('The data is not a valid '.$type.' string representation.');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should use sprintf here.

}

return $uid;
}

/**
* {@inheritdoc}
*/
public function supportsDenormalization($data, string $type, string $format = null)
{
return is_a($type, AbstractUid::class, true);
}

/**
* {@inheritdoc}
*/
public function hasCacheableSupportsMethod(): bool
{
return __CLASS__ === static::class;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

namespace Symfony\Component\Serializer\Tests\Normalizer;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
use Symfony\Component\Serializer\Normalizer\UidNormalizer;
use Symfony\Component\Uid\AbstractUid;
use Symfony\Component\Uid\Ulid;
use Symfony\Component\Uid\Uuid;
use Symfony\Component\Uid\UuidV1;
use Symfony\Component\Uid\UuidV3;
use Symfony\Component\Uid\UuidV4;
use Symfony\Component\Uid\UuidV5;
use Symfony\Component\Uid\UuidV6;

class UidNormalizerTest extends TestCase
{
/**
* @var UidNormalizer
*/
private $normalizer;

protected function setUp(): void
{
$this->normalizer = new UidNormalizer();
}

public function dataProvider()
{
return [
['9b7541de-6f87-11ea-ab3c-9da9a81562fc', UuidV1::class],
['e576629b-ff34-3642-9c08-1f5219f0d45b', UuidV3::class],
['4126dbc1-488e-4f6e-aadd-775dcbac482e', UuidV4::class],
['18cdf3d3-ea1b-5b23-a9c5-40abd0e2df22', UuidV5::class],
['1ea6ecef-eb9a-66fe-b62b-957b45f17e43', UuidV6::class],
['1ea6ecef-eb9a-66fe-b62b-957b45f17e43', AbstractUid::class],
['01E4BYF64YZ97MDV6RH0HAMN6X', Ulid::class],
];
}

public function testSupportsNormalization()
{
$this->assertTrue($this->normalizer->supportsNormalization(Uuid::v1()));
$this->assertTrue($this->normalizer->supportsNormalization(Uuid::v3(Uuid::v1(), 'foo')));
$this->assertTrue($this->normalizer->supportsNormalization(Uuid::v4()));
$this->assertTrue($this->normalizer->supportsNormalization(Uuid::v5(Uuid::v1(), 'foo')));
$this->assertTrue($this->normalizer->supportsNormalization(Uuid::v6()));
$this->assertTrue($this->normalizer->supportsNormalization(new Ulid()));
$this->assertFalse($this->normalizer->supportsNormalization(new \stdClass()));
}

/**
* @dataProvider dataProvider
*/
public function testNormalize($uuidString, $class)
{
if (Ulid::class === $class) {
$this->assertEquals($uuidString, $this->normalizer->normalize(Ulid::fromString($uuidString)));
} else {
$this->assertEquals($uuidString, $this->normalizer->normalize(Uuid::fromString($uuidString)));
}
}

public function testNormalizeForNonUid()
{
$this->expectException(InvalidArgumentException::class);
$this->normalizer->normalize(new \stdClass());
}

/**
* @dataProvider dataProvider
*/
public function testSupportsDenormalization($uuidString, $class)
{
$this->assertTrue($this->normalizer->supportsDenormalization($uuidString, $class));
}

public function testSupportsDenormalizationForNonUid()
{
$this->assertFalse($this->normalizer->supportsDenormalization('foo', \stdClass::class));
}

/**
* @dataProvider dataProvider
*/
public function testDenormalize($uuidString, $class)
{
if (Ulid::class === $class) {
$this->assertEquals(new Ulid($uuidString), $this->normalizer->denormalize($uuidString, $class));
} else {
$this->assertEquals(Uuid::fromString($uuidString), $this->normalizer->denormalize($uuidString, $class));
}
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.