Skip to content

[12.x] Introducing toUri to the Stringable Class #55862

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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
10 changes: 10 additions & 0 deletions src/Illuminate/Support/Stringable.php
Original file line number Diff line number Diff line change
Expand Up @@ -1430,6 +1430,16 @@ public function toDate($format = null, $tz = null)
return Date::createFromFormat($format, $this->value, $tz);
}

/**
* Get the underlying string value as a Uri instance.
*
* @return \Illuminate\Support\Uri
*/
public function toUri()
{
return Uri::of($this->value);
}

/**
* Convert the object to a string when JSON encoded.
*
Expand Down
12 changes: 12 additions & 0 deletions tests/Support/SupportStringableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Support\Collection;
use Illuminate\Support\HtmlString;
use Illuminate\Support\Stringable;
use Illuminate\Support\Uri;
use League\CommonMark\Environment\EnvironmentBuilderInterface;
use League\CommonMark\Extension\ExtensionInterface;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -1397,6 +1398,17 @@ public function testToDateThrowsException()
$this->stringable('not a date')->toDate();
}

public function testToUri()
{
$sentence = 'Laravel is a PHP framework. You can access the docs in: {https://laravel.com/docs}';

$uri = $this->stringable($sentence)->between('{', '}')->toUri();

$this->assertInstanceOf(Uri::class, $uri);
$this->assertSame('https://laravel.com/docs', (string) $uri);
$this->assertSame('https://laravel.com/docs', $uri->toHtml());
}

public function testArrayAccess()
{
$str = $this->stringable('my string');
Expand Down