Skip to content

Commit 120c77c

Browse files
committed
Add additional Where operators
1 parent ecf5108 commit 120c77c

File tree

3 files changed

+65
-50
lines changed

3 files changed

+65
-50
lines changed

src/Laravel/Filter/SupportsOperators.php

Lines changed: 0 additions & 25 deletions
This file was deleted.

src/Laravel/Filter/Where.php

Lines changed: 52 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -38,31 +38,46 @@ public function apply(object $query, array|string $value, Context $context): voi
3838
return;
3939
}
4040

41-
[$operator, $resolved] = $this->resolveOperator($value);
42-
43-
switch ($operator) {
44-
case 'eq':
45-
case 'in':
46-
$this->applyEquals($query, $resolved);
47-
break;
48-
49-
case 'ne':
50-
$this->applyNotEquals($query, $resolved);
51-
break;
52-
53-
case 'lt':
54-
case 'lte':
55-
case 'gt':
56-
case 'gte':
57-
$this->applyComparison($query, $operator, $resolved);
58-
break;
59-
60-
case 'like':
61-
$this->applyLike($query, $resolved);
62-
break;
63-
64-
default:
65-
throw new BadRequestException("Unsupported operator: $operator");
41+
if (is_string($value) || array_is_list($value)) {
42+
$this->applyEquals($query, $value);
43+
return;
44+
}
45+
46+
foreach ($value as $operator => $v) {
47+
switch ($operator) {
48+
case 'eq':
49+
case 'in':
50+
$this->applyEquals($query, $v);
51+
break;
52+
53+
case 'ne':
54+
case 'notin':
55+
$this->applyNotEquals($query, $v);
56+
break;
57+
58+
case 'lt':
59+
case 'lte':
60+
case 'gt':
61+
case 'gte':
62+
$this->applyComparison($query, $operator, $v);
63+
break;
64+
65+
case 'like':
66+
$this->applyLike($query, $v);
67+
break;
68+
69+
case 'notlike':
70+
$this->applyNotLike($query, $v);
71+
break;
72+
73+
case 'null':
74+
case 'notnull':
75+
$this->applyNull($query, $operator === 'null' ? (bool) $v : !$v);
76+
break;
77+
78+
default:
79+
throw new BadRequestException("Unsupported operator: $operator");
80+
}
6681
}
6782
}
6883

@@ -107,6 +122,18 @@ private function applyLike(object $query, array|string $value): void
107122
$query->where($this->getColumn(), 'like', $value);
108123
}
109124

125+
private function applyNotLike(object $query, array|string $value): void
126+
{
127+
$value = $this->firstValue($value);
128+
129+
$query->where($this->getColumn(), 'not like', $value);
130+
}
131+
132+
private function applyNull(object $query, bool $value): void
133+
{
134+
$query->{$value ? 'whereNull' : 'whereNotNull'}($this->getColumn());
135+
}
136+
110137
private function firstValue(array|string $value): mixed
111138
{
112139
if (is_array($value)) {

src/Laravel/Filter/WhereHas.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,19 @@ public function apply(object $query, array|string $value, Context $context): voi
8181
});
8282
}
8383

84+
private function resolveOperator(array|string $value): array
85+
{
86+
if (is_array($value) && !array_is_list($value)) {
87+
$keys = array_keys($value);
88+
89+
if (count($keys) === 1 && in_array($keys[0], ['eq', 'in', 'ne'])) {
90+
return [$keys[0], $value[$keys[0]]];
91+
}
92+
}
93+
94+
return ['eq', $value];
95+
}
96+
8497
private function extractIds(array|string $value): ?array
8598
{
8699
if (is_string($value)) {

0 commit comments

Comments
 (0)