Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.5] Let Str::is handles many patterns #20108

Merged
merged 1 commit into from
Jul 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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