Skip to content

Commit

Permalink
[9.x] Add missing Str::wrap() static method (#44207)
Browse files Browse the repository at this point in the history
* Move wrap to Str static method

* Add tests
  • Loading branch information
stevebauman committed Sep 19, 2022
1 parent 3bbc0e7 commit 8ea50bc
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/Illuminate/Support/Str.php
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
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
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

0 comments on commit 8ea50bc

Please sign in to comment.