diff --git a/src/Illuminate/Http/Concerns/InteractsWithInput.php b/src/Illuminate/Http/Concerns/InteractsWithInput.php index c420aad7fc71..a6b1e0983c05 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 a 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. * @@ -329,6 +353,8 @@ public function boolean($key = null, $default = false) * @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) { 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]]);