From 8a1991b34833624d9cc1f27afdbd8d17ac107bf3 Mon Sep 17 00:00:00 2001 From: Steve Bauman Date: Mon, 19 Sep 2022 10:11:29 -0400 Subject: [PATCH 1/2] Move wrap to Str static method --- src/Illuminate/Support/Str.php | 12 ++++++++++++ src/Illuminate/Support/Stringable.php | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Support/Str.php b/src/Illuminate/Support/Str.php index 58ee38525f28..18454ecbe4c2 100644 --- a/src/Illuminate/Support/Str.php +++ b/src/Illuminate/Support/Str.php @@ -338,6 +338,18 @@ public static function finish($value, $cap) return preg_replace('/(?:'.$quoted.')+$/u', '', $value).$cap; } + /** + * Wrap the string with the given strings. + * + * @param string $before + * @param string|null $after + * @return string + */ + public static function wrap($value, $before, $after = null) + { + return $before.$value.($after ??= $before); + } + /** * Determine if a given string matches a given pattern. * diff --git a/src/Illuminate/Support/Stringable.php b/src/Illuminate/Support/Stringable.php index 5ebf92e52724..a5dd34c93212 100644 --- a/src/Illuminate/Support/Stringable.php +++ b/src/Illuminate/Support/Stringable.php @@ -1060,7 +1060,7 @@ public function wordCount() */ public function wrap($before, $after = null) { - return new static($before.$this->value.($after ??= $before)); + return new static(Str::wrap($this->value, $before, $after)); } /** From 588a6c9f46f0f1c35e2b9e11ea80728831b770d9 Mon Sep 17 00:00:00 2001 From: Steve Bauman Date: Mon, 19 Sep 2022 10:11:33 -0400 Subject: [PATCH 2/2] Add tests --- tests/Support/SupportStrTest.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/Support/SupportStrTest.php b/tests/Support/SupportStrTest.php index 1984ec5858b7..f443bb700827 100755 --- a/tests/Support/SupportStrTest.php +++ b/tests/Support/SupportStrTest.php @@ -352,6 +352,12 @@ public function testFinish() $this->assertSame('abcbbc', Str::finish('abcbbcbc', 'bc')); } + public function testWrap() + { + $this->assertEquals('"value"', Str::wrap('value', '"')); + $this->assertEquals('foo-bar-baz', Str::wrap('-bar-', 'foo', 'baz')); + } + public function testIs() { $this->assertTrue(Str::is('/', '/'));