Skip to content

Commit

Permalink
Merge pull request #147 from alberto-bottarini/main
Browse files Browse the repository at this point in the history
Make operator optional in filters
  • Loading branch information
alexzarbn authored Jan 24, 2022
2 parents 73371f3 + 2743725 commit 375c37f
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 7 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
},
"require-dev": {
"mockery/mockery": "^1.0",
"phpunit/phpunit": "^6.0|^7.0|^8.0|^9.0",
"orchestra/testbench": "^3.7|^4.0|^5.0|^6.0"
"orchestra/testbench": "^3.7|^4.0|^5.0|^6.0",
"phpunit/phpunit": "^6.0|^7.0|^8.0|^9.0"
},
"autoload": {
"psr-4": {
Expand Down
2 changes: 1 addition & 1 deletion src/Drivers/Standard/ParamsValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function validateFilters(Request $request): void
'filters' => ['sometimes', 'array'],
'filters.*.type' => ['sometimes', 'in:and,or'],
'filters.*.field' => ['required_with:filters', 'regex:/^[\w.\_\-\>]+$/', new WhitelistedField($this->filterableBy)],
'filters.*.operator' => ['required_with:filters', 'in:<,<=,>,>=,=,!=,like,not like,ilike,not ilike,in,not in'],
'filters.*.operator' => ['sometimes', 'in:<,<=,>,>=,=,!=,like,not like,ilike,not ilike,in,not in'],
'filters.*.value' => ['present', 'nullable'],
]
)->validate();
Expand Down
6 changes: 3 additions & 3 deletions src/Drivers/Standard/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ protected function buildFilterNestedQueryWhereClause(
if (!is_array($filterDescriptor['value']) || $constraint === 'whereDate') {
$query->{$or ? 'or' . ucfirst($constraint) : $constraint}(
$field,
$filterDescriptor['operator'],
$filterDescriptor['operator'] ?? '=',
$filterDescriptor['value']
);
} else {
Expand Down Expand Up @@ -258,14 +258,14 @@ protected function buildPivotFilterNestedQueryWhereClause(
$query->addNestedWhereQuery(
$query->newPivotStatement()->whereDate(
$query->getTable().".{$field}",
$filterDescriptor['operator'],
$filterDescriptor['operator'] ?? '=',
$filterDescriptor['value']
)
);
} elseif (!is_array($filterDescriptor['value'])) {
$query->{$or ? 'orWherePivot' : 'wherePivot'}(
$field,
$filterDescriptor['operator'],
$filterDescriptor['operator'] ?? '=',
$filterDescriptor['value']
);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function build(): ?array
]
],
'required' => [
'field', 'operator', 'value'
'field', 'value'
]
]
];
Expand Down
23 changes: 23 additions & 0 deletions tests/Feature/StandardIndexFilteringOperationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,29 @@

class StandardIndexFilteringOperationsTest extends TestCase
{
/** @test */
public function getting_a_list_of_resources_filtered_by_model_field_using_default_operator(): void
{
$matchingPost = factory(Post::class)->create(['title' => 'match'])->fresh();
factory(Post::class)->create(['title' => 'not match'])->fresh();

Gate::policy(Post::class, GreenPolicy::class);

$response = $this->post(
'/api/posts/search',
[
'filters' => [
['field' => 'title', 'value' => 'match'],
],
]
);

$this->assertResourcesPaginated(
$response,
$this->makePaginator([$matchingPost], 'posts/search')
);
}

/** @test */
public function getting_a_list_of_resources_filtered_by_model_field_using_equal_operator(): void
{
Expand Down

0 comments on commit 375c37f

Please sign in to comment.