From b4ddc048dc6275c7edb0ec65e6327a61a8799872 Mon Sep 17 00:00:00 2001 From: Jason McCreary Date: Wed, 21 Sep 2022 10:45:52 -0400 Subject: [PATCH 1/3] Methods to cast request data as integer or float --- .../Http/Concerns/InteractsWithInput.php | 24 ++++++++++ tests/Http/HttpRequestTest.php | 44 +++++++++++++++++++ 2 files changed, 68 insertions(+) diff --git a/src/Illuminate/Http/Concerns/InteractsWithInput.php b/src/Illuminate/Http/Concerns/InteractsWithInput.php index c420aad7fc71..1ebec146d9cd 100644 --- a/src/Illuminate/Http/Concerns/InteractsWithInput.php +++ b/src/Illuminate/Http/Concerns/InteractsWithInput.php @@ -322,6 +322,30 @@ public function boolean($key = null, $default = false) return filter_var($this->input($key, $default), FILTER_VALIDATE_BOOLEAN); } + /** + * Retrieve input as an integer value. + * + * @param string $key + * @param int $default + * @return int + */ + public function integer($key, $default = 0) + { + return intval($this->input($key, $default)); + } + + /** + * Retrieve input as an float value. + * + * @param string $key + * @param float $default + * @return float + */ + public function float($key, $default = 0.0) + { + return floatval($this->input($key, $default)); + } + /** * Retrieve input from the request as a Carbon instance. * diff --git a/tests/Http/HttpRequestTest.php b/tests/Http/HttpRequestTest.php index dedf9f685381..b98bf4dc245c 100644 --- a/tests/Http/HttpRequestTest.php +++ b/tests/Http/HttpRequestTest.php @@ -562,6 +562,50 @@ public function testBooleanMethod() $this->assertTrue($request->boolean('with_yes')); } + public function testIntegerMethod() + { + $request = Request::create('/', 'GET', [ + 'int' => '123', + 'raw_int' => 456, + 'zero_padded' => '078', + 'space_padded' => ' 901', + 'nan' => 'nan', + 'mixed'=> '1ab', + 'underscore_notation'=> '2_000', + ]); + $this->assertSame(123, $request->integer('int')); + $this->assertSame(456, $request->integer('raw_int')); + $this->assertSame(78, $request->integer('zero_padded')); + $this->assertSame(901, $request->integer('space_padded')); + $this->assertSame(0, $request->integer('nan')); + $this->assertSame(1, $request->integer('mixed')); + $this->assertSame(2, $request->integer('underscore_notation')); + $this->assertSame(123456, $request->integer('unknown_key', 123456)); + } + + public function testFloatMethod() + { + $request = Request::create('/', 'GET', [ + 'float' => '1.23', + 'raw_float' => 45.6, + 'decimal_only' => '.6', + 'zero_padded' => '0.78', + 'space_padded' => ' 90.1', + 'nan' => 'nan', + 'mixed'=> '1.ab', + 'scientific_notation'=> '1e3', + ]); + $this->assertSame(1.23, $request->float('float')); + $this->assertSame(45.6, $request->float('raw_float')); + $this->assertSame(.6, $request->float('decimal_only')); + $this->assertSame(0.78, $request->float('zero_padded')); + $this->assertSame(90.1, $request->float('space_padded')); + $this->assertSame(0.0, $request->float('nan')); + $this->assertSame(1.0, $request->float('mixed')); + $this->assertSame(1e3, $request->float('scientific_notation')); + $this->assertSame(123.456, $request->float('unknown_key', 123.456)); + } + public function testCollectMethod() { $request = Request::create('/', 'GET', ['users' => [1, 2, 3]]); From 346aa3ce4caf5f46a77c5ffbdd3500577cfe3fb3 Mon Sep 17 00:00:00 2001 From: Jason McCreary Date: Wed, 21 Sep 2022 11:39:17 -0400 Subject: [PATCH 2/3] Update docblock --- src/Illuminate/Http/Concerns/InteractsWithInput.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Illuminate/Http/Concerns/InteractsWithInput.php b/src/Illuminate/Http/Concerns/InteractsWithInput.php index 1ebec146d9cd..3e1758bc9320 100644 --- a/src/Illuminate/Http/Concerns/InteractsWithInput.php +++ b/src/Illuminate/Http/Concerns/InteractsWithInput.php @@ -353,6 +353,8 @@ public function float($key, $default = 0.0) * @param string|null $format * @param string|null $tz * @return \Illuminate\Support\Carbon|null + * + * @throws \Carbon\Exceptions\InvalidFormatException */ public function date($key, $format = null, $tz = null) { From 5664521c7e74b3c2173437cb2f1c6693d6714e57 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 21 Sep 2022 11:08:08 -0500 Subject: [PATCH 3/3] Update InteractsWithInput.php --- src/Illuminate/Http/Concerns/InteractsWithInput.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Http/Concerns/InteractsWithInput.php b/src/Illuminate/Http/Concerns/InteractsWithInput.php index 3e1758bc9320..a6b1e0983c05 100644 --- a/src/Illuminate/Http/Concerns/InteractsWithInput.php +++ b/src/Illuminate/Http/Concerns/InteractsWithInput.php @@ -335,7 +335,7 @@ public function integer($key, $default = 0) } /** - * Retrieve input as an float value. + * Retrieve input as a float value. * * @param string $key * @param float $default