Skip to content

Commit 2d7c499

Browse files
[12.x] Introducing toUri to the Stringable Class (#55862)
* Feat: Add Stringable toUri method * Tests
1 parent f9ffdc1 commit 2d7c499

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

src/Illuminate/Support/Stringable.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1430,6 +1430,16 @@ public function toDate($format = null, $tz = null)
14301430
return Date::createFromFormat($format, $this->value, $tz);
14311431
}
14321432

1433+
/**
1434+
* Get the underlying string value as a Uri instance.
1435+
*
1436+
* @return \Illuminate\Support\Uri
1437+
*/
1438+
public function toUri()
1439+
{
1440+
return Uri::of($this->value);
1441+
}
1442+
14331443
/**
14341444
* Convert the object to a string when JSON encoded.
14351445
*

tests/Support/SupportStringableTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Support\Collection;
77
use Illuminate\Support\HtmlString;
88
use Illuminate\Support\Stringable;
9+
use Illuminate\Support\Uri;
910
use League\CommonMark\Environment\EnvironmentBuilderInterface;
1011
use League\CommonMark\Extension\ExtensionInterface;
1112
use PHPUnit\Framework\TestCase;
@@ -1397,6 +1398,17 @@ public function testToDateThrowsException()
13971398
$this->stringable('not a date')->toDate();
13981399
}
13991400

1401+
public function testToUri()
1402+
{
1403+
$sentence = 'Laravel is a PHP framework. You can access the docs in: {https://laravel.com/docs}';
1404+
1405+
$uri = $this->stringable($sentence)->between('{', '}')->toUri();
1406+
1407+
$this->assertInstanceOf(Uri::class, $uri);
1408+
$this->assertSame('https://laravel.com/docs', (string) $uri);
1409+
$this->assertSame('https://laravel.com/docs', $uri->toHtml());
1410+
}
1411+
14001412
public function testArrayAccess()
14011413
{
14021414
$str = $this->stringable('my string');

0 commit comments

Comments
 (0)