Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.x] Add missing Str::wrap() static method #44207

Merged
merged 2 commits into from
Sep 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/Illuminate/Support/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Support/Stringable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

/**
Expand Down
6 changes: 6 additions & 0 deletions tests/Support/SupportStrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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('/', '/'));
Expand Down