[Serializer] Add datetimezone normalizer

This commit is contained in:
jewome62 2019-04-06 17:45:16 +02:00 committed by Fabien Potencier
parent 09dee1737d
commit 1546c0dfa0
3 changed files with 161 additions and 0 deletions

View File

@ -5,6 +5,7 @@ CHANGELOG
-----
* added the list of constraint violations' parameters in `ConstraintViolationListNormalizer`
* added support for serializing `DateTimeZone` objects
4.2.0
-----

View File

@ -0,0 +1,79 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Serializer\Normalizer;
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
/**
* Normalizes a {@see \DateTimeZone} object to a timezone string.
*
* @author Jérôme Desjardins <jewome62@gmail.com>
*/
class DateTimeZoneNormalizer implements NormalizerInterface, DenormalizerInterface, CacheableSupportsMethodInterface
{
/**
* {@inheritdoc}
*
* @throws InvalidArgumentException
*/
public function normalize($object, $format = null, array $context = [])
{
if (!$object instanceof \DateTimeZone) {
throw new InvalidArgumentException('The object must be an instance of "\DateTimeZone".');
}
return $object->getName();
}
/**
* {@inheritdoc}
*/
public function supportsNormalization($data, $format = null)
{
return $data instanceof \DateTimeZone;
}
/**
* {@inheritdoc}
*
* @throws NotNormalizableValueException
*/
public function denormalize($data, $class, $format = null, array $context = [])
{
if ('' === $data || null === $data) {
throw new NotNormalizableValueException('The data is either an empty string or null, you should pass a string that can be parsed as a DateTimeZone.');
}
try {
return new \DateTimeZone($data);
} catch (\Exception $e) {
throw new NotNormalizableValueException($e->getMessage(), $e->getCode(), $e);
}
}
/**
* {@inheritdoc}
*/
public function supportsDenormalization($data, $type, $format = null)
{
return \DateTimeZone::class === $type;
}
/**
* {@inheritdoc}
*/
public function hasCacheableSupportsMethod(): bool
{
return __CLASS__ === \get_class($this);
}
}

View File

@ -0,0 +1,81 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Serializer\Tests\Normalizer;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Serializer\Normalizer\DateTimeZoneNormalizer;
/**
* @author Jérôme Desjardins <jewome62@gmail.com>
*/
class DateTimeZoneNormalizerTest extends TestCase
{
/**
* @var DateTimeZoneNormalizer
*/
private $normalizer;
protected function setUp()
{
$this->normalizer = new DateTimeZoneNormalizer();
}
public function testSupportsNormalization()
{
$this->assertTrue($this->normalizer->supportsNormalization(new \DateTimeZone('UTC')));
$this->assertFalse($this->normalizer->supportsNormalization(new \DateTimeImmutable()));
$this->assertFalse($this->normalizer->supportsNormalization(new \stdClass()));
}
public function testNormalize()
{
$this->assertEquals('UTC', $this->normalizer->normalize(new \DateTimeZone('UTC')));
$this->assertEquals('Asia/Tokyo', $this->normalizer->normalize(new \DateTimeZone('Asia/Tokyo')));
}
/**
* @expectedException \Symfony\Component\Serializer\Exception\InvalidArgumentException
*/
public function testNormalizeBadObjectTypeThrowsException()
{
$this->normalizer->normalize(new \stdClass());
}
public function testSupportsDenormalization()
{
$this->assertTrue($this->normalizer->supportsDenormalization(null, \DateTimeZone::class));
$this->assertFalse($this->normalizer->supportsDenormalization(null, \DateTimeImmutable::class));
$this->assertFalse($this->normalizer->supportsDenormalization(null, \stdClass::class));
}
public function testDenormalize()
{
$this->assertEquals(new \DateTimeZone('UTC'), $this->normalizer->denormalize('UTC', \DateTimeZone::class, null));
$this->assertEquals(new \DateTimeZone('Asia/Tokyo'), $this->normalizer->denormalize('Asia/Tokyo', \DateTimeZone::class, null));
}
/**
* @expectedException \Symfony\Component\Serializer\Exception\NotNormalizableValueException
*/
public function testDenormalizeNullTimeZoneThrowsException()
{
$this->normalizer->denormalize(null, \DateTimeZone::class, null);
}
/**
* @expectedException \Symfony\Component\Serializer\Exception\NotNormalizableValueException
*/
public function testDenormalizeBadTimeZoneThrowsException()
{
$this->normalizer->denormalize('Jupiter/Europa', \DateTimeZone::class, null);
}
}