8000 Add datetimezone normalizer #30145 · symfony/symfony@7cd0682 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7cd0682

Browse files
committed
Add datetimezone normalizer #30145
1 parent 09dee17 commit 7cd0682

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+
namespace Symfony\Component\Serializer\Normalizer;
13+
14+
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
15+
use Symfony\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 DateTimeZoneNormalizer implements NormalizerInterface, DenormalizerInterface, CacheableSupportsMethodInterface
23+
{
24+
25+
/**
26+
* {@inheritdoc}
27+
*
28+
* @throws InvalidArgumentException
29+
*/
30+
public function normalize($object, $format = null, array $context = [])
31+
{
32+
if (!$object instanceof \DateTimeZone) {
33+
throw new InvalidArgumentException('The object must implement the "\DateTimeZone".');
34+
}
35+
36+
return $object->getName();
37+
}
38+
39+
/**
40+
* {@inheritdoc}
41+
*/
42+
public function supportsNormalization($data, $format = null)
43+
{
44+
return $data instanceof \DateTimeZone;
45+
}
46+
47+
/**
48+
* {@inheritdoc}
49+
*
50+
* @throws NotNormalizableValueException
51+
*/
52+
public function denormalize($data, $class, $format = null, array $context = [])
53+
{
54+
if ('' === $data || null === $data) {
55+
throw new NotNormalizableValueException('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+
return new \DateTimeZone($data);
60+
} catch (\Exception $e) {
61+
throw new NotNormalizableValueException($e->getMessage(), $e->getCode(), $e);
62+
}
63+
}
64+
65+
/**
66+
* {@inheritdoc}
67+
*/
68+
public function supportsDenormalization($data, $type, $format = null)
69+
{
70+
return \DateTimeZone::class === $type;
71+
}
72+
73+
/**
74+
* {@inheritdoc}
75+
*/
76+
public function hasCacheableSupportsMethod(): 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+
namespace Symfony\Component\Serializer\Tests\Normalizer;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Serializer\Normalizer\DateTimeZoneNormalizer;
16+
17+
/**
18+
* @author Jérôme Desjardins <jewome62@gmail.com>
19+
*/
20+
class DateTimeZoneNormalizerTest extends TestCase
21+
{
22+
/**
23+
* @var DateTimeZoneNormalizer
24+
*/
25+
private $normalizer;
26+
27+
protected function setUp()
28+
{
29+
$this->normalizer = new DateTimeZoneNormalizer();
30+
}
31+
32+
public function testSupportsNormalization()
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+
public function testNormalize()
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+
public function testNormalizeBadObjectTypeThrowsException()
49+
{
50+
$this->normalizer->normalize(new \stdClass());
51+
}
52+
53+
public function testSupportsDenormalization()
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+
public function testDenormalize()
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+
public function testDenormalizeNullTimeZoneThrowsException()
70+
{
71+
$this->normalizer->denormalize(null, \DateTimeZone::class, null);
72+
}
73+
74+
/**
75+
* @expectedException \Symfony\Component\Serializer\Exception\NotNormalizableValueException
76+
*/
77+
public function testDenormalizeBadTimeZoneThrowsException()
78+
{
79+
$this->normalizer->denormalize('Jupiter/Europa', \DateTimeZone::class, null);
80+
}
81+
}

0 commit comments

Comments
 (0)
0