diff --git a/src/Illuminate/Support/Str.php b/src/Illuminate/Support/Str.php index aefa94aeb98..af8f0ea6568 100644 --- a/src/Illuminate/Support/Str.php +++ b/src/Illuminate/Support/Str.php @@ -477,9 +477,10 @@ public static function unwrap($value, $before, $after = null) * * @param string|iterable $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; @@ -497,6 +498,10 @@ 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 @@ -504,7 +509,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; } } diff --git a/src/Illuminate/Support/Stringable.php b/src/Illuminate/Support/Stringable.php index 4723942a976..bfaf00c4ba8 100644 --- a/src/Illuminate/Support/Stringable.php +++ b/src/Illuminate/Support/Stringable.php @@ -341,11 +341,12 @@ public function finish($cap) * Determine if a given string matches a given pattern. * * @param string|iterable $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); } /** diff --git a/tests/Support/SupportStrTest.php b/tests/Support/SupportStrTest.php index ae6e0febefb..f00c5e3d3dc 100755 --- a/tests/Support/SupportStrTest.php +++ b/tests/Support/SupportStrTest.php @@ -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/')); diff --git a/tests/Support/SupportStringableTest.php b/tests/Support/SupportStringableTest.php index 4d8b8735b92..c352d88db43 100644 --- a/tests/Support/SupportStringableTest.php +++ b/tests/Support/SupportStringableTest.php @@ -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*']));