Skip to content
Merged
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
8 changes: 4 additions & 4 deletions src/Stache/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,19 +157,19 @@ protected function filterWhereBasic($values, $where)

protected function filterWhereIn($values, $where)
{
$lookup = array_flip($where['values']);
$lookup = array_flip(array_map(fn ($v) => $v ?? '__NULL__', $where['values']));

return $values->filter(
fn ($value) => isset($lookup[$value])
fn ($value) => isset($lookup[$value ?? '__NULL__'])
);
}

protected function filterWhereNotIn($values, $where)
{
$lookup = array_flip($where['values']);
$lookup = array_flip(array_map(fn ($v) => $v ?? '__NULL__', $where['values']));

return $values->filter(
fn ($value) => ! isset($lookup[$value])
fn ($value) => ! isset($lookup[$value ?? '__NULL__'])
);
}

Expand Down
30 changes: 30 additions & 0 deletions tests/Data/Entries/EntryQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,36 @@ public function entries_are_found_using_or_where_not_in()
$this->assertEquals(['Post 3', 'Post 4'], $entries->map->title->all());
}

#[Test]
public function entries_are_found_using_where_in_with_null()
{
EntryFactory::id('1')->slug('post-1')->collection('posts')->data(['title' => 'Post 1', 'category' => 'news'])->create();
EntryFactory::id('2')->slug('post-2')->collection('posts')->data(['title' => 'Post 2', 'category' => 'blog'])->create();
EntryFactory::id('3')->slug('post-3')->collection('posts')->data(['title' => 'Post 3'])->create(); // category is null
EntryFactory::id('4')->slug('post-4')->collection('posts')->data(['title' => 'Post 4', 'category' => 'news'])->create();
EntryFactory::id('5')->slug('post-5')->collection('posts')->data(['title' => 'Post 5'])->create(); // category is null

$entries = Entry::query()->whereIn('category', ['news', null])->get();

$this->assertCount(4, $entries);
$this->assertEquals(['Post 1', 'Post 3', 'Post 4', 'Post 5'], $entries->map->title->all());
}

#[Test]
public function entries_are_found_using_where_not_in_with_null()
{
EntryFactory::id('1')->slug('post-1')->collection('posts')->data(['title' => 'Post 1', 'category' => 'news'])->create();
EntryFactory::id('2')->slug('post-2')->collection('posts')->data(['title' => 'Post 2', 'category' => 'blog'])->create();
EntryFactory::id('3')->slug('post-3')->collection('posts')->data(['title' => 'Post 3'])->create(); // category is null
EntryFactory::id('4')->slug('post-4')->collection('posts')->data(['title' => 'Post 4', 'category' => 'news'])->create();
EntryFactory::id('5')->slug('post-5')->collection('posts')->data(['title' => 'Post 5'])->create(); // category is null

$entries = Entry::query()->whereNotIn('category', ['news', null])->get();

$this->assertCount(1, $entries);
$this->assertEquals(['Post 2'], $entries->map->title->all());
}

#[Test]
public function entries_are_found_using_where_date()
{
Expand Down