Skip to content

Commit 89ab60e

Browse files
authored
Add case sensitivity option to pattern matching (#7219)
1 parent 5467fc8 commit 89ab60e

File tree

4 files changed

+89
-6
lines changed

4 files changed

+89
-6
lines changed

src/Str.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,9 +360,10 @@ public static function finish($value, $cap)
360360
*
361361
* @param array|string $pattern
362362
* @param string $value
363+
* @param bool $ignoreCase
363364
* @return bool
364365
*/
365-
public static function is($pattern, $value)
366+
public static function is($pattern, $value, $ignoreCase = false)
366367
{
367368
$value = (string) $value;
368369

@@ -389,7 +390,7 @@ public static function is($pattern, $value)
389390
// pattern such as "library/*", making any string check convenient.
390391
$pattern = str_replace('\*', '.*', $pattern);
391392

392-
if (preg_match('#^' . $pattern . '\z#u', $value) === 1) {
393+
if (preg_match('#^' . $pattern . '\z#' . ($ignoreCase ? 'iu' : 'u'), $value) === 1) {
393394
return true;
394395
}
395396
}

src/Stringable.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -321,11 +321,12 @@ public function finish($cap)
321321
* Determine if a given string matches a given pattern.
322322
*
323323
* @param string|string[] $pattern
324+
* @param bool $ignoreCase
324325
* @return bool
325326
*/
326-
public function is($pattern)
327+
public function is($pattern, $ignoreCase = false)
327328
{
328-
return Str::is($pattern, $this->value);
329+
return Str::is($pattern, $this->value, $ignoreCase);
329330
}
330331

331332
/**
@@ -1070,9 +1071,9 @@ public function whenExactly($needles, $callback, $default = null)
10701071
return $this->when($this->exactly($needles), $callback, $default);
10711072
}
10721073

1073-
public function whenIs($pattern, $callback, $default = null)
1074+
public function whenIs($pattern, $callback, $default = null, $ignoreCase = false)
10741075
{
1075-
return $this->when($this->is($pattern), $callback, $default);
1076+
return $this->when($this->is($pattern, $ignoreCase), $callback, $default);
10761077
}
10771078

10781079
public function whenIsUlid($callback, $default = null)

tests/StrTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,47 @@ public function testIs()
340340
$this->assertFalse(Str::is('', 0));
341341
$this->assertFalse(Str::is([null], 0));
342342
$this->assertTrue(Str::is([null], null));
343+
344+
$this->assertTrue(Str::is('/', '/'));
345+
$this->assertFalse(Str::is('/', ' /'));
346+
$this->assertFalse(Str::is('/', '/a'));
347+
$this->assertTrue(Str::is('foo/*', 'foo/bar/baz'));
348+
349+
$this->assertTrue(Str::is('*@*', 'App\Class@method'));
350+
$this->assertTrue(Str::is('*@*', 'app\Class@'));
351+
$this->assertTrue(Str::is('*@*', '@method'));
352+
353+
// is case sensitive
354+
$this->assertFalse(Str::is('*BAZ*', 'foo/bar/baz'));
355+
$this->assertFalse(Str::is('*FOO*', 'foo/bar/baz'));
356+
$this->assertFalse(Str::is('A', 'a'));
357+
358+
// is not case sensitive
359+
$this->assertTrue(Str::is('A', 'a', true));
360+
$this->assertTrue(Str::is('*BAZ*', 'foo/bar/baz', true));
361+
$this->assertTrue(Str::is(['A*', 'B*'], 'a/', true));
362+
$this->assertFalse(Str::is(['A*', 'B*'], 'f/', true));
363+
$this->assertTrue(Str::is('FOO', 'foo', true));
364+
$this->assertTrue(Str::is('*FOO*', 'foo/bar/baz', true));
365+
$this->assertTrue(Str::is('foo/*', 'FOO/bar', true));
366+
367+
// Accepts array of patterns
368+
$this->assertTrue(Str::is(['a*', 'b*'], 'a/'));
369+
$this->assertTrue(Str::is(['a*', 'b*'], 'b/'));
370+
$this->assertFalse(Str::is(['a*', 'b*'], 'f/'));
371+
372+
// numeric values and patterns
373+
$this->assertFalse(Str::is(['a*', 'b*'], 123));
374+
$this->assertTrue(Str::is(['*2*', 'b*'], 11211));
375+
376+
$this->assertTrue(Str::is('*/foo', 'blah/baz/foo'));
377+
378+
// empty patterns
379+
$this->assertFalse(Str::is([], 'test'));
380+
381+
$this->assertFalse(Str::is('', 0));
382+
$this->assertFalse(Str::is([null], 0));
383+
$this->assertTrue(Str::is([null], null));
343384
}
344385

345386
public function testCamel()

tests/StringableTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,46 @@ public function testExcerpt()
183183
$this->assertSame('...is a beautiful morn...', (string) $this->stringable('This is a beautiful morning')->excerpt('beautiful', ['radius' => 5]));
184184
}
185185

186+
public function testIs()
187+
{
188+
$this->assertTrue($this->stringable('/')->is('/'));
189+
$this->assertFalse($this->stringable('/')->is(' /'));
190+
$this->assertFalse($this->stringable('/a')->is('/'));
191+
$this->assertTrue($this->stringable('foo/bar/baz')->is('foo/*'));
192+
193+
$this->assertTrue($this->stringable('App\Class@method')->is('*@*'));
194+
$this->assertTrue($this->stringable('app\Class@')->is('*@*'));
195+
$this->assertTrue($this->stringable('@method')->is('*@*'));
196+
197+
// is case sensitive
198+
$this->assertFalse($this->stringable('foo/bar/baz')->is('*BAZ*'));
199+
$this->assertFalse($this->stringable('foo/bar/baz')->is('*FOO*'));
200+
$this->assertFalse($this->stringable('a')->is('A'));
201+
202+
// is not case sensitive
203+
$this->assertTrue($this->stringable('a')->is('A', true));
204+
$this->assertTrue($this->stringable('foo/bar/baz')->is('*BAZ*', true));
205+
$this->assertTrue($this->stringable('a/')->is(['A*', 'B*'], true));
206+
$this->assertFalse($this->stringable('f/')->is(['A*', 'B*'], true));
207+
$this->assertTrue($this->stringable('foo')->is('FOO', true));
208+
$this->assertTrue($this->stringable('foo/bar/baz')->is('*FOO*', true));
209+
$this->assertTrue($this->stringable('FOO/bar')->is('foo/*', true));
210+
211+
// Accepts array of patterns
212+
$this->assertTrue($this->stringable('a/')->is(['a*', 'b*']));
213+
$this->assertTrue($this->stringable('b/')->is(['a*', 'b*']));
214+
$this->assertFalse($this->stringable('f/')->is(['a*', 'b*']));
215+
216+
// numeric values and patterns
217+
$this->assertFalse($this->stringable(123)->is(['a*', 'b*']));
218+
$this->assertTrue($this->stringable(11211)->is(['*2*', 'b*']));
219+
220+
$this->assertTrue($this->stringable('blah/baz/foo')->is('*/foo'));
221+
222+
// empty patterns
223+
$this->assertFalse($this->stringable('test')->is([]));
224+
}
225+
186226
public function testIsAscii()
187227
{
188228
$this->assertTrue($this->stringable('Hello World!')->isAscii());

0 commit comments

Comments
 (0)