Skip to content

Commit af6115e

Browse files
[Bugfix] Handle array or collection comparison in filter matching (#27)
* fix: check if the searchable is an array or collection * test: can be filtered using where in with an array or a collection
1 parent 45066b3 commit af6115e

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

src/Engines/ArrayEngine.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,19 @@ private function matchesFilters($record, $filters, $not = false)
140140

141141
$match = function ($record, $key, $value) {
142142
if (is_array($value)) {
143-
return in_array(data_get($record, $key), $value, true);
143+
$needle = data_get($record, $key);
144+
145+
if (is_array($needle)) {
146+
return !empty(array_intersect($needle, $value));
147+
}
148+
149+
if ($needle instanceof Collection) {
150+
return $needle->contains(function ($item) use ($value) {
151+
return in_array($item, $value, true);
152+
});
153+
}
154+
155+
return in_array($needle, $value, true);
144156
}
145157
return data_get($record, $key) === $value;
146158
};

tests/Engines/ArrayEngineTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,46 @@ public function it_can_be_filtered_using_where_in()
456456
$this->assertEquals(2, $results['hits'][0]['scoutKey']);
457457
}
458458

459+
/** @test */
460+
public function it_can_be_filtered_using_where_in_array()
461+
{
462+
$engine = new ArrayEngine(new ArrayStore());
463+
$engine->update(Collection::make([
464+
new SearchableModel(['foo' => 'bar', 'x' => ['x', 'y'], 'scoutKey' => 1]),
465+
new SearchableModel(['foo' => 'baz', 'x' => ['x'], 'scoutKey' => 2]),
466+
new SearchableModel(['foo' => 'bax', 'x' => ['z'], 'scoutKey' => 3]),
467+
]));
468+
469+
$builder = new Builder(new SearchableModel(), null);
470+
$builder->whereIns = [
471+
'x' => ['x', 'y']
472+
];
473+
$results = $engine->search($builder);
474+
475+
$this->assertCount(2, $results['hits']);
476+
$this->assertEquals([2, 1], array_column($results['hits'], 'scoutKey'));
477+
}
478+
479+
/** @test */
480+
public function it_can_be_filtered_using_where_in_collection()
481+
{
482+
$engine = new ArrayEngine(new ArrayStore());
483+
$engine->update(Collection::make([
484+
new SearchableModel(['foo' => 'bar', 'x' => collect(['x', 'y']), 'scoutKey' => 1]),
485+
new SearchableModel(['foo' => 'baz', 'x' => collect(['x']), 'scoutKey' => 2]),
486+
new SearchableModel(['foo' => 'bax', 'x' => collect(['z']), 'scoutKey' => 3]),
487+
]));
488+
489+
$builder = new Builder(new SearchableModel(), null);
490+
$builder->whereIns = [
491+
'x' => ['x', 'y']
492+
];
493+
$results = $engine->search($builder);
494+
495+
$this->assertCount(2, $results['hits']);
496+
$this->assertEquals([2, 1], array_column($results['hits'], 'scoutKey'));
497+
}
498+
459499
/** @test */
460500
public function it_can_be_filtered_using_where_not_in()
461501
{

0 commit comments

Comments
 (0)