diff --git a/src/Illuminate/Support/Stringable.php b/src/Illuminate/Support/Stringable.php index 90177d5b258b..2a54910c75fb 100644 --- a/src/Illuminate/Support/Stringable.php +++ b/src/Illuminate/Support/Stringable.php @@ -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. * diff --git a/tests/Support/SupportStringableTest.php b/tests/Support/SupportStringableTest.php index 05b4f178cc04..d94be3ac61dc 100644 --- a/tests/Support/SupportStringableTest.php +++ b/tests/Support/SupportStringableTest.php @@ -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(