diff --git a/src/Str.php b/src/Str.php index ec00505..c5a3b56 100644 --- a/src/Str.php +++ b/src/Str.php @@ -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; @@ -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; } } diff --git a/src/Stringable.php b/src/Stringable.php index c3dccbb..5ac2f5f 100644 --- a/src/Stringable.php +++ b/src/Stringable.php @@ -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); } /** @@ -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) diff --git a/tests/StrTest.php b/tests/StrTest.php index 70b01ba..ddad518 100644 --- a/tests/StrTest.php +++ b/tests/StrTest.php @@ -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() diff --git a/tests/StringableTest.php b/tests/StringableTest.php index e47da5e..a81b46c 100644 --- a/tests/StringableTest.php +++ b/tests/StringableTest.php @@ -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());