diff --git a/src/Modifiers/CoreModifiers.php b/src/Modifiers/CoreModifiers.php index 31b3cefb9b..175751e385 100644 --- a/src/Modifiers/CoreModifiers.php +++ b/src/Modifiers/CoreModifiers.php @@ -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 * tags between the last two words of each paragraph. diff --git a/tests/Antlers/Runtime/CoreModifiersTest.php b/tests/Antlers/Runtime/CoreModifiersTest.php index 03fc062a60..014c48ae37 100644 --- a/tests/Antlers/Runtime/CoreModifiersTest.php +++ b/tests/Antlers/Runtime/CoreModifiersTest.php @@ -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 }}'));