Skip to content

Commit

Permalink
Add case sensitivity option to pattern matching (#7219)
Browse files Browse the repository at this point in the history
  • Loading branch information
huangdijia authored Jan 8, 2025
1 parent 5467fc8 commit 89ab60e
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 6 deletions.
5 changes: 3 additions & 2 deletions src/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -360,9 +360,10 @@ public static function finish($value, $cap)
*
* @param array|string $pattern
* @param string $value
* @param bool $ignoreCase
* @return bool
*/
public static function is($pattern, $value)
public static function is($pattern, $value, $ignoreCase = false)
{
$value = (string) $value;

Expand All @@ -389,7 +390,7 @@ public static function is($pattern, $value)
// pattern such as "library/*", making any string check convenient.
$pattern = str_replace('\*', '.*', $pattern);

if (preg_match('#^' . $pattern . '\z#u', $value) === 1) {
if (preg_match('#^' . $pattern . '\z#' . ($ignoreCase ? 'iu' : 'u'), $value) === 1) {
return true;
}
}
Expand Down
9 changes: 5 additions & 4 deletions src/Stringable.php
Original file line number Diff line number Diff line change
Expand Up @@ -321,11 +321,12 @@ public function finish($cap)
* Determine if a given string matches a given pattern.
*
* @param string|string[] $pattern
* @param bool $ignoreCase
* @return bool
*/
public function is($pattern)
public function is($pattern, $ignoreCase = false)
{
return Str::is($pattern, $this->value);
return Str::is($pattern, $this->value, $ignoreCase);
}

/**
Expand Down Expand Up @@ -1070,9 +1071,9 @@ public function whenExactly($needles, $callback, $default = null)
return $this->when($this->exactly($needles), $callback, $default);
}

public function whenIs($pattern, $callback, $default = null)
public function whenIs($pattern, $callback, $default = null, $ignoreCase = false)
{
return $this->when($this->is($pattern), $callback, $default);
return $this->when($this->is($pattern, $ignoreCase), $callback, $default);
}

public function whenIsUlid($callback, $default = null)
Expand Down
41 changes: 41 additions & 0 deletions tests/StrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,47 @@ public function testIs()
$this->assertFalse(Str::is('', 0));
$this->assertFalse(Str::is([null], 0));
$this->assertTrue(Str::is([null], null));

$this->assertTrue(Str::is('/', '/'));
$this->assertFalse(Str::is('/', ' /'));
$this->assertFalse(Str::is('/', '/a'));
$this->assertTrue(Str::is('foo/*', 'foo/bar/baz'));

$this->assertTrue(Str::is('*@*', 'App\Class@method'));
$this->assertTrue(Str::is('*@*', 'app\Class@'));
$this->assertTrue(Str::is('*@*', '@method'));

// is case sensitive
$this->assertFalse(Str::is('*BAZ*', 'foo/bar/baz'));
$this->assertFalse(Str::is('*FOO*', 'foo/bar/baz'));
$this->assertFalse(Str::is('A', 'a'));

// is not case sensitive
$this->assertTrue(Str::is('A', 'a', true));
$this->assertTrue(Str::is('*BAZ*', 'foo/bar/baz', true));
$this->assertTrue(Str::is(['A*', 'B*'], 'a/', true));
$this->assertFalse(Str::is(['A*', 'B*'], 'f/', true));
$this->assertTrue(Str::is('FOO', 'foo', true));
$this->assertTrue(Str::is('*FOO*', 'foo/bar/baz', true));
$this->assertTrue(Str::is('foo/*', 'FOO/bar', true));

// Accepts array of patterns
$this->assertTrue(Str::is(['a*', 'b*'], 'a/'));
$this->assertTrue(Str::is(['a*', 'b*'], 'b/'));
$this->assertFalse(Str::is(['a*', 'b*'], 'f/'));

// numeric values and patterns
$this->assertFalse(Str::is(['a*', 'b*'], 123));
$this->assertTrue(Str::is(['*2*', 'b*'], 11211));

$this->assertTrue(Str::is('*/foo', 'blah/baz/foo'));

// empty patterns
$this->assertFalse(Str::is([], 'test'));

$this->assertFalse(Str::is('', 0));
$this->assertFalse(Str::is([null], 0));
$this->assertTrue(Str::is([null], null));
}

public function testCamel()
Expand Down
40 changes: 40 additions & 0 deletions tests/StringableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,46 @@ public function testExcerpt()
$this->assertSame('...is a beautiful morn...', (string) $this->stringable('This is a beautiful morning')->excerpt('beautiful', ['radius' => 5]));
}

public function testIs()
{
$this->assertTrue($this->stringable('/')->is('/'));
$this->assertFalse($this->stringable('/')->is(' /'));
$this->assertFalse($this->stringable('/a')->is('/'));
$this->assertTrue($this->stringable('foo/bar/baz')->is('foo/*'));

$this->assertTrue($this->stringable('App\Class@method')->is('*@*'));
$this->assertTrue($this->stringable('app\Class@')->is('*@*'));
$this->assertTrue($this->stringable('@method')->is('*@*'));

// is case sensitive
$this->assertFalse($this->stringable('foo/bar/baz')->is('*BAZ*'));
$this->assertFalse($this->stringable('foo/bar/baz')->is('*FOO*'));
$this->assertFalse($this->stringable('a')->is('A'));

// is not case sensitive
$this->assertTrue($this->stringable('a')->is('A', true));
$this->assertTrue($this->stringable('foo/bar/baz')->is('*BAZ*', true));
$this->assertTrue($this->stringable('a/')->is(['A*', 'B*'], true));
$this->assertFalse($this->stringable('f/')->is(['A*', 'B*'], true));
$this->assertTrue($this->stringable('foo')->is('FOO', true));
$this->assertTrue($this->stringable('foo/bar/baz')->is('*FOO*', true));
$this->assertTrue($this->stringable('FOO/bar')->is('foo/*', true));

// Accepts array of patterns
$this->assertTrue($this->stringable('a/')->is(['a*', 'b*']));
$this->assertTrue($this->stringable('b/')->is(['a*', 'b*']));
$this->assertFalse($this->stringable('f/')->is(['a*', 'b*']));

// numeric values and patterns
$this->assertFalse($this->stringable(123)->is(['a*', 'b*']));
$this->assertTrue($this->stringable(11211)->is(['*2*', 'b*']));

$this->assertTrue($this->stringable('blah/baz/foo')->is('*/foo'));

// empty patterns
$this->assertFalse($this->stringable('test')->is([]));
}

public function testIsAscii()
{
$this->assertTrue($this->stringable('Hello World!')->isAscii());
Expand Down

0 comments on commit 89ab60e

Please sign in to comment.