Skip to content

[Serializer] Better example for date denormalization #19356

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
Dec 30, 2023
Merged

[Serializer] Better example for date denormalization #19356

merged 1 commit into from
Dec 30, 2023

Conversation

misaert
Copy link
Contributor

@misaert misaert commented Dec 29, 2023

Hi,

When we use the DateTimeNormalizer to denormalize a date without time, using the same format Y-m-d does not seem a good example because the object DateTimeImmutable has the time from the moment of denormalization.

Example:

$dateTimeFormat = 'Y-m-d'; // From `$context[self::FORMAT_KEY]`

$result = DateTimeImmutable::createFromFormat($dateTimeFormat, '2024-01-31');

var_dump($result);

Output:

object(DateTimeImmutable)symfony#1 (3) {
  ["date"]=>
  string(26) "2024-01-31 17:14:23.000000"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(16) "Europe/Amsterdam"
}

It seems better to differentiate the normalization and the denormalization like:

#[Context(
    normalizationContext: [DateTimeNormalizer::FORMAT_KEY => 'Y-m-d'],
    denormalizationContext: [DateTimeNormalizer::FORMAT_KEY => '!Y-m-d'],
)]

The format !Y-m-d prevents to have a time and weird behavior.

$dateTimeFormat = '!Y-m-d'; // From `$context[self::FORMAT_KEY]`

$result = DateTimeImmutable::createFromFormat($dateTimeFormat, '2024-01-31');

var_dump($result);

Output:

object(DateTimeImmutable)symfony#1 (3) {
  ["date"]=>
  string(26) "2024-01-31 00:00:00.000000"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(16) "Europe/Amsterdam"
}

WDYT?

Mickaël

@OskarStark
Copy link
Contributor

Thank you Mickaël.

@OskarStark OskarStark merged commit 57e50c3 into symfony:5.4 Dec 30, 2023
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.

4 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy