From 1355f4f0df9b26b710986a6a346e37de2cd0647c Mon Sep 17 00:00:00 2001 From: Markus Hebenstreit Date: Sun, 13 Mar 2022 15:56:16 +0100 Subject: [PATCH] [9.x] Add new wrap string helper (#41455) * Added wrap string helper * CS * formatting Co-authored-by: Taylor Otwell --- src/Illuminate/Support/Stringable.php | 12 ++++++++++++ tests/Support/SupportStringableTest.php | 6 ++++++ 2 files changed, 18 insertions(+) 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(