Skip to content

Commit

Permalink
[11.x] Add $ignoreCase option to Str::is (#53981)
Browse files Browse the repository at this point in the history
* Add ability to ignore case using Str::is

* Add tests

* Add param doc

* CS fix
  • Loading branch information
stevebauman authored Dec 19, 2024
1 parent 7d0a3d3 commit b6c9169
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
9 changes: 7 additions & 2 deletions src/Illuminate/Support/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -477,9 +477,10 @@ public static function unwrap($value, $before, $after = null)
*
* @param string|iterable<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 @@ -497,14 +498,18 @@ public static function is($pattern, $value)
return true;
}

if ($ignoreCase && mb_strtolower($pattern) === mb_strtolower($value)) {
return true;
}

$pattern = preg_quote($pattern, '#');

// Asterisks are translated into zero-or-more regular expression wildcards
// to make it convenient to check if the strings starts with the given
// 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
5 changes: 3 additions & 2 deletions src/Illuminate/Support/Stringable.php
Original file line number Diff line number Diff line change
Expand Up @@ -341,11 +341,12 @@ public function finish($cap)
* Determine if a given string matches a given pattern.
*
* @param string|iterable<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
9 changes: 9 additions & 0 deletions tests/Support/SupportStrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,15 @@ public function testIs()
$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/'));
Expand Down
9 changes: 9 additions & 0 deletions tests/Support/SupportStringableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,15 @@ public function testIs()
$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*']));
Expand Down

0 comments on commit b6c9169

Please sign in to comment.