Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/Illuminate/Http/Concerns/InteractsWithInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -322,13 +322,39 @@ 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.
*
* @param string $key
* @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)
{
Expand Down
44 changes: 44 additions & 0 deletions tests/Http/HttpRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]]);
Expand Down