You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This discussion is about the following methods implemented in InteractsWithInput for Request class.
str()
string()
boolean()
integer()
float()
enum()
date()
Suggestion 1: A way to get a string, not Stringable
Both string() and str() methods return a Stringable object, not a string. Therefore, we have to always cast it to a string in our code, which is repetitive.
$name = (string) $request->string('name');
The problem with Stringable is when you use strict types in PHP. Most (almost all) dependencies are typed to take strings as parameters, never Stringable.
<?phpdeclare(strict_types=1);
$name = $request->string('name');
SomeLibrary::process($name); // <- this throws an type error when the dependency uses strict types
For that reason, my first suggestion is that there should be a way to get a string directly from the request. However, I am not sure what the best way to implement this.
Change string() method (backward incompatible and risky)
Add a new method like stringRaw()
Suggestion 2: Null inconsistency
enum() and date() methods can return null values. For this reason, these functions are almost not useful when writing strictly type-safe code.
$type = $request->enum('type', TypeEnum::class); // TypeEnum|null
SomeLibrary::process($type); // this shows a static error because `enum()` cannot guarantee a non-null value
Therefore we have to always do this, which is verbose.
$type = TypeEnum::from((string) $request->string('type'));
SomeLibrary::process($type); // no static errors
The ideal solution would be throw an exception on invalid enums in enum() method. Currently, it uses ::tryFrom - it could be changed to ::from().
But, again, this will be backward incompatible.
Suggestion 3: A new nullable method
Sometimes, you need to get inputs that may be null. Currently, here's how you would do that (there could be a less complicated way).
#1 and #2 suggestions needs to be discussed because of the backward compatibility issues. #3 can be easily implemented (though it won't make sense with enum() and date()). I'm happy to work on a PR for #3.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
This discussion is about the following methods implemented in InteractsWithInput for
Request
class.str()
string()
boolean()
integer()
float()
enum()
date()
Suggestion 1: A way to get a string, not Stringable
Both
string()
andstr()
methods return a Stringable object, not a string. Therefore, we have to always cast it to a string in our code, which is repetitive.The problem with
Stringable
is when you use strict types in PHP. Most (almost all) dependencies are typed to takestring
s as parameters, neverStringable
.For that reason, my first suggestion is that there should be a way to get a
string
directly from the request. However, I am not sure what the best way to implement this.string()
method (backward incompatible and risky)stringRaw()
Suggestion 2: Null inconsistency
enum()
anddate()
methods can return null values. For this reason, these functions are almost not useful when writing strictly type-safe code.Therefore we have to always do this, which is verbose.
The ideal solution would be throw an exception on invalid enums in
enum()
method. Currently, it uses::tryFrom
- it could be changed to::from()
.But, again, this will be backward incompatible.
Suggestion 3: A new nullable method
Sometimes, you need to get inputs that may be
null
. Currently, here's how you would do that (there could be a less complicated way).My suggestion is:
#1
and#2
suggestions needs to be discussed because of the backward compatibility issues.#3
can be easily implemented (though it won't make sense withenum()
anddate()
). I'm happy to work on a PR for#3
.Beta Was this translation helpful? Give feedback.
All reactions