Skip to content

Commit bd8f6ab

Browse files
authored
Merge pull request #58 from ensi-platform/v7-ecs-803
add orWhere and orWhereNot
2 parents 8d05d85 + 49d3573 commit bd8f6ab

File tree

3 files changed

+46
-11
lines changed

3 files changed

+46
-11
lines changed

src/Concerns/DecoratesBoolQuery.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,27 @@ public function where(string $field, mixed $operator, mixed $value = null): stat
2323
return $this;
2424
}
2525

26+
public function orWhere(string $field, mixed $operator, mixed $value = null): static
27+
{
28+
$this->forwardCallTo($this->boolQuery(), __FUNCTION__, func_get_args());
29+
30+
return $this;
31+
}
32+
2633
public function whereNot(string $field, mixed $value): static
2734
{
2835
$this->forwardCallTo($this->boolQuery(), __FUNCTION__, func_get_args());
2936

3037
return $this;
3138
}
3239

40+
public function orWhereNot(string $field, mixed $value): static
41+
{
42+
$this->forwardCallTo($this->boolQuery(), __FUNCTION__, func_get_args());
43+
44+
return $this;
45+
}
46+
3347
public function whereHas(string $nested, Closure $filter): static
3448
{
3549
$this->forwardCallTo($this->boolQuery(), __FUNCTION__, func_get_args());

src/Contracts/BoolQuery.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@ interface BoolQuery
99
{
1010
public function where(string $field, mixed $operator, mixed $value = null): static;
1111

12+
public function orWhere(string $field, mixed $operator, mixed $value = null): static;
13+
1214
public function whereNot(string $field, mixed $value): static;
1315

16+
public function orWhereNot(string $field, mixed $value): static;
17+
1418
public function whereIn(string $field, array|Arrayable $values): static;
1519

1620
public function orWhereIn(string $field, array|Arrayable $values): static;

src/Filtering/BoolQueryBuilder.php

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -93,23 +93,47 @@ public function where(string $field, mixed $operator, mixed $value = null): stat
9393
return $this->whereNot($field, $value);
9494
}
9595

96-
if (func_num_args() === 2) {
97-
[$operator, $value] = ['=', $operator];
96+
$this->filter->add($this->makeWhere(...func_get_args()));
97+
98+
return $this;
99+
}
100+
101+
public function orWhere(string $field, mixed $operator, mixed $value = null): static
102+
{
103+
if ($operator === '!=') {
104+
return $this->orWhereNot($field, $value);
98105
}
99106

100-
$criteria = $this->createComparisonCriteria($this->absolutePath($field), $operator, $value);
101-
$this->filter->add($criteria);
107+
$this->should->add($this->makeWhere(...func_get_args()));
102108

103109
return $this;
104110
}
105111

112+
protected function makeWhere(string $field, mixed $operator, mixed $value = null): Criteria
113+
{
114+
if (func_num_args() === 2) {
115+
[$operator, $value] = ['=', $operator];
116+
}
117+
118+
return $operator === '='
119+
? new Term($this->absolutePath($field), $value)
120+
: new RangeBound($this->absolutePath($field), $operator, $value);
121+
}
122+
106123
public function whereNot(string $field, mixed $value): static
107124
{
108125
$this->mustNot->add(new Term($this->absolutePath($field), $value));
109126

110127
return $this;
111128
}
112129

130+
public function orWhereNot(string $field, mixed $value): static
131+
{
132+
$this->should->add(new Term($this->absolutePath($field), $value));
133+
134+
return $this;
135+
}
136+
113137
public function whereIn(string $field, array|Arrayable $values): static
114138
{
115139
$this->filter->add(new Terms($this->absolutePath($field), $values));
@@ -240,13 +264,6 @@ protected function addNestedCriteria(string $nested, Closure $filter, CriteriaCo
240264
return $this;
241265
}
242266

243-
protected function createComparisonCriteria(string $field, string $operator, mixed $value): Criteria
244-
{
245-
return $operator === '=' || $operator === '!='
246-
? new Term($field, $value)
247-
: new RangeBound($field, $operator, $value);
248-
}
249-
250267
protected function basePath(): string
251268
{
252269
return $this->path;

0 commit comments

Comments
 (0)