From 8ea50bc1b7bab7507ca5b9d3761c45cee450b9f0 Mon Sep 17 00:00:00 2001 From: Steve Bauman Date: Mon, 19 Sep 2022 10:27:19 -0400 Subject: [PATCH] [9.x] Add missing `Str::wrap()` static method (#44207) * Move wrap to Str static method * Add tests --- src/Illuminate/Support/Str.php | 12 ++++++++++++ src/Illuminate/Support/Stringable.php | 2 +- tests/Support/SupportStrTest.php | 6 ++++++ 3 files changed, 19 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)); } /** 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('/', '/'));