From cb9983c3a56e65b967cd4ff59ed75cc2a6949f9c Mon Sep 17 00:00:00 2001 From: Steve Bauman Date: Mon, 22 Jan 2024 11:23:01 -0500 Subject: [PATCH] [10.x] Add `Str::unwrap` (#49779) * Add Str::unwrap * Add "unwrap" to stringable * Add more tests --- src/Illuminate/Support/Str.php | 21 +++++++++++++++++++++ src/Illuminate/Support/Stringable.php | 12 ++++++++++++ tests/Support/SupportStrTest.php | 9 +++++++++ tests/Support/SupportStringableTest.php | 7 +++++++ 4 files changed, 49 insertions(+) diff --git a/src/Illuminate/Support/Str.php b/src/Illuminate/Support/Str.php index 4d51462f6d65..fc444ba699c7 100644 --- a/src/Illuminate/Support/Str.php +++ b/src/Illuminate/Support/Str.php @@ -390,6 +390,27 @@ public static function wrap($value, $before, $after = null) return $before.$value.($after ??= $before); } + /** + * Unwrap the string with the given strings. + * + * @param string $value + * @param string $before + * @param string|null $after + * @return string + */ + public static function unwrap($value, $before, $after = null) + { + if (static::startsWith($value, $before)) { + $value = static::substr($value, static::length($before)); + } + + if (static::endsWith($value, $after ??= $before)) { + $value = static::substr($value, 0, -static::length($after)); + } + + return $value; + } + /** * Determine if a given string matches a given pattern. * diff --git a/src/Illuminate/Support/Stringable.php b/src/Illuminate/Support/Stringable.php index 144cbb8c5537..cc1157b5d496 100644 --- a/src/Illuminate/Support/Stringable.php +++ b/src/Illuminate/Support/Stringable.php @@ -1224,6 +1224,18 @@ public function wrap($before, $after = null) return new static(Str::wrap($this->value, $before, $after)); } + /** + * Unwrap the string with the given strings. + * + * @param string $before + * @param string|null $after + * @return static + */ + public function unwrap($before, $after = null) + { + return new static(Str::unwrap($this->value, $before, $after)); + } + /** * Convert the string into a `HtmlString` instance. * diff --git a/tests/Support/SupportStrTest.php b/tests/Support/SupportStrTest.php index fbf5eeff6d0d..af0523b935fd 100755 --- a/tests/Support/SupportStrTest.php +++ b/tests/Support/SupportStrTest.php @@ -425,6 +425,15 @@ public function testWrap() $this->assertEquals('foo-bar-baz', Str::wrap('-bar-', 'foo', 'baz')); } + public function testUnwrap() + { + $this->assertEquals('value', Str::unwrap('"value"', '"')); + $this->assertEquals('value', Str::unwrap('"value', '"')); + $this->assertEquals('value', Str::unwrap('value"', '"')); + $this->assertEquals('bar', Str::unwrap('foo-bar-baz', 'foo-', '-baz')); + $this->assertEquals('some: "json"', Str::unwrap('{some: "json"}', '{', '}')); + } + public function testIs() { $this->assertTrue(Str::is('/', '/')); diff --git a/tests/Support/SupportStringableTest.php b/tests/Support/SupportStringableTest.php index 5cf433fa4973..5192a62cc002 100644 --- a/tests/Support/SupportStringableTest.php +++ b/tests/Support/SupportStringableTest.php @@ -1177,6 +1177,13 @@ public function testWrap() $this->assertEquals('"value"', $this->stringable('value')->wrap('"')); } + public function testUnwrap() + { + $this->assertEquals('value', $this->stringable('"value"')->unwrap('"')); + $this->assertEquals('bar', $this->stringable('foo-bar-baz')->unwrap('foo-', '-baz')); + $this->assertEquals('some: "json"', $this->stringable('{some: "json"}')->unwrap('{', '}')); + } + public function testToHtmlString() { $this->assertEquals(