Skip to content

Commit

Permalink
[7.x] Added new Stringable::leftTrim() and Stringable::rightTrim() me…
Browse files Browse the repository at this point in the history
…thods (#32288)

* Added new Stringable::leftTrim() and Stringable::rightTrim() methods

* Updated method names to match PHP functions and updated tests to pass
  • Loading branch information
ryangjchandler committed Apr 8, 2020
1 parent 8a7039e commit 4dd5501
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/Illuminate/Support/Stringable.php
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,28 @@ public function trim($characters = null)
return new static(trim(...array_merge([$this->value], func_get_args())));
}

/**
* Left trim the string of the given characters.
*
* @param string $characters
* @return static
*/
public function ltrim($characters = null)
{
return new static(ltrim(...array_merge([$this->value], func_get_args())));
}

/**
* Right trim the string of the given characters.
*
* @param string $characters
* @return static
*/
public function rtrim($characters = null)
{
return new static(rtrim(...array_merge([$this->value], func_get_args())));
}

/**
* Make a string's first character uppercase.
*
Expand Down
10 changes: 10 additions & 0 deletions tests/Support/SupportStringableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ public function testTrim()
$this->assertSame('foo', (string) $this->stringable(' foo ')->trim());
}

public function testLtrim()
{
$this->assertSame('foo ', (string) $this->stringable(' foo ')->ltrim());
}

public function testRtrim()
{
$this->assertSame(' foo', (string) $this->stringable(' foo ')->rtrim());
}

public function testCanBeLimitedByWords()
{
$this->assertSame('Taylor...', (string) $this->stringable('Taylor Otwell')->words(1));
Expand Down

0 comments on commit 4dd5501

Please sign in to comment.