From d9e2b85852e19a16c0cb41fd8254debdf9a6aff7 Mon Sep 17 00:00:00 2001 From: Markus Hebenstreit Date: Sat, 12 Mar 2022 10:55:16 +0100 Subject: [PATCH 1/3] Added wrap string helper --- src/Illuminate/Support/Str.php | 17 +++++++++++++++++ src/Illuminate/Support/Stringable.php | 12 ++++++++++++ tests/Support/SupportStrTest.php | 6 ++++++ tests/Support/SupportStringableTest.php | 6 ++++++ 4 files changed, 41 insertions(+) diff --git a/src/Illuminate/Support/Str.php b/src/Illuminate/Support/Str.php index a87afe9a2c4e..35fdee4ce41b 100644 --- a/src/Illuminate/Support/Str.php +++ b/src/Illuminate/Support/Str.php @@ -462,6 +462,23 @@ public static function words($value, $words = 100, $end = '...') return rtrim($matches[0]).$end; } + /** + * Wraps a string with the given wrappers. + * + * @param string $value + * @param string $before + * @param string|null $after + * @return string + */ + public static function wrap($value, $before, $after = null) + { + if ($after === null) { + $after = $before; + } + + return $before.$value.$after; + } + /** * Converts GitHub flavored Markdown into HTML. * diff --git a/src/Illuminate/Support/Stringable.php b/src/Illuminate/Support/Stringable.php index 90177d5b258b..38d6cf2f1414 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); } + /** + * Wraps a string with the given wrappers + * + * @param string $before + * @param string|null $after + * @return static + */ + public function wrap($before, $after = null) + { + return new static(Str::wrap($this->value, $before, $after)); + } + /** * Convert the string into a `HtmlString` instance. * diff --git a/tests/Support/SupportStrTest.php b/tests/Support/SupportStrTest.php index 36e167ba66b7..973b3c63090a 100755 --- a/tests/Support/SupportStrTest.php +++ b/tests/Support/SupportStrTest.php @@ -721,6 +721,12 @@ public function testWordCount() $this->assertEquals(10, Str::wordCount('Hi, this is my first contribution to the Laravel framework.')); } + public function testWrap() + { + $this->assertEquals('This is me!', Str::wrap('is', 'This ', ' me!')); + $this->assertEquals('"value"', Str::wrap('value', '"')); + } + public function validUuidList() { return [ 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( From 1cdeb32432f2b60f563b35f4d793d681a660687d Mon Sep 17 00:00:00 2001 From: Markus Hebenstreit Date: Sat, 12 Mar 2022 17:54:28 +0100 Subject: [PATCH 2/3] CS --- src/Illuminate/Support/Stringable.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Support/Stringable.php b/src/Illuminate/Support/Stringable.php index 38d6cf2f1414..401fa7f88190 100644 --- a/src/Illuminate/Support/Stringable.php +++ b/src/Illuminate/Support/Stringable.php @@ -993,9 +993,9 @@ public function wordCount() } /** - * Wraps a string with the given wrappers + * Wraps a string with the given wrappers. * - * @param string $before + * @param string $before * @param string|null $after * @return static */ From d6a72a076872735df66fe9973b96506982311767 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Sun, 13 Mar 2022 09:51:23 -0500 Subject: [PATCH 3/3] formatting --- src/Illuminate/Support/Str.php | 17 ----------------- src/Illuminate/Support/Stringable.php | 4 ++-- tests/Support/SupportStrTest.php | 6 ------ 3 files changed, 2 insertions(+), 25 deletions(-) diff --git a/src/Illuminate/Support/Str.php b/src/Illuminate/Support/Str.php index 35fdee4ce41b..a87afe9a2c4e 100644 --- a/src/Illuminate/Support/Str.php +++ b/src/Illuminate/Support/Str.php @@ -462,23 +462,6 @@ public static function words($value, $words = 100, $end = '...') return rtrim($matches[0]).$end; } - /** - * Wraps a string with the given wrappers. - * - * @param string $value - * @param string $before - * @param string|null $after - * @return string - */ - public static function wrap($value, $before, $after = null) - { - if ($after === null) { - $after = $before; - } - - return $before.$value.$after; - } - /** * Converts GitHub flavored Markdown into HTML. * diff --git a/src/Illuminate/Support/Stringable.php b/src/Illuminate/Support/Stringable.php index 401fa7f88190..2a54910c75fb 100644 --- a/src/Illuminate/Support/Stringable.php +++ b/src/Illuminate/Support/Stringable.php @@ -993,7 +993,7 @@ public function wordCount() } /** - * Wraps a string with the given wrappers. + * Wrap the string with the given strings. * * @param string $before * @param string|null $after @@ -1001,7 +1001,7 @@ public function wordCount() */ public function wrap($before, $after = null) { - return new static(Str::wrap($this->value, $before, $after)); + return new static($before.$this->value.($after ??= $before)); } /** diff --git a/tests/Support/SupportStrTest.php b/tests/Support/SupportStrTest.php index 973b3c63090a..36e167ba66b7 100755 --- a/tests/Support/SupportStrTest.php +++ b/tests/Support/SupportStrTest.php @@ -721,12 +721,6 @@ public function testWordCount() $this->assertEquals(10, Str::wordCount('Hi, this is my first contribution to the Laravel framework.')); } - public function testWrap() - { - $this->assertEquals('This is me!', Str::wrap('is', 'This ', ' me!')); - $this->assertEquals('"value"', Str::wrap('value', '"')); - } - public function validUuidList() { return [