-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Serializer] Add a new DateTime normalizer #17411
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* 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\UnexpectedValueException; | ||
|
||
/** | ||
* Normalizes an object implementing the {@see \DateTimeInterface} to a date string. | ||
* Denormalizes a date string to an instance of {@see \DateTime} or {@see \DateTimeImmutable}. | ||
* | ||
* @author Kévin Dunglas <[email protected]> | ||
*/ | ||
class DateTimeNormalizer implements NormalizerInterface, DenormalizerInterface | ||
{ | ||
const FORMAT_KEY = 'datetime_format'; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $format; | ||
|
||
/** | ||
* @param string $format | ||
*/ | ||
public function __construct($format = \DateTime::RFC3339) | ||
{ | ||
$this->format = $format; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
* | ||
* @throws InvalidArgumentException | ||
*/ | ||
public function normalize($object, $format = null, array $context = array()) | ||
{ | ||
if (!$object instanceof \DateTimeInterface) { | ||
throw new InvalidArgumentException('The object must implement the "\DateTimeInterface".'); | ||
} | ||
|
||
$format = isset($context[self::FORMAT_KEY]) ? $context[self::FORMAT_KEY] : $this->format; | ||
|
||
return $object->format($format); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supportsNormalization($data, $format = null) | ||
{ | ||
return $data instanceof \DateTimeInterface; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
* | ||
* @throws UnexpectedValueException | ||
*/ | ||
public function denormalize($data, $class, $format = null, array $context = array()) | ||
{ | ||
try { | ||
return \DateTime::class === $class ? new \DateTime($data) : new \DateTimeImmutable($data); | ||
} catch (\Exception $e) { | ||
throw new UnexpectedValueException($e->getMessage(), $e->getCode(), $e); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supportsDenormalization($data, $type, $format = null) | ||
{ | ||
$supportedTypes = array( | ||
\DateTimeInterface::class => true, | ||
\DateTimeImmutable::class => true, | ||
\DateTime::class => true, | ||
); | ||
|
||
return isset($supportedTypes[$type]); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* 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 Symfony\Component\Serializer\Normalizer\DateTimeNormalizer; | ||
|
||
/** | ||
* @author Kévin Dunglas <[email protected]> | ||
*/ | ||
class DateTimeNormalizerTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @var DateTimeNormalizer | ||
*/ | ||
private $normalizer; | ||
|
||
public function setUp() | ||
{ | ||
$this->normalizer = new DateTimeNormalizer(); | ||
} | ||
|
||
public function testSupportNormalization() | ||
{ | ||
$this->assertTrue($this->normalizer->supportsNormalization(new \DateTime())); | ||
$this->assertTrue($this->normalizer->supportsNormalization(new \DateTimeImmutable())); | ||
$this->assertFalse($this->normalizer->supportsNormalization(new \stdClass())); | ||
} | ||
|
||
public function testNormalize() | ||
{ | ||
$this->assertEquals('2016-01-01T00:00:00+00:00', $this->normalizer->normalize(new \DateTime('2016/01/01'))); | ||
$this->assertEquals('2016-01-01T00:00:00+00:00', $this->normalizer->normalize(new \DateTimeImmutable('2016/01/01'))); | ||
} | ||
|
||
public function testContextFormat() | ||
{ | ||
$this->assertEquals('2016', $this->normalizer->normalize(new \DateTime('2016/01/01'), null, array(DateTimeNormalizer::FORMAT_KEY => 'Y'))); | ||
} | ||
|
||
public function testConstructorFormat() | ||
{ | ||
$this->assertEquals('16', (new DateTimeNormalizer('y'))->normalize(new \DateTime('2016/01/01'))); | ||
} | ||
|
||
/** | ||
* @expectedException \Symfony\Component\Serializer\Exception\InvalidArgumentException | ||
* @expectedExceptionMessage The object must implement the "\DateTimeInterface". | ||
*/ | ||
public function testInvalidDataThrowException() | ||
{ | ||
$this->normalizer->normalize(new \stdClass()); | ||
} | ||
|
||
public function testSupportDenormalization() | ||
{ | ||
$this->assertTrue($this->normalizer->supportsDenormalization('2016-01-01T00:00:00+00:00', \DateTimeInterface::class)); | ||
$this->assertTrue($this->normalizer->supportsDenormalization('2016-01-01T00:00:00+00:00', \DateTime::class)); | ||
$this->assertTrue($this->normalizer->supportsDenormalization('2016-01-01T00:00:00+00:00', \DateTimeImmutable::class)); | ||
$this->assertFalse($this->normalizer->supportsDenormalization('foo', 'Bar')); | ||
} | ||
|
||
public function testDenormalize() | ||
{ | ||
$this->assertEquals(new \DateTimeImmutable('2016/01/01'), $this->normalizer->denormalize('2016-01-01T00:00:00+00:00', \DateTimeInterface::class)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So only this one format is supported for denormalize? It feels weird - since normalize allows you to specify a different format. But I personally don't denormalize often There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, |
||
$this->assertEquals(new \DateTimeImmutable('2016/01/01'), $this->normalizer->denormalize('2016-01-01T00:00:00+00:00', \DateTimeImmutable::class)); | ||
$this->assertEquals(new \DateTime('2016/01/01'), $this->normalizer->denormalize('2016-01-01T00:00:00+00:00', \DateTime::class)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The thrown exception in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Test added. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
} | ||
|
||
/** | ||
* @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException | ||
*/ | ||
public function testInvalidDateThrowException() | ||
{ | ||
$this->normalizer->denormalize('invalid date', \DateTimeInterface::class); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One more test could assert, when the provided
$context
not contains the 'datetime_format' key, then it normalizes with$this->format
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is already a test for that (
testConstructorFormat
).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are right, ty.