Skip to content

Commit

Permalink
[8.x] Add new Stringable::test method (#36462)
Browse files Browse the repository at this point in the history
* feature: add new Stringable::matches method

* tests: new Stringable::matches method

* refactor: use match instead of matchAll

* chore: rename method from matches to test
  • Loading branch information
ryangjchandler committed Mar 4, 2021
1 parent 6cbe0eb commit 34418f3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/Illuminate/Support/Stringable.php
Expand Up @@ -365,6 +365,17 @@ public function matchAll($pattern)
return collect($matches[1] ?? $matches[0]);
}

/**
* Determine if the string matches the given pattern.
*
* @param string $pattern
* @return bool
*/
public function test($pattern)
{
return $this->match($pattern)->isNotEmpty();
}

/**
* Pad both sides of the string with another.
*
Expand Down
8 changes: 8 additions & 0 deletions tests/Support/SupportStringableTest.php
Expand Up @@ -62,6 +62,14 @@ public function testMatch()
$this->assertTrue($stringable->matchAll('/nothing/')->isEmpty());
}

public function testTest()
{
$stringable = $this->stringable('foo bar');

$this->assertTrue($stringable->test('/bar/'));
$this->assertTrue($stringable->test('/foo (.*)/'));
}

public function testTrim()
{
$this->assertSame('foo', (string) $this->stringable(' foo ')->trim());
Expand Down

0 comments on commit 34418f3

Please sign in to comment.