From 0c8faf11caa24ac9a9b2b189fa5b87dc569b94d5 Mon Sep 17 00:00:00 2001 From: Pedro Augusto <93937477+devsquad-pedro-silva@users.noreply.github.com> Date: Thu, 29 May 2025 14:24:42 -0300 Subject: [PATCH 1/2] fix: check if the searchable is an array or collection --- src/Engines/ArrayEngine.php | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/Engines/ArrayEngine.php b/src/Engines/ArrayEngine.php index 8360f2c..22c3564 100644 --- a/src/Engines/ArrayEngine.php +++ b/src/Engines/ArrayEngine.php @@ -140,7 +140,19 @@ private function matchesFilters($record, $filters, $not = false) $match = function ($record, $key, $value) { if (is_array($value)) { - return in_array(data_get($record, $key), $value, true); + $needle = data_get($record, $key); + + if (is_array($needle)) { + return !empty(array_intersect($needle, $value)); + } + + if ($needle instanceof Collection) { + return $needle->contains(function ($item) use ($value) { + return in_array($item, $value, true); + }); + } + + return in_array($needle, $value, true); } return data_get($record, $key) === $value; }; From 52d362963fe6d6eb0165c515da5ca3e94d5cb7c8 Mon Sep 17 00:00:00 2001 From: Pedro Augusto <93937477+devsquad-pedro-silva@users.noreply.github.com> Date: Thu, 29 May 2025 14:36:20 -0300 Subject: [PATCH 2/2] test: can be filtered using where in with an array or a collection --- tests/Engines/ArrayEngineTest.php | 40 +++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/tests/Engines/ArrayEngineTest.php b/tests/Engines/ArrayEngineTest.php index 6b008c1..67ee0be 100644 --- a/tests/Engines/ArrayEngineTest.php +++ b/tests/Engines/ArrayEngineTest.php @@ -456,6 +456,46 @@ public function it_can_be_filtered_using_where_in() $this->assertEquals(2, $results['hits'][0]['scoutKey']); } + /** @test */ + public function it_can_be_filtered_using_where_in_array() + { + $engine = new ArrayEngine(new ArrayStore()); + $engine->update(Collection::make([ + new SearchableModel(['foo' => 'bar', 'x' => ['x', 'y'], 'scoutKey' => 1]), + new SearchableModel(['foo' => 'baz', 'x' => ['x'], 'scoutKey' => 2]), + new SearchableModel(['foo' => 'bax', 'x' => ['z'], 'scoutKey' => 3]), + ])); + + $builder = new Builder(new SearchableModel(), null); + $builder->whereIns = [ + 'x' => ['x', 'y'] + ]; + $results = $engine->search($builder); + + $this->assertCount(2, $results['hits']); + $this->assertEquals([2, 1], array_column($results['hits'], 'scoutKey')); + } + + /** @test */ + public function it_can_be_filtered_using_where_in_collection() + { + $engine = new ArrayEngine(new ArrayStore()); + $engine->update(Collection::make([ + new SearchableModel(['foo' => 'bar', 'x' => collect(['x', 'y']), 'scoutKey' => 1]), + new SearchableModel(['foo' => 'baz', 'x' => collect(['x']), 'scoutKey' => 2]), + new SearchableModel(['foo' => 'bax', 'x' => collect(['z']), 'scoutKey' => 3]), + ])); + + $builder = new Builder(new SearchableModel(), null); + $builder->whereIns = [ + 'x' => ['x', 'y'] + ]; + $results = $engine->search($builder); + + $this->assertCount(2, $results['hits']); + $this->assertEquals([2, 1], array_column($results['hits'], 'scoutKey')); + } + /** @test */ public function it_can_be_filtered_using_where_not_in() {