8000 [Form] Add support for DateTimeImmutable by jameshalsall · Pull Request #19889 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Form] Add support for DateTimeImmutable #19889

New issue 8000

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

Closed

Conversation

jameshalsall
Copy link
Contributor
@jameshalsall jameshalsall commented Sep 8, 2016
Q A
Branch? master
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #9508
License MIT
Doc PR TODO

This PR is an initial concept for adding support for \DateTimeImmutable in the Form component.

The DateType, TimeType and DateTimeType now support a new "input" option called "datetimeimmutable". When this is set the returned values from $form->getData() will be instances of \DateTimeImmutable instead of \DateTime (which is the case for an "input" value of "datetime" - the default).

The existing DateTime transformers now take an additional constructor argument indicating whether the DateTime object is immutable, affecting the reverse-transformed value

I'm looking for feedback on this approach before I finalise it.

namespace Symfony\Component\Form\Extension\Core\DataTransformer;

/**
* @package Symfony\Component\Form\Extension\Core\DataTransformer
Copy link
Contributor
@HeahDude HeahDude Sep 8, 2016

Choose a reason for hiding this comment

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

We don't use @package in symfony core

@HeahDude
Copy link
Contributor
HeahDude commented Sep 9, 2016

Much better :)

@gharlan
Copy link
Contributor
gharlan commented Sep 14, 2016

I would like to have an option to use custom DateTime class (extending \DateTime or DateTimeImmutable).

@@ -108,7 +109,7 @@ public function transform($dateTime)
*
* @param array $value Localized date
*
* @return \DateTime Normalized date
* @return \DateTimeInterface Normalized date
Copy link
Contributor

Choose a reason for hiding this comment

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

the hint should be explicit here IMO: @return \DateTime|DateTimeImmutable since no other implementation is used.

Copy link
Contributor Author
@jameshalsall jameshalsall Sep 20, 2016

Choose a reason for hiding this comment

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

I think leaving it as \DateTimeInterface is still semantically correct, and is more concise

@HeahDude
Copy link
Contributor

@gharlan I think this is a topic for another PR.

@jameshalsall jameshalsall force-pushed the immutable-datetime-in-form branch from 4062815 to 8d60d72 Compare September 20, 2016 20:12
@jameshalsall
Copy link
Contributor Author

@fabpot @webmozart what are the chances of this making it into 3.2 before the end of the development phase?

Copy link
Contributor
@lemoinem lemoinem left a comment

Choose a reason for hiding this comment

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

The tests are failing because of issues in SecurityBundle or the VarDumper Component, and a (purposefully) malformed ini for hhvm.

If you could prepare the docs PR, I think this is quite ready to be merged.

@jameshalsall
Copy link
Contributor Author

I'm away for the next few days so I'll work on the docs PR when I return.

@jameshalsall jameshalsall reopened this Sep 28, 2016
@javiereguiluz javiereguiluz added this to the 3.3 milestone Nov 7, 2016
@kaiwa
Copy link
kaiwa commented Feb 1, 2017

I'll work on the docs PR when I return.

@jameshalsall I hope you returned 😆

@jameshalsall
Copy link
Contributor Author

@kaiwa Haha, I completely forgot about this - I'll add it to my TODO list to create the docs PR

@fabpot
Copy link
Member
fabpot commented Mar 1, 2017

This one needs a rebase :)

@HeahDude
Copy link
Contributor
HeahDude commented Mar 1, 2017

I would not merge this one as is, in favor of more work to address the last (and most important IMHO) part of the related issue:

If a new major version of Symfony bumps the requirement to PHP 5.5+, the normalized data could then be switched to the DateTimeImmutable class to simplify the logic (which is currently cloning the DateTime to avoid modifying the original one.

@fabpot
Copy link
Member
fabpot commented Mar 1, 2017

Makes sense. So, I'm closing this one as Symfony 3 does require PHP 5.5+.

@fabpot fabpot closed this Mar 1, 2017
@HeahDude
Copy link
Contributor
HeahDude commented Mar 1, 2017

I have an idea of what to do for this, but some other issues must be addressed first.

@HeahDude
Copy link
Contributor
HeahDude 8000 commented Mar 1, 2017

In the mean time, it's really easy to implement a model transformer in user land if needed.

@jameshalsall
Copy link
Contributor Author

Wish you'd have said that back in September ;)

@nicolas-grekas nicolas-grekas removed this from the 3.x milestone Mar 24, 2017
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.

10 participants
0