Skip to content

[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

Merged
merged 1 commit into from
Jan 22, 2016

Conversation

dunglas
Copy link
Member

@dunglas dunglas commented Jan 17, 2016

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:

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"
// }

public function normalize($object, $format = null, array $context = array())
{
if (!$object instanceof \DateTimeInterface) {
throw new InvalidArgumentException('The object must implements the "\DateTimeInterface".');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[...] implement [...] :-)

@GuilhemN
Copy link
Contributor

Amazing! 👍

@fabpot
Copy link
Member

fabpot commented Jan 20, 2016

👍

@derrabus
Copy link
Member

❤️

@stof
Copy link
Member

stof commented Jan 20, 2016

👍

{
try {
return \DateTime::class === $class ? new \DateTime($data) : new \DateTimeImmutable($data);
} catch (\Exception $exception) {
Copy link
Contributor

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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed

Copy link
Member Author

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

Copy link
Member

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.

@xabbuh
Copy link
Member

xabbuh commented Jan 21, 2016

👍

Status: Reviewed

@dunglas dunglas force-pushed the normalizer_datetime branch from f49fbc3 to 6749a70 Compare January 22, 2016 07:14
@dunglas dunglas merged commit 6749a70 into symfony:master Jan 22, 2016
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
@dunglas dunglas deleted the normalizer_datetime branch January 22, 2016 07:41

public function testDenormalize()
{
$this->assertEquals(new \DateTimeImmutable('2016/01/01'), $this->normalizer->denormalize('2016-01-01T00:00:00+00:00', \DateTimeInterface::class));
Copy link
Member

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

Copy link
Member Author

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

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
@fabpot fabpot mentioned this pull request May 13, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.