Skip to content

Commit 1355f4f

Browse files
[9.x] Add new wrap string helper (#41455)
* Added wrap string helper * CS * formatting Co-authored-by: Taylor Otwell <[email protected]>
1 parent 0a503ed commit 1355f4f

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/Illuminate/Support/Stringable.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -992,6 +992,18 @@ public function wordCount()
992992
return str_word_count($this->value);
993993
}
994994

995+
/**
996+
* Wrap the string with the given strings.
997+
*
998+
* @param string $before
999+
* @param string|null $after
1000+
* @return static
1001+
*/
1002+
public function wrap($before, $after = null)
1003+
{
1004+
return new static($before.$this->value.($after ??= $before));
1005+
}
1006+
9951007
/**
9961008
* Convert the string into a `HtmlString` instance.
9971009
*

tests/Support/SupportStringableTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -941,6 +941,12 @@ public function testWordCount()
941941
$this->assertEquals(10, $this->stringable('Hi, this is my first contribution to the Laravel framework.')->wordCount());
942942
}
943943

944+
public function testWrap()
945+
{
946+
$this->assertEquals('This is me!', $this->stringable('is')->wrap('This ', ' me!'));
947+
$this->assertEquals('"value"', $this->stringable('value')->wrap('"'));
948+
}
949+
944950
public function testToHtmlString()
945951
{
946952
$this->assertEquals(

0 commit comments

Comments
 (0)