-
-
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
Conversation
public function normalize($object, $format = null, array $context = array()) | ||
{ | ||
if (!$object instanceof \DateTimeInterface) { | ||
throw new InvalidArgumentException('The object must implements the "\DateTimeInterface".'); |
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.
[...] implement [...]
:-)
Amazing! 👍 |
👍 |
❤️ |
👍 |
{ | ||
try { | ||
return \DateTime::class === $class ? new \DateTime($data) : new \DateTimeImmutable($data); | ||
} catch (\Exception $exception) { |
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 should rename this to $e
following this PR #14937
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.
agreed
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.
👍 I'll do it before the merge
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.
Ok, I let you manage this.
👍 Status: Reviewed |
f49fbc3
to
6749a70
Compare
This PR was merged into the 3.1-dev branch. Discussion ---------- [Serializer] Add a new DateTime normalizer | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | n/a | License | MIT | Doc PR | todo This PR add support for dates and times to the Normalizer component. It supports all date and time formats supported by PHP. Dates and times are normalized in the RFC 3339 format by default. The output format can be customized using the `$context` parameter. The default format can be changed using a constructor parameter. Usage: ```php use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer; use Symfony\Component\Serializer\Serializer; $serializer = new Serializer(array(new DateTimeNormalizer())); echo $serializer->normalize(new \DateTimeImmutable('2016/01/01')); // 2016-01-01T00:00:00+00:00 echo $serializer->normalize(new \DateTime('2016/01/01')); // 2016-01-01T00:00:00+00:00 echo $serializer->normalize(new \DateTime('2016/01/01'), null, array(DateTimeNormalizer::FORMAT_KEY => 'Y')) // 2016 var_dump($serializer->denormalize('2016-01-01T00:00:00+00:00', \DateTimeInterface::class)); // class DateTimeImmutable#1 (3) { // public $date => // string(26) "2016-01-01 00:00:00.000000" // public $timezone_type => // int(1) // public $timezone => // string(6) "+00:00" // } var_dump($serializer->denormalize('2016-01-01T00:00:00+00:00', \DateTime::class)); // class DateTime#1 (3) { // public $date => // string(26) "2016-01-01 00:00:00.000000" // public $timezone_type => // int(1) // public $timezone => // string(6) "+00:00" // } ``` Commits ------- 6749a70 [Serializer] Add a new DateTime normalizer
|
||
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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
No, denormalize
can parse any format supported by \DateTime::__construct()
: http://php.net/manual/en/datetime.formats.php
This PR was squashed before being merged into the master branch (closes #6314). Discussion ---------- New normalizers | Q | A | ------------- | --- | Doc fix? | no | New docs? | yes symfony/symfony#17603 symfony/symfony#17411 symfony/symfony#16164 | Applies to | 3.1 | Fixed tickets | Commits ------- 4ff3a28 New normalizers
This PR add support for dates and times to the Normalizer component. It supports all date and time formats supported by PHP. Dates and times are normalized in the RFC 3339 format by default. The output format can be customized using the
$context
parameter. The default format can be changed using a constructor parameter.Usage: