Skip to content
Draft
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
19 changes: 14 additions & 5 deletions src/Illuminate/Support/Traits/InteractsWithData.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Date;
use Illuminate\Support\Str;
use InvalidArgumentException;
use stdClass;

use function Illuminate\Support\enum_value;
Expand Down Expand Up @@ -231,19 +232,27 @@ protected function isEmptyString($key)
*/
public function str($key, $default = null)
{
return $this->string($key, $default);
return Str::of($this->data($key, $default));
}

/**
* Retrieve data from the instance as a Stringable instance.
* Retrieve data from the instance as a string.
*
* @param string $key
* @param mixed $default
* @return \Illuminate\Support\Stringable
* @return string
*/
public function string($key, $default = null)
public function string($key, $default = null): string
{
return Str::of($this->data($key, $default));
$value = $this->data($key, $default);

if (! is_scalar($value) && ! is_null($value)) {
throw new InvalidArgumentException(
sprintf('Value for key [%s] must be a string, %s given.', $key, gettype($value))
);
}

return (string) $value;
}

/**
Expand Down
48 changes: 37 additions & 11 deletions tests/Http/HttpRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -604,17 +604,43 @@ public function testStringMethod()
'empty_str' => '',
'null' => null,
]);
$this->assertTrue($request->string('int') instanceof Stringable);
$this->assertTrue($request->string('unknown_key') instanceof Stringable);
$this->assertSame('123', $request->string('int')->value());
$this->assertSame('456', $request->string('int_str')->value());
$this->assertSame('123.456', $request->string('float')->value());
$this->assertSame('123.456', $request->string('float_str')->value());
$this->assertSame('0', $request->string('float_zero')->value());
$this->assertSame('0.000', $request->string('float_str_zero')->value());
$this->assertSame('', $request->string('empty_str')->value());
$this->assertSame('', $request->string('null')->value());
$this->assertSame('', $request->string('unknown_key')->value());
$this->assertIsString($request->string('int'));
$this->assertIsString($request->string('unknown_key'));
$this->assertSame('123', $request->string('int'));
$this->assertSame('456', $request->string('int_str'));
$this->assertSame('123.456', $request->string('float'));
$this->assertSame('123.456', $request->string('float_str'));
$this->assertSame('0', $request->string('float_zero'));
$this->assertSame('0.000', $request->string('float_str_zero'));
$this->assertSame('', $request->string('empty_str'));
$this->assertSame('', $request->string('null'));
$this->assertSame('', $request->string('unknown_key'));
}

public function testStrMethod()
{
$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->assertTrue($request->str('int') instanceof Stringable);
$this->assertTrue($request->str('unknown_key') instanceof Stringable);
$this->assertSame('123', $request->str('int')->value());
$this->assertSame('456', $request->str('int_str')->value());
$this->assertSame('123.456', $request->str('float')->value());
$this->assertSame('123.456', $request->str('float_str')->value());
$this->assertSame('0', $request->str('float_zero')->value());
$this->assertSame('0.000', $request->str('float_str_zero')->value());
$this->assertSame('', $request->str('empty_str')->value());
$this->assertSame('', $request->str('null')->value());
$this->assertSame('', $request->str('unknown_key')->value());
}

public function testBooleanMethod()
Expand Down
23 changes: 11 additions & 12 deletions tests/Support/SupportFluentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Support\Fluent;
use Illuminate\Support\Stringable;
use InvalidArgumentException;
use IteratorAggregate;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -181,17 +180,17 @@ public function testStringMethod()
'empty_str' => '',
'null' => null,
]);
$this->assertTrue($fluent->string('int') instanceof Stringable);
$this->assertTrue($fluent->string('unknown_key') instanceof Stringable);
$this->assertSame('123', $fluent->string('int')->value());
$this->assertSame('456', $fluent->string('int_str')->value());
$this->assertSame('123.456', $fluent->string('float')->value());
$this->assertSame('123.456', $fluent->string('float_str')->value());
$this->assertSame('0', $fluent->string('float_zero')->value());
$this->assertSame('0.000', $fluent->string('float_str_zero')->value());
$this->assertSame('', $fluent->string('empty_str')->value());
$this->assertSame('', $fluent->string('null')->value());
$this->assertSame('', $fluent->string('unknown_key')->value());
$this->assertIsString($fluent->string('int'));
$this->assertIsString($fluent->string('unknown_key'));
$this->assertSame('123', $fluent->string('int'));
$this->assertSame('456', $fluent->string('int_str'));
$this->assertSame('123.456', $fluent->string('float'));
$this->assertSame('123.456', $fluent->string('float_str'));
$this->assertSame('0', $fluent->string('float_zero'));
$this->assertSame('0.000', $fluent->string('float_str_zero'));
$this->assertSame('', $fluent->string('empty_str'));
$this->assertSame('', $fluent->string('null'));
$this->assertSame('', $fluent->string('unknown_key'));
}

public function testBooleanMethod()
Expand Down
24 changes: 12 additions & 12 deletions tests/Support/ValidatedInputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -352,18 +352,18 @@ public function test_string_method()
'null' => null,
]);

$this->assertTrue($input->string('int') instanceof Stringable);
$this->assertTrue($input->string('int') instanceof Stringable);
$this->assertTrue($input->string('unknown_key') instanceof Stringable);
$this->assertSame('123', $input->string('int')->value());
$this->assertSame('456', $input->string('int_str')->value());
$this->assertSame('123.456', $input->string('float')->value());
$this->assertSame('123.456', $input->string('float_str')->value());
$this->assertSame('0', $input->string('float_zero')->value());
$this->assertSame('0.000', $input->string('float_str_zero')->value());
$this->assertSame('', $input->string('empty_str')->value());
$this->assertSame('', $input->string('null')->value());
$this->assertSame('', $input->string('unknown_key')->value());
$this->assertIsString($input->string('int'));
$this->assertIsString($input->string('int'));
$this->assertIsString($input->string('unknown_key'));
$this->assertSame('123', $input->string('int'));
$this->assertSame('456', $input->string('int_str'));
$this->assertSame('123.456', $input->string('float'));
$this->assertSame('123.456', $input->string('float_str'));
$this->assertSame('0', $input->string('float_zero'));
$this->assertSame('0.000', $input->string('float_str_zero'));
$this->assertSame('', $input->string('empty_str'));
$this->assertSame('', $input->string('null'));
$this->assertSame('', $input->string('unknown_key'));
}

public function test_boolean_method()
Expand Down
10 changes: 5 additions & 5 deletions tests/View/ViewComponentAttributeBagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -346,11 +346,11 @@ public function testString()
'number' => 123,
]);

$this->assertInstanceOf(\Illuminate\Support\Stringable::class, $bag->string('name'));
$this->assertEquals('test', (string) $bag->string('name'));
$this->assertEquals('', (string) $bag->string('empty'));
$this->assertEquals('123', (string) $bag->string('number'));
$this->assertEquals('default', (string) $bag->string('missing', 'default'));
$this->assertIsString($bag->string('name'));
$this->assertEquals('test', $bag->string('name'));
$this->assertEquals('', $bag->string('empty'));
$this->assertEquals('123', $bag->string('number'));
$this->assertEquals('default', $bag->string('missing', 'default'));
}

public function testBoolean()
Expand Down