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

Commitce220cd

Browse files
committed
feature#30915 [Serializer] Add datetimezone normalizer (jewome62)
This PR was squashed before being merged into the 4.3-dev branch (closes#30915).Discussion----------[Serializer] Add datetimezone normalizer| Q | A| ------------- | ---| Branch? | master| Bug fix? | no| New feature? | yes| BC breaks? | no| Deprecations? | no| Tests pass? | yes| Fixed tickets |#30145| License | MIT| Doc PR | not yetCommits-------1546c0d [Serializer] Add datetimezone normalizer
2 parentsa68b4c7 +1546c0d commitce220cd

File tree

3 files changed

+161
-0
lines changed

3 files changed

+161
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
-----
66

77
* added the list of constraint violations' parameters in`ConstraintViolationListNormalizer`
8+
* added support for serializing`DateTimeZone` objects
89

910
4.2.0
1011
-----
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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\NotNormalizableValueException;
16+
17+
/**
18+
* Normalizes a {@see \DateTimeZone} object to a timezone string.
19+
*
20+
* @author Jérôme Desjardins <jewome62@gmail.com>
21+
*/
22+
class DateTimeZoneNormalizerimplements NormalizerInterface, DenormalizerInterface, CacheableSupportsMethodInterface
23+
{
24+
/**
25+
* {@inheritdoc}
26+
*
27+
* @throws InvalidArgumentException
28+
*/
29+
publicfunctionnormalize($object,$format =null,array$context = [])
30+
{
31+
if (!$objectinstanceof \DateTimeZone) {
32+
thrownewInvalidArgumentException('The object must be an instance of "\DateTimeZone".');
33+
}
34+
35+
return$object->getName();
36+
}
37+
38+
/**
39+
* {@inheritdoc}
40+
*/
41+
publicfunctionsupportsNormalization($data,$format =null)
42+
{
43+
return$datainstanceof \DateTimeZone;
44+
}
45+
46+
/**
47+
* {@inheritdoc}
48+
*
49+
* @throws NotNormalizableValueException
50+
*/
51+
publicfunctiondenormalize($data,$class,$format =null,array$context = [])
52+
{
53+
if ('' ===$data ||null ===$data) {
54+
thrownewNotNormalizableValueException('The data is either an empty string or null, you should pass a string that can be parsed as a DateTimeZone.');
55+
}
56+
57+
try {
58+
returnnew \DateTimeZone($data);
59+
}catch (\Exception$e) {
60+
thrownewNotNormalizableValueException($e->getMessage(),$e->getCode(),$e);
61+
}
62+
}
63+
64+
/**
65+
* {@inheritdoc}
66+
*/
67+
publicfunctionsupportsDenormalization($data,$type,$format =null)
68+
{
69+
return \DateTimeZone::class ===$type;
70+
}
71+
72+
/**
73+
* {@inheritdoc}
74+
*/
75+
publicfunctionhasCacheableSupportsMethod():bool
76+
{
77+
return__CLASS__ ===\get_class($this);
78+
}
79+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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\Tests\Normalizer;
13+
14+
usePHPUnit\Framework\TestCase;
15+
useSymfony\Component\Serializer\Normalizer\DateTimeZoneNormalizer;
16+
17+
/**
18+
* @author Jérôme Desjardins <jewome62@gmail.com>
19+
*/
20+
class DateTimeZoneNormalizerTestextends TestCase
21+
{
22+
/**
23+
* @var DateTimeZoneNormalizer
24+
*/
25+
private$normalizer;
26+
27+
protectedfunctionsetUp()
28+
{
29+
$this->normalizer =newDateTimeZoneNormalizer();
30+
}
31+
32+
publicfunctiontestSupportsNormalization()
33+
{
34+
$this->assertTrue($this->normalizer->supportsNormalization(new \DateTimeZone('UTC')));
35+
$this->assertFalse($this->normalizer->supportsNormalization(new \DateTimeImmutable()));
36+
$this->assertFalse($this->normalizer->supportsNormalization(new \stdClass()));
37+
}
38+
39+
publicfunctiontestNormalize()
40+
{
41+
$this->assertEquals('UTC',$this->normalizer->normalize(new \DateTimeZone('UTC')));
42+
$this->assertEquals('Asia/Tokyo',$this->normalizer->normalize(new \DateTimeZone('Asia/Tokyo')));
43+
}
44+
45+
/**
46+
* @expectedException \Symfony\Component\Serializer\Exception\InvalidArgumentException
47+
*/
48+
publicfunctiontestNormalizeBadObjectTypeThrowsException()
49+
{
50+
$this->normalizer->normalize(new \stdClass());
51+
}
52+
53+
publicfunctiontestSupportsDenormalization()
54+
{
55+
$this->assertTrue($this->normalizer->supportsDenormalization(null, \DateTimeZone::class));
56+
$this->assertFalse($this->normalizer->supportsDenormalization(null, \DateTimeImmutable::class));
57+
$this->assertFalse($this->normalizer->supportsDenormalization(null, \stdClass::class));
58+
}
59+
60+
publicfunctiontestDenormalize()
61+
{
62+
$this->assertEquals(new \DateTimeZone('UTC'),$this->normalizer->denormalize('UTC', \DateTimeZone::class,null));
63+
$this->assertEquals(new \DateTimeZone('Asia/Tokyo'),$this->normalizer->denormalize('Asia/Tokyo', \DateTimeZone::class,null));
64+
}
65+
66+
/**
67+
* @expectedException \Symfony\Component\Serializer\Exception\NotNormalizableValueException
68+
*/
69+
publicfunctiontestDenormalizeNullTimeZoneThrowsException()
70+
{
71+
$this->normalizer->denormalize(null, \DateTimeZone::class,null);
72+
}
73+
74+
/**
75+
* @expectedException \Symfony\Component\Serializer\Exception\NotNormalizableValueException
76+
*/
77+
publicfunctiontestDenormalizeBadTimeZoneThrowsException()
78+
{
79+
$this->normalizer->denormalize('Jupiter/Europa', \DateTimeZone::class,null);
80+
}
81+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp