-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[HttpFoundation] ParameterBag::getEnum() #48820
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
df7d44a
to
ed20671
Compare
ed20671
to
c773701
Compare
return $default; | ||
} | ||
|
||
return $class::tryFrom($value) ?? $default; |
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.
wondering if its a good idea to return the default in case an invalid value (not a valid backend enum value) is passed? 🤔
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 think get() behaves the same, but in what case a default should be returned then?
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.
Better keep it consistent with the rest of the API I think.
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.
With get
, there is no invalid value.
In #48525 there is the same dilemma and the trend is to throw an exception when the value is invalid. This is particularly useful when the value comes from the query string, a BadRequest response is sent when the value is unexpected.
The default is returned when the parameter is not set.
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.
What do you think to add the same parameter $failOnInvalid
?
We could even add annotation psalm-assert
to narrow the type
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.
$failOnInvalid
is added for changing the behaviour while keeping backward compatibility. I think it is better to define a behaviour for all instead of adding options.
If you return the default in case of invalid value, you cannot distinguish when the parameter is not set and when the value is invalid.
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.
the behavior of the method is fixed now:
- we throw an
\UnexpectedValueException
when the value is not part of the backed enum, and not a string or an int (ie: it could be an array) - I've overridden the method in
InputBag
to throw aBadRequestException
in that case
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.
With remaining suggestion applied
3bb289f
to
6b6c7df
Compare
6b6c7df
to
e4ba7b7
Compare
Thank you @nikophil. |
Adds availability to get an enum directly from a parameter bag: