Skip to content
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

[9.x] Add new wrap string helper #41455

Merged
merged 3 commits into from Mar 13, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/Illuminate/Support/Stringable.php
Expand Up @@ -992,6 +992,18 @@ public function wordCount()
return str_word_count($this->value);
}

/**
* Wrap the string with the given strings.
*
* @param string $before
* @param string|null $after
* @return static
*/
public function wrap($before, $after = null)
{
return new static($before.$this->value.($after ??= $before));
}

/**
* Convert the string into a `HtmlString` instance.
*
Expand Down
6 changes: 6 additions & 0 deletions tests/Support/SupportStringableTest.php
Expand Up @@ -941,6 +941,12 @@ public function testWordCount()
$this->assertEquals(10, $this->stringable('Hi, this is my first contribution to the Laravel framework.')->wordCount());
}

public function testWrap()
{
$this->assertEquals('This is me!', $this->stringable('is')->wrap('This ', ' me!'));
$this->assertEquals('"value"', $this->stringable('value')->wrap('"'));
}

public function testToHtmlString()
{
$this->assertEquals(
Expand Down