-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[HttpFoundation] adds getDate method to the ParameterBag class #19296
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
Changes from 1 commit
319b955
ba3af51
e7b221b
e31f24b
969b4ba
b3a292e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
…converted to a datetime; added more tests
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -192,25 +192,21 @@ public function getBoolean($key, $default = false) | |
* | ||
* @param string $key The parameter key | ||
* @param string $format The expected date format | ||
* @param \DateTime|null $default The default value if the parameter key does not exist | ||
* @param \DateTimeZone|null $timeZone | ||
* @param string $default The default value to be converted to a DateTime object if the parameter key does not exist | ||
* @param \DateTimeZone|null $timeZone A DateTimeZone object representing the desired time zone | ||
* | ||
* @return \DateTime|null | ||
*/ | ||
public function getDate($key, $format = 'Y-m-d', $default = null, $timeZone = null) | ||
public function getDate($key, $format, $default = null, $timeZone = null) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Still not sure why |
||
{ | ||
if (!$this->has($key)) { | ||
return $default; | ||
} | ||
|
||
$time = $this->get($key); | ||
$time = $this->get($key, (string) $default); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about if (null === ($time = $this->get($key, $default))) {
return null;
} Next, if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not too sold on creating a variable and checking for value in the same line, however that is a matter of preference. Early return might be doable, but I avoided it because fabbot doesn't accept Regarding There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do it in 2 lines then :) Its more about not casting Hence i would throw an exception when the value is invalid for the given format. Ignoring the user's expected format and just return null can be a real WTF. This is also the difference compared to Separating null from void is a good thing :) And this should be accepted by now afaik (symfony/symfony-docs#6614) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree about not casting $default |
||
|
||
// if the user has specified a timezone then pass that | ||
// otherwise do not even attempt to put a value but rather let the runtime decide | ||
// the default value by itself | ||
// this is in order to ensure compatibility with all php versions since | ||
// some accept null as a TimeZone parameter and others do not | ||
if ($timeZone !== null) { | ||
if (null !== $timeZone) { | ||
$result = \DateTime::createFromFormat($format, $time, $timeZone); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If format is |
||
} else { | ||
$result = \DateTime::createFromFormat($format, $time); | ||
|
@@ -220,21 +216,6 @@ public function getDate($key, $format = 'Y-m-d', $default = null, $timeZone = nu | |
return false === $result ? null : $result; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just read @nicolas-grekas comment once more
I guess your right. Letting the app error 500 based on user input is not desired. Maybe it should only throw |
||
} | ||
|
||
/** | ||
* Returns the parameter value converted to a DateTime object while also parsing the time. | ||
* | ||
* @param string $key The parameter key | ||
* @param string $format The expected date format | ||
* @param \DateTime|null $default The default value if the parameter key does not exist | ||
* @param \DateTimeZone|null $timeZone | ||
* | ||
* @return \DateTime|null | ||
*/ | ||
public function getDateTime($key, $format = 'Y-m-d H:i:s', $default = null, \DateTimeZone $timeZone = null) | ||
{ | ||
return $this->getDate($key, $format, $default, $timeZone); | ||
} | ||
|
||
/** | ||
* Filter key. | ||
* | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not passing a
DateTime
object as the default value?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would argue that having a string to be returned as a date feels more natural and reuses existing parameters, also as per @nicolas-grekas suggestion.
Take the following examples. I would argue that the second one is more verbose and redundant as the timezone needs to be specified twice, which is why I ended up with a string as the default (in the original PR the default parameter was actually a datetime object as you suggest).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@xabbuh I stick to my first pov: all the other getter (getAlpha, etc.) start with something like
$this->get($key, $default)
then do transform the result.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, let's keep it this way to be consistent with the other methods.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Though, the other getter methods accept
mixed
argument which works if you only do a simple type cast (for ints, for example, as an int cast to int is still an int). So I think we should still allow (but not require) to pass a\DateTime
object here and simply not transform it when it is used.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@maldoinc wdyt of @xabbuh 's idea, allowing a DateTime(Interface) here?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess we can allow it for the
$default
parameter, although the combination of a\DateTimeInterface
and a string is an odd one.I'm also a bit torn on the error handling aspect.
Usually I'd expect the function to throw an exception on malformed input. However returning fasly values is more PHP-esque(?) @ro0NL also seems to be on this train. What's your input on this: @nicolas-grekas @xabbuh?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
returning
null
in case of parse failure looks fine to me: this is user input, ie potentially garbage. An exception would force a mandatory try/catch around any call to getDate (which most will forget). Doingif ($date = $foo->getDate(..))
has less boilerplate to me.If we accept DateTimeInterface as default, then the return type hint should be changed to DateTimeInterface|null