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; }; 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() {