Skip to content
Closed
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
16 changes: 16 additions & 0 deletions src/Illuminate/Support/Traits/InteractsWithData.php
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,22 @@ public function string($key, $default = null)
return new Stringable($this->data($key, $default));
}

/**
* Retrieve data from the instance as a string.
*
* @param string $key
* @param mixed $default
* @return string|null
*/
public function plainString($key, $default = null)
{
if ($this->isNotFilled($key)) {
return value($default);
}

return (string) $this->data($key);
}

/**
* Retrieve data as a boolean value.
*
Expand Down
25 changes: 25 additions & 0 deletions tests/Http/HttpRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,31 @@ public function testStringMethod()
$this->assertSame('', $request->string('unknown_key')->value());
}

public function testPlainStringMethod()
{
$request = Request::create('/', 'GET', [
'int' => 123,
'int_str' => '456',
'float' => 123.456,
'float_str' => '123.456',
'float_zero' => 0.000,
'float_str_zero' => '0.000',
'str' => 'abc',
'empty_str' => '',
'null' => null,
]);
$this->assertSame('123', $request->plainString('int'));
$this->assertSame('456', $request->plainString('int_str'));
$this->assertSame('123.456', $request->plainString('float'));
$this->assertSame('123.456', $request->plainString('float_str'));
$this->assertSame('0', $request->plainString('float_zero'));
$this->assertSame('0.000', $request->plainString('float_str_zero'));
$this->assertSame(null, $request->plainString('empty_str'));
$this->assertSame(null, $request->plainString('null'));
$this->assertSame(null, $request->plainString('unknown_key'));
$this->assertSame('default', $request->plainString('unknown_key', 'default'));
}

public function testBooleanMethod()
{
$request = Request::create('/', 'GET', ['with_trashed' => 'false', 'download' => true, 'checked' => 1, 'unchecked' => '0', 'with_on' => 'on', 'with_yes' => 'yes']);
Expand Down
Loading