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

[4.x] Make contains modifier handle array needles #8357

Closed
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 4 additions & 11 deletions src/Modifiers/CoreModifiers.php
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,10 @@ public function contains($haystack, $params, $context)
$needle = $this->getFromContext($context, $params);

if (is_array($haystack)) {
if (is_array($needle)) {
return count(array_intersect($haystack, $needle)) > 0;
}

if (Arr::isAssoc($haystack)) {
return Arr::exists($haystack, $needle);
}
Expand Down Expand Up @@ -1276,17 +1280,6 @@ public function isUrl($value)
return Str::isUrl($value);
}

/**
* Returns true if the string is an external URL.
*
* @param $value
* @return bool
*/
public function isExternalUrl($value)
{
return Str::isUrl($value) && URL::isExternal($value);
}

jasonvarga marked this conversation as resolved.
Show resolved Hide resolved
/**
* Determines if the date on a weekday.
*
Expand Down
18 changes: 18 additions & 0 deletions tests/Modifiers/ContainsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,24 @@ public function it_returns_false_if_needle_not_found_in_context_in_array(): void
$this->assertFalse($modified);
}

/** @test */
public function it_returns_true_if_needle_array_found_in_array(): void
{
$haystack = ['bacon', 'bread', 'tomato'];

$modified = $this->modify($haystack, ['delicious'], ['delicious' => ['bacon', 'bread'], 'gross' => 'broccoli']);
$this->assertTrue($modified);
}

/** @test */
public function it_returns_false_if_needle_array_not_found_in_array(): void
{
$haystack = ['bacon', 'bread', 'tomato'];

$modified = $this->modify($haystack, ['gross'], ['delicious' => 'bacon', 'gross' => ['broccoli']]);
$this->assertFalse($modified);
}

private function modify($value, array $params, array $context)
{
return Modify::value($value)->context($context)->contains($params)->fetch();
Expand Down