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

Commit7cd0682

Browse files
committed
Add datetimezone normalizer#30145
1 parent09dee17 commit7cd0682

File tree

2 files changed

+161
-0
lines changed

2 files changed

+161
-0
lines changed
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\NotNormalizableValueException;
16+
17+
/**
18+
* Normalizes an object {@see \DateTimeZone} to a timezone string.
19+
*
20+
* @author Jérôme Desjardins <jewome62@gmail.com>
21+
*/
22+
class DateTimeZoneNormalizerimplements NormalizerInterface, DenormalizerInterface, CacheableSupportsMethodInterface
23+
{
24+
25+
/**
26+
* {@inheritdoc}
27+
*
28+
* @throws InvalidArgumentException
29+
*/
30+
publicfunctionnormalize($object,$format =null,array$context = [])
31+
{
32+
if (!$objectinstanceof \DateTimeZone) {
33+
thrownewInvalidArgumentException('The object must implement the "\DateTimeZone".');
34+
}
35+
36+
return$object->getName();
37+
}
38+
39+
/**
40+
* {@inheritdoc}
41+
*/
42+
publicfunctionsupportsNormalization($data,$format =null)
43+
{
44+
return$datainstanceof \DateTimeZone;
45+
}
46+
47+
/**
48+
* {@inheritdoc}
49+
*
50+
* @throws NotNormalizableValueException
51+
*/
52+
publicfunctiondenormalize($data,$class,$format =null,array$context = [])
53+
{
54+
if ('' ===$data ||null ===$data) {
55+
thrownewNotNormalizableValueException('The data is either an empty string or null, you should pass a string that can be parsed with the passed format or a valid DateTimeZone string.');
56+
}
57+
58+
try {
59+
returnnew \DateTimeZone($data);
60+
}catch (\Exception$e) {
61+
thrownewNotNormalizableValueException($e->getMessage(),$e->getCode(),$e);
62+
}
63+
}
64+
65+
/**
66+
* {@inheritdoc}
67+
*/
68+
publicfunctionsupportsDenormalization($data,$type,$format =null)
69+
{
70+
return \DateTimeZone::class ===$type;
71+
}
72+
73+
/**
74+
* {@inheritdoc}
75+
*/
76+
publicfunctionhasCacheableSupportsMethod():bool
77+
{
78+
return__CLASS__ ===\get_class($this);
79+
}
80+
}
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