Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commit1c21c78

Browse files
Guillaume Pédelagrabefabpot
Guillaume Pédelagrabe
authored andcommitted
UidNormalizer.
1 parentbe6146c commit1c21c78

File tree

4 files changed

+177
-0
lines changed

4 files changed

+177
-0
lines changed

‎src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ CHANGELOG
2828
* Made`BrowserKitAssertionsTrait` report the original error message in case of a failure
2929
* Added ability for`config:dump-reference` and`debug:config` to dump and debug kernel container extension configuration.
3030
* Deprecated`session.attribute_bag` service and`session.flash_bag` service.
31+
* Added`UidNormalizer` to the framework serializer.
3132

3233
5.0.0
3334
-----

‎src/Symfony/Component/Serializer/CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ CHANGELOG
1313
* added support for`\stdClass` to`ObjectNormalizer`
1414
* added the ability to ignore properties using metadata (e.g.`@Symfony\Component\Serializer\Annotation\Ignore`)
1515
* added an option to serialize constraint violations payloads (e.g. severity)
16+
* added`UidNormalizer`
1617

1718
5.0.0
1819
-----
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespaceSymfony\Component\Serializer\Normalizer;
13+
14+
useSymfony\Component\Serializer\Exception\InvalidArgumentException;
15+
useSymfony\Component\Serializer\Exception\LogicException;
16+
useSymfony\Component\Serializer\Exception\NotNormalizableValueException;
17+
useSymfony\Component\Uid\AbstractUid;
18+
useSymfony\Component\Uid\Ulid;
19+
useSymfony\Component\Uid\Uuid;
20+
21+
finalclass UidNormalizerimplements NormalizerInterface, DenormalizerInterface, CacheableSupportsMethodInterface
22+
{
23+
/**
24+
* {@inheritdoc}
25+
*
26+
* @throws InvalidArgumentException
27+
*/
28+
publicfunctionnormalize($object,string$format =null,array$context = [])
29+
{
30+
if (!$objectinstanceof AbstractUid) {
31+
thrownewInvalidArgumentException('The object must be an instance of "\Symfony\Component\Uid\AbstractUid".');
32+
}
33+
34+
return (string)$object;
35+
}
36+
37+
/**
38+
* {@inheritdoc}
39+
*/
40+
publicfunctionsupportsNormalization($data,string$format =null)
41+
{
42+
return$datainstanceof AbstractUid;
43+
}
44+
45+
/**
46+
* {@inheritdoc}
47+
*
48+
* @throws NotNormalizableValueException
49+
*/
50+
publicfunctiondenormalize($data,string$type,string$format =null,array$context = [])
51+
{
52+
if (!class_exists(AbstractUid::class)) {
53+
thrownewLogicException('You cannot use the "Symfony\Component\Serializer\Normalizer\UidNormalizer" as the Symfony Uid Component is not installed. Try running "composer require symfony/uid".');
54+
}
55+
56+
try {
57+
$uid = Ulid::class ===$type ? Ulid::fromString($data) : Uuid::fromString($data);
58+
}catch (\InvalidArgumentException$exception) {
59+
thrownewNotNormalizableValueException('The data is not a valid'.$type.' string representation.');
60+
}
61+
62+
return$uid;
63+
}
64+
65+
/**
66+
* {@inheritdoc}
67+
*/
68+
publicfunctionsupportsDenormalization($data,string$type,string$format =null)
69+
{
70+
returnis_a($type, AbstractUid::class,true);
71+
}
72+
73+
/**
74+
* {@inheritdoc}
75+
*/
76+
publicfunctionhasCacheableSupportsMethod():bool
77+
{
78+
return__CLASS__ ===static::class;
79+
}
80+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
namespaceSymfony\Component\Serializer\Tests\Normalizer;
4+
5+
usePHPUnit\Framework\TestCase;
6+
useSymfony\Component\Serializer\Exception\InvalidArgumentException;
7+
useSymfony\Component\Serializer\Normalizer\UidNormalizer;
8+
useSymfony\Component\Uid\AbstractUid;
9+
useSymfony\Component\Uid\Ulid;
10+
useSymfony\Component\Uid\Uuid;
11+
useSymfony\Component\Uid\UuidV1;
12+
useSymfony\Component\Uid\UuidV3;
13+
useSymfony\Component\Uid\UuidV4;
14+
useSymfony\Component\Uid\UuidV5;
15+
useSymfony\Component\Uid\UuidV6;
16+
17+
class UidNormalizerTestextends TestCase
18+
{
19+
/**
20+
* @var UidNormalizer
21+
*/
22+
private$normalizer;
23+
24+
protectedfunctionsetUp():void
25+
{
26+
$this->normalizer =newUidNormalizer();
27+
}
28+
29+
publicfunctiondataProvider()
30+
{
31+
return [
32+
['9b7541de-6f87-11ea-ab3c-9da9a81562fc', UuidV1::class],
33+
['e576629b-ff34-3642-9c08-1f5219f0d45b', UuidV3::class],
34+
['4126dbc1-488e-4f6e-aadd-775dcbac482e', UuidV4::class],
35+
['18cdf3d3-ea1b-5b23-a9c5-40abd0e2df22', UuidV5::class],
36+
['1ea6ecef-eb9a-66fe-b62b-957b45f17e43', UuidV6::class],
37+
['1ea6ecef-eb9a-66fe-b62b-957b45f17e43', AbstractUid::class],
38+
['01E4BYF64YZ97MDV6RH0HAMN6X', Ulid::class],
39+
];
40+
}
41+
42+
publicfunctiontestSupportsNormalization()
43+
{
44+
$this->assertTrue($this->normalizer->supportsNormalization(Uuid::v1()));
45+
$this->assertTrue($this->normalizer->supportsNormalization(Uuid::v3(Uuid::v1(),'foo')));
46+
$this->assertTrue($this->normalizer->supportsNormalization(Uuid::v4()));
47+
$this->assertTrue($this->normalizer->supportsNormalization(Uuid::v5(Uuid::v1(),'foo')));
48+
$this->assertTrue($this->normalizer->supportsNormalization(Uuid::v6()));
49+
$this->assertTrue($this->normalizer->supportsNormalization(newUlid()));
50+
$this->assertFalse($this->normalizer->supportsNormalization(new \stdClass()));
51+
}
52+
53+
/**
54+
* @dataProvider dataProvider
55+
*/
56+
publicfunctiontestNormalize($uuidString,$class)
57+
{
58+
if (Ulid::class ===$class) {
59+
$this->assertEquals($uuidString,$this->normalizer->normalize(Ulid::fromString($uuidString)));
60+
}else {
61+
$this->assertEquals($uuidString,$this->normalizer->normalize(Uuid::fromString($uuidString)));
62+
}
63+
}
64+
65+
publicfunctiontestNormalizeForNonUid()
66+
{
67+
$this->expectException(InvalidArgumentException::class);
68+
$this->normalizer->normalize(new \stdClass());
69+
}
70+
71+
/**
72+
* @dataProvider dataProvider
73+
*/
74+
publicfunctiontestSupportsDenormalization($uuidString,$class)
75+
{
76+
$this->assertTrue($this->normalizer->supportsDenormalization($uuidString,$class));
77+
}
78+
79+
publicfunctiontestSupportsDenormalizationForNonUid()
80+
{
81+
$this->assertFalse($this->normalizer->supportsDenormalization('foo', \stdClass::class));
82+
}
83+
84+
/**
85+
* @dataProvider dataProvider
86+
*/
87+
publicfunctiontestDenormalize($uuidString,$class)
88+
{
89+
if (Ulid::class ===$class) {
90+
$this->assertEquals(newUlid($uuidString),$this->normalizer->denormalize($uuidString,$class));
91+
}else {
92+
$this->assertEquals(Uuid::fromString($uuidString),$this->normalizer->denormalize($uuidString,$class));
93+
}
94+
}
95+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp