Skip to content

Commit

Permalink
[9.x] Add new wrap string helper (#41455)
Browse files Browse the repository at this point in the history
* Added wrap string helper

* CS

* formatting

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
hebinet and taylorotwell committed Mar 13, 2022
1 parent 0a503ed commit 1355f4f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/Illuminate/Support/Stringable.php
Expand Up @@ -992,6 +992,18 @@ public function wordCount()
return str_word_count($this->value);
}

/**
* Wrap the string with the given strings.
*
* @param string $before
* @param string|null $after
* @return static
*/
public function wrap($before, $after = null)
{
return new static($before.$this->value.($after ??= $before));
}

/**
* Convert the string into a `HtmlString` instance.
*
Expand Down
6 changes: 6 additions & 0 deletions tests/Support/SupportStringableTest.php
Expand Up @@ -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(
Expand Down

0 comments on commit 1355f4f

Please sign in to comment.