Skip to content

fix: handle array-to-array comparison in filter matching #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/Engines/ArrayEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ protected function performSearch(Builder $builder, array $options = [])
$this->matchesFilters($record, $builder->whereIns) &&
$this->matchesFilters($record, data_get($builder, 'whereNotIns', []), true) &&
!empty(array_filter(iterator_to_array($values, false), function ($value) use ($builder) {
return !$builder->query || stripos($value, $builder->query) !== false;
return !$builder->query || stripos((string) $value, $builder->query) !== false;
}));
}, true);

Expand All @@ -139,10 +139,15 @@ private function matchesFilters($record, $filters, $not = false)
}

$match = function ($record, $key, $value) {
$recordValue = data_get($record, $key);
if (is_array($value)) {
return in_array(data_get($record, $key), $value, true);
if (is_array($recordValue)) {
return count(array_intersect($recordValue, $value)) > 0;
}
return in_array($recordValue, $value, true);
}
return data_get($record, $key) === $value;

return $recordValue === $value;
};

$match = Collection::make($filters)->every(function ($value, $key) use ($match, $record) {
Expand Down Expand Up @@ -289,7 +294,8 @@ protected function buildSearchQuery(Builder $builder)
);

return $this->constrainForSoftDeletes(
$builder, $this->addAdditionalConstraints($builder, $query->take($builder->limit))
$builder,
$this->addAdditionalConstraints($builder, $query->take($builder->limit))
);
}
}
60 changes: 60 additions & 0 deletions tests/Engines/ArrayEngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,66 @@ public function it_returns_empty_array_if_no_results_found()
$this->assertEquals(0, $results['total']);
}

/** @test */
public function it_can_filter_using_where_in_with_array_intersection()
{
$engine = new ArrayEngine(new ArrayStore());
$engine->update(Collection::make([
new SearchableModel(['id' => 1, 'tags' => ['php', 'laravel'], 'scoutKey' => 1]),
new SearchableModel(['id' => 2, 'tags' => ['javascript', 'vue'], 'scoutKey' => 2]),
new SearchableModel(['id' => 3, 'tags' => ['php', 'symfony'], 'scoutKey' => 3]),
]));

$builder = new Builder(new SearchableModel(), null);
$builder->whereIns = [
'tags' => ['php'],
];
$results = $engine->search($builder);

$this->assertCount(2, $results['hits']);
$this->assertEquals([3, 1], array_column($results['hits'], 'scoutKey'));
}

/** @test */
public function it_can_filter_using_where_not_in_with_array_values()
{
$engine = new ArrayEngine(new ArrayStore());
$engine->update(Collection::make([
new SearchableModel(['id' => 1, 'tags' => ['php', 'laravel'], 'scoutKey' => 1]),
new SearchableModel(['id' => 2, 'tags' => ['javascript', 'vue'], 'scoutKey' => 2]),
new SearchableModel(['id' => 3, 'tags' => ['php', 'symfony'], 'scoutKey' => 3]),
]));

$builder = new Builder(new SearchableModel(), null);
$builder->whereNotIns = [
'tags' => ['javascript'],
];
$results = $engine->search($builder);

$this->assertCount(2, $results['hits']);
$this->assertEquals([3, 1], array_column($results['hits'], 'scoutKey'));
}

/** @test */
public function it_can_filter_using_where_in_with_single_value_against_array()
{
$engine = new ArrayEngine(new ArrayStore());
$engine->update(Collection::make([
new SearchableModel(['id' => 1, 'category' => 'php', 'scoutKey' => 1]),
new SearchableModel(['id' => 2, 'category' => 'javascript', 'scoutKey' => 2]),
new SearchableModel(['id' => 3, 'category' => 'php', 'scoutKey' => 3]),
]));

$builder = new Builder(new SearchableModel(), null);
$builder->whereIns = [
'category' => ['php'],
];
$results = $engine->search($builder);

$this->assertCount(2, $results['hits']);
$this->assertEquals([3, 1], array_column($results['hits'], 'scoutKey'));
}

/** @test */
public function custom_index_can_be_passed()
{
Expand Down