From b6c91698cc6762249b53dcf101997bcd6b620e94 Mon Sep 17 00:00:00 2001 From: Steve Bauman Date: Thu, 19 Dec 2024 13:03:05 -0500 Subject: [PATCH] [11.x] Add `$ignoreCase` option to `Str::is` (#53981) * Add ability to ignore case using Str::is * Add tests * Add param doc * CS fix --- src/Illuminate/Support/Str.php | 9 +++++++-- src/Illuminate/Support/Stringable.php | 5 +++-- tests/Support/SupportStrTest.php | 9 +++++++++ tests/Support/SupportStringableTest.php | 9 +++++++++ 4 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/Illuminate/Support/Str.php b/src/Illuminate/Support/Str.php index aefa94aeb98c..af8f0ea6568b 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 4723942a9769..bfaf00c4ba87 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 ae6e0febefbd..f00c5e3d3dcf 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 4d8b8735b920..c352d88db434 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*']));