Skip to content

Commit b6c9169

Browse files
authored
[11.x] Add $ignoreCase option to Str::is (#53981)
* Add ability to ignore case using Str::is * Add tests * Add param doc * CS fix
1 parent 7d0a3d3 commit b6c9169

File tree

4 files changed

+28
-4
lines changed

4 files changed

+28
-4
lines changed

src/Illuminate/Support/Str.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -477,9 +477,10 @@ public static function unwrap($value, $before, $after = null)
477477
*
478478
* @param string|iterable<string> $pattern
479479
* @param string $value
480+
* @param bool $ignoreCase
480481
* @return bool
481482
*/
482-
public static function is($pattern, $value)
483+
public static function is($pattern, $value, $ignoreCase = false)
483484
{
484485
$value = (string) $value;
485486

@@ -497,14 +498,18 @@ public static function is($pattern, $value)
497498
return true;
498499
}
499500

501+
if ($ignoreCase && mb_strtolower($pattern) === mb_strtolower($value)) {
502+
return true;
503+
}
504+
500505
$pattern = preg_quote($pattern, '#');
501506

502507
// Asterisks are translated into zero-or-more regular expression wildcards
503508
// to make it convenient to check if the strings starts with the given
504509
// pattern such as "library/*", making any string check convenient.
505510
$pattern = str_replace('\*', '.*', $pattern);
506511

507-
if (preg_match('#^'.$pattern.'\z#u', $value) === 1) {
512+
if (preg_match('#^'.$pattern.'\z#'.($ignoreCase ? 'iu' : 'u'), $value) === 1) {
508513
return true;
509514
}
510515
}

src/Illuminate/Support/Stringable.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,11 +341,12 @@ public function finish($cap)
341341
* Determine if a given string matches a given pattern.
342342
*
343343
* @param string|iterable<string> $pattern
344+
* @param bool $ignoreCase
344345
* @return bool
345346
*/
346-
public function is($pattern)
347+
public function is($pattern, $ignoreCase = false)
347348
{
348-
return Str::is($pattern, $this->value);
349+
return Str::is($pattern, $this->value, $ignoreCase);
349350
}
350351

351352
/**

tests/Support/SupportStrTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,15 @@ public function testIs()
507507
$this->assertFalse(Str::is('*FOO*', 'foo/bar/baz'));
508508
$this->assertFalse(Str::is('A', 'a'));
509509

510+
// is not case sensitive
511+
$this->assertTrue(Str::is('A', 'a', true));
512+
$this->assertTrue(Str::is('*BAZ*', 'foo/bar/baz', true));
513+
$this->assertTrue(Str::is(['A*', 'B*'], 'a/', true));
514+
$this->assertFalse(Str::is(['A*', 'B*'], 'f/', true));
515+
$this->assertTrue(Str::is('FOO', 'foo', true));
516+
$this->assertTrue(Str::is('*FOO*', 'foo/bar/baz', true));
517+
$this->assertTrue(Str::is('foo/*', 'FOO/bar', true));
518+
510519
// Accepts array of patterns
511520
$this->assertTrue(Str::is(['a*', 'b*'], 'a/'));
512521
$this->assertTrue(Str::is(['a*', 'b*'], 'b/'));

tests/Support/SupportStringableTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -789,6 +789,15 @@ public function testIs()
789789
$this->assertFalse($this->stringable('foo/bar/baz')->is('*FOO*'));
790790
$this->assertFalse($this->stringable('a')->is('A'));
791791

792+
// is not case sensitive
793+
$this->assertTrue($this->stringable('a')->is('A', true));
794+
$this->assertTrue($this->stringable('foo/bar/baz')->is('*BAZ*', true));
795+
$this->assertTrue($this->stringable('a/')->is(['A*', 'B*'], true));
796+
$this->assertFalse($this->stringable('f/')->is(['A*', 'B*'], true));
797+
$this->assertTrue($this->stringable('foo')->is('FOO', true));
798+
$this->assertTrue($this->stringable('foo/bar/baz')->is('*FOO*', true));
799+
$this->assertTrue($this->stringable('FOO/bar')->is('foo/*', true));
800+
792801
// Accepts array of patterns
793802
$this->assertTrue($this->stringable('a/')->is(['a*', 'b*']));
794803
$this->assertTrue($this->stringable('b/')->is(['a*', 'b*']));

0 commit comments

Comments
 (0)