[Serializer] Add a new DateTime normalizer#17411
Merged
Merged
Conversation
Contributor
|
Amazing! 👍 |
Member
|
👍 |
Member
|
❤️ |
Member
|
👍 |
Contributor
There was a problem hiding this comment.
You should rename this to $e following this PR #14937
Member
Author
There was a problem hiding this comment.
👍 I'll do it before the merge
Member
|
👍 Status: Reviewed |
f49fbc3 to
6749a70
Compare
dunglas
added a commit
that referenced
this pull request
Jan 22, 2016
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)); |
Contributor
There was a problem hiding this comment.
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
Member
Author
There was a problem hiding this comment.
No, denormalize can parse any format supported by \DateTime::__construct(): http://php.net/manual/en/datetime.formats.php
xabbuh
added a commit
to symfony/symfony-docs
that referenced
this pull request
Mar 13, 2016
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
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
$contextparameter. The default format can be changed using a constructor parameter.Usage: