Skip to content

Commit

Permalink
Match how whereJsonContains to how Laravel handles it
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanmitchell committed Nov 12, 2024
1 parent d25c9e3 commit 158bb14
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/Query/IteratorBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ protected function filterWhereJsonContains($entries, $where)
return false;
}

return ! empty(array_intersect($value, $where['values']));
return count(array_intersect($value, $where['values'])) == count($where['values']);
});
}

Expand All @@ -193,7 +193,7 @@ protected function filterWhereJsonDoesntContain($entries, $where)
return true;
}

return empty(array_intersect($value, $where['values']));
return count(array_intersect($value, $where['values'])) != count($where['values']);
});
}

Expand Down
4 changes: 2 additions & 2 deletions src/Stache/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ protected function filterWhereJsonContains($values, $where)
return false;
}

return ! empty(array_intersect($value, $where['values']));
return count(array_intersect($value, $where['values'])) == count($where['values']);
});
}

Expand All @@ -312,7 +312,7 @@ protected function filterWhereJsonDoesntContain($values, $where)
return true;
}

return empty(array_intersect($value, $where['values']));
return count(array_intersect($value, $where['values'])) != count($where['values']);
});
}

Expand Down
6 changes: 3 additions & 3 deletions tests/Data/Assets/AssetQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -344,10 +344,10 @@ public function assets_are_found_using_where_json_contains()
Asset::find('test::d.jpg')->data(['test_taxonomy' => ['taxonomy-3', 'taxonomy-4']])->save();
Asset::find('test::e.jpg')->data(['test_taxonomy' => ['taxonomy-5']])->save();

$assets = $this->container->queryAssets()->whereJsonContains('test_taxonomy', ['taxonomy-1', 'taxonomy-5'])->get();
$assets = $this->container->queryAssets()->whereJsonContains('test_taxonomy', ['taxonomy-1', 'taxonomy-3'])->get();

$this->assertCount(3, $assets);
$this->assertEquals(['a', 'c', 'e'], $assets->map->filename()->all());
$this->assertCount(1, $assets);
$this->assertEquals(['c'], $assets->map->filename()->all());

$assets = $this->container->queryAssets()->whereJsonContains('test_taxonomy', 'taxonomy-1')->get();

Expand Down
6 changes: 3 additions & 3 deletions tests/Data/Entries/EntryQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -399,10 +399,10 @@ public function entries_are_found_using_where_json_contains()
EntryFactory::id('4')->slug('post-4')->collection('posts')->data(['title' => 'Post 4', 'test_taxonomy' => ['taxonomy-3', 'taxonomy-4']])->create();
EntryFactory::id('5')->slug('post-5')->collection('posts')->data(['title' => 'Post 5', 'test_taxonomy' => ['taxonomy-5']])->create();

$entries = Entry::query()->whereJsonContains('test_taxonomy', ['taxonomy-1', 'taxonomy-5'])->get();
$entries = Entry::query()->whereJsonContains('test_taxonomy', ['taxonomy-1', 'taxonomy-3'])->get();

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

$entries = Entry::query()->whereJsonContains('test_taxonomy', 'taxonomy-1')->get();

Expand Down
6 changes: 3 additions & 3 deletions tests/Data/Taxonomies/TermQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -509,10 +509,10 @@ public function terms_are_found_using_where_json_contains()
Term::make('4')->taxonomy('tags')->data(['test_taxonomy' => ['taxonomy-3', 'taxonomy-4']])->save();
Term::make('5')->taxonomy('tags')->data(['test_taxonomy' => ['taxonomy-5']])->save();

$entries = Term::query()->whereJsonContains('test_taxonomy', ['taxonomy-1', 'taxonomy-5'])->get();
$entries = Term::query()->whereJsonContains('test_taxonomy', ['taxonomy-1', 'taxonomy-3'])->get();

$this->assertCount(3, $entries);
$this->assertEquals(['1', '3', '5'], $entries->map->slug()->all());
$this->assertCount(1, $entries);
$this->assertEquals(['3'], $entries->map->slug()->all());

$entries = Term::query()->whereJsonContains('test_taxonomy', 'taxonomy-1')->get();

Expand Down
6 changes: 3 additions & 3 deletions tests/Forms/SubmissionQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,10 +293,10 @@ public function submissions_are_found_using_where_json_contains()
FormSubmission::make()->form($form)->data(['id' => '4', 'test_taxonomy' => ['taxonomy-3', 'taxonomy-4']])->save();
FormSubmission::make()->form($form)->data(['id' => '5', 'test_taxonomy' => ['taxonomy-5']])->save();

$entries = FormSubmission::query()->whereJsonContains('test_taxonomy', ['taxonomy-1', 'taxonomy-5'])->get();
$entries = FormSubmission::query()->whereJsonContains('test_taxonomy', ['taxonomy-1', 'taxonomy-3'])->get();

$this->assertCount(3, $entries);
$this->assertEquals(['1', '3', '5'], $entries->map->get('id')->all());
$this->assertCount(1, $entries);
$this->assertEquals(['3'], $entries->map->get('id')->all());

$entries = FormSubmission::query()->whereJsonContains('test_taxonomy', 'taxonomy-1')->get();

Expand Down

0 comments on commit 158bb14

Please sign in to comment.