Skip to content

Commit

Permalink
Let Str::is handles many patterns.
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasmichot committed Jul 17, 2017
1 parent d607b9c commit ceb59c1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
28 changes: 20 additions & 8 deletions src/Illuminate/Support/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,18 +149,30 @@ public static function finish($value, $cap)
*/
public static function is($pattern, $value)
{
if ($pattern == $value) {
return true;
$patterns = is_array($pattern) ? $pattern : (array) $pattern;

if (empty($patterns)) {
return false;
}

$pattern = preg_quote($pattern, '#');
foreach ($patterns as $pattern) {
if ($pattern == $value) {
return true;
}

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

return (bool) preg_match('#^'.$pattern.'\z#u', $value);
// Asterisks are translated into zero-or-more regular expression wildcards
// to make it convenient to check if the strings starts with the given
// pattern such as "library/*", making any string check convenient.
$pattern = str_replace('\*', '.*', $pattern);

if (preg_match('#^'.$pattern.'\z#u', $value) === 1) {
return true;
}
}

return false;
}

/**
Expand Down
12 changes: 12 additions & 0 deletions tests/Support/SupportHelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,18 @@ public function testStrIs()
$this->assertFalse(Str::is('foo.ar', 'foobar'));
$this->assertFalse(Str::is('foo?bar', 'foobar'));
$this->assertFalse(Str::is('foo?bar', 'fobar'));

$this->assertTrue(Str::is([
'*.dev',
'*oc*',
], 'localhost.dev'));

$this->assertFalse(Str::is([
'/',
'a*',
], 'localhost.dev'));

$this->assertFalse(Str::is([], 'localhost.dev'));
}

public function testStrRandom()
Expand Down

0 comments on commit ceb59c1

Please sign in to comment.