Skip to content

Commit

Permalink
Add where_in modifier
Browse files Browse the repository at this point in the history
  • Loading branch information
andjsch committed Jul 30, 2024
1 parent f9777b8 commit bce782f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/Modifiers/CoreModifiers.php
Original file line number Diff line number Diff line change
Expand Up @@ -2896,6 +2896,23 @@ public function where($value, $params)
return $collection->values()->all();
}

/**
* Filters the data by a given key / value pair.
*
* @param array $value
* @param array $params
* @return array
*/
public function whereIn($value, $params)
{
$key = Arr::get($params, 0);
$arr = Arr::get($params, 1);

$collection = collect($value)->whereIn($key, $arr);

return $collection->values()->all();
}

/**
* Attempts to prevent widows in a string by adding
* <nobr> tags between the last two words of each paragraph.
Expand Down
21 changes: 21 additions & 0 deletions tests/Antlers/Runtime/CoreModifiersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,27 @@ public function test_where()
$this->assertSame('DominionNetrunner', $this->resultOf($template));
}

public function test_where_in()
{
$template = <<<'EOT'
{{ complex | where_in("last_name", ["Zebra", "Bravo"]) }}{{ first_name }}{{ /complex }}
EOT;

$this->assertSame('ZealousBlathering', $this->resultOf($template));

$template = <<<'EOT'
{{ complex where_in="{"last_name"}|{["Zebra", "Bravo"]}" }}{{ first_name }}{{ /complex }}
EOT;

$this->assertSame('ZealousBlathering', $this->resultOf($template));

$template = <<<'EOT'
{{ complex | where_in("last_name", "Zebra") }}{{ first_name }}{{ /complex }}
EOT;

$this->assertSame('Zealous', $this->resultOf($template));
}

public function test_unique()
{
$this->assertSame('zebra, hippo, hyena, giraffe', $this->resultOf('{{ checklist | unique | list }}'));
Expand Down

0 comments on commit bce782f

Please sign in to comment.