-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[PropertyAccess] Auto-cast from/to DateTime/Immutable when appropriate #50295
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
6b968f0
to
9b6495a
Compare
would the try/catch be possible on the calling side? |
If we go this way, I suggest supporting the auto-cast in both directions. This would make it easier to change the default input mode in the Form component in a future version. |
This PR was merged into the 6.4 branch. Discussion ---------- Remove unnecessary usages of DateTime | Q | A | ------------- | --- | Branch? | 6.3 | Bug fix? | no | New feature? | no | Deprecations? | no | Tickets | Part of #47580 | License | MIT | Doc PR | - Together with #50290, this PR removes `DateTime` everywhere possible. What remains is: - test cases that test both DateTime and DateTimeImmutable - legit to keep - date/time form-types and transformers in the Form component - we'd need a separate effort for them, related to #50295 - `PersistentTokenInterface::getLastUsed(): \DateTime` - separate effort also Commits ------- 8b08294 Remove unnecessary usages of DateTime
9b6495a
to
bef2047
Compare
bef2047
to
1b16dc0
Compare
Done for both. |
1b16dc0
to
3156a60
Compare
Now with tests. PR ready for review. |
Thank you @nicolas-grekas. |
Good that the feature is in, but you're now relying on exceptions for normal program flow. The approach I followed in #47776 does not, and may therefore be more performant I believe? |
Could you run some bench? The reflection logic might be quite involving also, especially considering using types/etc |
Also I made some specific exceptions:
Your implementation is not excluding adder/remover (which may cause collections to end up with a rag tag mix of all types) nor union/intersection types. |
Best reviewed ignoring whitespaces.
My starting point is a form with a date defined as such:
Then doing something like:
If I submit this form, I get:
The current solution is to set option
input
todatetime_immutable
inbuildForm()
.But this is boring. Too much boilerplate.
I've tried looking at the Form component to define the
input
option based on the entity provided when building the form, but this entity is not passed to sub-forms, so that we have no way to do this.The only way I found is the one attached: if we cannot set a
DateTime
, we try setting aDateTimeImmutable
.I thought about changing the default value of option
input
todatetime_immutable
, but this would need a nasty deprecation that'd force everybody to write even more boilerplate before v7, while what we want is less.So here we are.