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.8] Add support for containsAll in Str:contains #28688

Closed
wants to merge 3 commits into from
Closed
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
17 changes: 13 additions & 4 deletions src/Illuminate/Support/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,23 @@ public static function camel($value)
*
* @param string $haystack
* @param string|array $needles
* @param bool $containsAll
* @return bool
*/
public static function contains($haystack, $needles)
public static function contains($haystack, $needles, $containsAll = false)
{
foreach ((array) $needles as $needle) {
if ($needle !== '' && mb_strpos($haystack, $needle) !== false) {
return true;
if (! $containsAll) {
foreach ((array) $needles as $needle) {
if ($needle !== '' && mb_strpos($haystack, $needle) !== false) {
return true;
}
}

return false;
}

if (count((array) $needles) == count(array_intersect(explode(' ', strtolower($haystack)), (array) $needles))) {
return true;
}

return false;
Expand Down
5 changes: 5 additions & 0 deletions tests/Support/SupportStrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ public function testStrContains()
$this->assertFalse(Str::contains('taylor', 'xxx'));
$this->assertFalse(Str::contains('taylor', ['xxx']));
$this->assertFalse(Str::contains('taylor', ''));
$this->assertTrue(Str::contains('taylor otwell', ['taylor', 'otwell'], true));
$this->assertTrue(Str::contains('taylor otwell', 'taylor', true));
$this->assertFalse(Str::contains('taylor otwell', 'xxx', true));
$this->assertTrue(Str::contains('taylor otwell', ['taylor'], true));
$this->assertFalse(Str::contains('taylor otwell', ['taylor', 'xxx'], true));
}

public function testParseCallback()
Expand Down