Skip to content

Commit ce220cd

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 yet Commits ------- 1546c0d [Serializer] Add datetimezone normalizer
2 parents a68b4c7 + 1546c0d commit ce220cd

File tree

3 files changed

+161
-0
lines changed

3 files changed

+161
-0
lines changed

src/Symfony/Component/Serializer/CHANGELOG.md

+1
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
-----
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+
namespace Symfony\Component\Serializer\Normalizer;
13+
14+
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
15+
use Symfony\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 DateTimeZoneNormalizer implements NormalizerInterface, DenormalizerInterface, CacheableSupportsMethodInterface
23+
{
24+
/**
25+
* {@inheritdoc}
26+
*
27+
* @throws InvalidArgumentException
28+
*/
29+
public function normalize($object, $format = null, array $context = [])
30+
{
31+
if (!$object instanceof \DateTimeZone) {
32+
throw new InvalidArgumentException('The object must be an instance of "\DateTimeZone".');
33+
}
34+
35+
return $object->getName();
36+
}
37+
38+
/**
39+
* {@inheritdoc}
40+
*/
41+
public function supportsNormalization($data, $format = null)
42+
{
43+
return $data instanceof \DateTimeZone;
44+
}
45+
46+
/**
47+
* {@inheritdoc}
48+
*
49+
* @throws NotNormalizableValueException
50+
*/
51+
public function denormalize($data, $class, $format = null, array $context = [])
52+
{
53+
if ('' === $data || null === $data) {
54+
throw new NotNormalizableValueException('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+
return new \DateTimeZone($data);
59+
} catch (\Exception $e) {
60+
throw new NotNormalizableValueException($e->getMessage(), $e->getCode(), $e);
61+
}
62+
}
63+
64+
/**
65+
* {@inheritdoc}
66+
*/
67+
public function supportsDenormalization($data, $type, $format = null)
68+
{
69+
return \DateTimeZone::class === $type;
70+
}
71+
72+
/**
73+
* {@inheritdoc}
74+
*/
75+
public function hasCacheableSupportsMethod(): bool
76+
{
77+
return __CLASS__ === \get_class($this);
78+
}
79+
}
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)