From bce782f16c6872979aef919dd11b7996a8301444 Mon Sep 17 00:00:00 2001 From: Andreas Schantl Date: Tue, 30 Jul 2024 13:55:50 +0200 Subject: [PATCH 1/3] Add where_in modifier --- src/Modifiers/CoreModifiers.php | 17 +++++++++++++++++ tests/Antlers/Runtime/CoreModifiersTest.php | 21 +++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/src/Modifiers/CoreModifiers.php b/src/Modifiers/CoreModifiers.php index 31b3cefb9ba..175751e3854 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 03fc062a609..014c48ae37e 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 }}')); From e207d9896cbf494b2941efc4f641d75dd9dc4013 Mon Sep 17 00:00:00 2001 From: Andreas Schantl Date: Tue, 30 Jul 2024 14:17:10 +0200 Subject: [PATCH 2/3] Update function description --- src/Modifiers/CoreModifiers.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Modifiers/CoreModifiers.php b/src/Modifiers/CoreModifiers.php index 175751e3854..68d83417f51 100644 --- a/src/Modifiers/CoreModifiers.php +++ b/src/Modifiers/CoreModifiers.php @@ -2897,7 +2897,7 @@ public function where($value, $params) } /** - * Filters the data by a given key / value pair. + * Filters the data by a given key that matches an array of values. * * @param array $value * @param array $params From 163e985560a43c6bbacb5f8812b75b0fa0a32518 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Wed, 31 Jul 2024 16:44:49 -0400 Subject: [PATCH 3/3] move to dedicated test --- tests/Antlers/Runtime/CoreModifiersTest.php | 21 ---------- tests/Modifiers/WhereInTest.php | 44 +++++++++++++++++++++ 2 files changed, 44 insertions(+), 21 deletions(-) create mode 100644 tests/Modifiers/WhereInTest.php diff --git a/tests/Antlers/Runtime/CoreModifiersTest.php b/tests/Antlers/Runtime/CoreModifiersTest.php index 014c48ae37e..03fc062a609 100644 --- a/tests/Antlers/Runtime/CoreModifiersTest.php +++ b/tests/Antlers/Runtime/CoreModifiersTest.php @@ -238,27 +238,6 @@ 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 }}')); diff --git a/tests/Modifiers/WhereInTest.php b/tests/Modifiers/WhereInTest.php new file mode 100644 index 00000000000..c5ff317b601 --- /dev/null +++ b/tests/Modifiers/WhereInTest.php @@ -0,0 +1,44 @@ + 'Desk', 'price' => 200], + ['product' => 'Chair', 'price' => 100], + ['product' => 'Bookcase', 'price' => 150], + ['product' => 'Door', 'price' => 100], + ]; + $modified = $this->modify($collection, ['price', [150, 200]]); + $this->assertEquals(['Desk', 'Bookcase'], Arr::pluck($modified, 'product')); + } + + #[Test] + public function it_filters_data_by_key_and_single_value(): void + { + $collection = [ + ['product' => 'Desk', 'price' => 200], + ['product' => 'Chair', 'price' => 100], + ['product' => 'Bookcase', 'price' => 150], + ['product' => 'Door', 'price' => 100], + ]; + $modified = $this->modify($collection, ['price', 100]); + $this->assertEquals(['Chair', 'Door'], Arr::pluck($modified, 'product')); + } + + private function modify($value, array $params) + { + return Modify::value($value)->whereIn($params)->fetch(); + } +}