Skip to content

Commit 34418f3

Browse files
[8.x] Add new Stringable::test method (#36462)
* feature: add new Stringable::matches method * tests: new Stringable::matches method * refactor: use match instead of matchAll * chore: rename method from matches to test
1 parent 6cbe0eb commit 34418f3

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

src/Illuminate/Support/Stringable.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,17 @@ public function matchAll($pattern)
365365
return collect($matches[1] ?? $matches[0]);
366366
}
367367

368+
/**
369+
* Determine if the string matches the given pattern.
370+
*
371+
* @param string $pattern
372+
* @return bool
373+
*/
374+
public function test($pattern)
375+
{
376+
return $this->match($pattern)->isNotEmpty();
377+
}
378+
368379
/**
369380
* Pad both sides of the string with another.
370381
*

tests/Support/SupportStringableTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,14 @@ public function testMatch()
6262
$this->assertTrue($stringable->matchAll('/nothing/')->isEmpty());
6363
}
6464

65+
public function testTest()
66+
{
67+
$stringable = $this->stringable('foo bar');
68+
69+
$this->assertTrue($stringable->test('/bar/'));
70+
$this->assertTrue($stringable->test('/foo (.*)/'));
71+
}
72+
6573
public function testTrim()
6674
{
6775
$this->assertSame('foo', (string) $this->stringable(' foo ')->trim());

0 commit comments

Comments
 (0)