Skip to content

Commit 18559d9

Browse files
committed
refactor: tidy up where all and where any filters
1 parent 3702578 commit 18559d9

File tree

4 files changed

+50
-34
lines changed

4 files changed

+50
-34
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file. This projec
55

66
## Unreleased
77

8+
### Added
9+
10+
- [#38](https://github.com/laravel-json-api/eloquent/pull/38) Added `WhereAll` and `WhereAny` filters.
11+
812
## [4.2.0] - 2024-08-26
913

1014
### Added

src/Filters/Concerns/HasColumns.php

+37-15
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@
1111

1212
namespace LaravelJsonApi\Eloquent\Filters\Concerns;
1313

14+
use Illuminate\Database\Eloquent\Model;
15+
1416
trait HasColumns
1517
{
16-
1718
/**
1819
* @var string|null
1920
*/
2021
private ?string $table = null;
2122

22-
2323
/**
2424
* @var array<string>
2525
*/
@@ -33,47 +33,69 @@ public function columns(): array
3333
return $this->columns;
3434
}
3535

36-
public function withColumn(string $column): self
36+
/**
37+
* Add a column to the filter.
38+
*
39+
* @param string $column
40+
* @return $this
41+
*/
42+
public function withColumn(string $column): static
3743
{
3844
$this->columns[] = $column;
3945

4046
return $this;
4147
}
4248

4349
/**
44-
* Force the table name when qualifying the columns.
50+
* Add columns to the filter.
4551
*
46-
* This allows the developer to force the table that the columns are qualified with.
47-
*
48-
* @param string $table
52+
* @param string ...$columns
4953
* @return $this
5054
*/
51-
public function qualifyAs(string $table): self
55+
public function withColumns(string ...$columns): static
5256
{
53-
$this->table = $table;
57+
$this->columns = [
58+
...$this->columns,
59+
...$columns,
60+
];
5461

5562
return $this;
5663
}
5764

5865
/**
59-
* Determine if developer has forced a table to qualify columns as
66+
* Force the table name when qualifying the columns.
67+
*
68+
* This allows the developer to force the table that the columns are qualified with.
6069
*
61-
* @return bool
70+
* @param string $table
71+
* @return $this
6272
*/
63-
public function isQualified(): bool
73+
public function qualifyAs(string $table): static
6474
{
65-
return $this->table === null;
75+
$this->table = $table;
76+
77+
return $this;
6678
}
6779

6880
/**
6981
* Get qualified columns.
7082
*
7183
* @return array<string>
7284
*/
73-
protected function qualifiedColumns(): array
85+
protected function qualifiedColumns(?Model $model = null): array
7486
{
7587
if ($this->table) {
76-
return array_map(fn($column) => $this->table . '.' . $column, $this->columns);
88+
return array_map(
89+
fn($column) => $this->table . '.' . $column,
90+
$this->columns,
91+
);
92+
}
93+
94+
if ($model) {
95+
return array_map(
96+
static fn($column) => $model->qualifyColumn($column),
97+
$this->columns,
98+
);
7799
}
78100

79101
return $this->columns;

src/Filters/WhereAll.php

+5-10
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
class WhereAll implements Filter
1818
{
19-
2019
use Concerns\DeserializesValue;
2120
use Concerns\HasColumns;
2221
use Concerns\HasOperator;
@@ -32,19 +31,19 @@ class WhereAll implements Filter
3231
* Create a new filter.
3332
*
3433
* @param string $name
35-
* @param array<string> $columns
34+
* @param array<string>|null $columns
3635
* @return static
3736
*/
38-
public static function make(string $name, array $columns = null): self
37+
public static function make(string $name, array $columns = null): static
3938
{
4039
return new static($name, $columns);
4140
}
4241

4342
/**
44-
* WhereAny constructor.
43+
* WhereAll constructor.
4544
*
4645
* @param string $name
47-
* @param array<string> $columns
46+
* @param array<string>|null $columns
4847
*/
4948
public function __construct(string $name, array $columns = null)
5049
{
@@ -66,12 +65,8 @@ public function key(): string
6665
*/
6766
public function apply($query, $value)
6867
{
69-
if (!$this->isQualified()){
70-
$this->qualifyAs($query->getModel()->getTable());
71-
}
72-
7368
return $query->whereAll(
74-
$this->qualifiedColumns(),
69+
$this->qualifiedColumns($query->getModel()),
7570
$this->operator(),
7671
$this->deserialize($value)
7772
);

src/Filters/WhereAny.php

+4-9
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
class WhereAny implements Filter
1818
{
19-
2019
use Concerns\DeserializesValue;
2120
use Concerns\HasColumns;
2221
use Concerns\HasOperator;
@@ -32,10 +31,10 @@ class WhereAny implements Filter
3231
* Create a new filter.
3332
*
3433
* @param string $name
35-
* @param array<string> $columns
34+
* @param array<string>|null $columns
3635
* @return static
3736
*/
38-
public static function make(string $name, array $columns = null): self
37+
public static function make(string $name, array $columns = null): static
3938
{
4039
return new static($name, $columns);
4140
}
@@ -44,7 +43,7 @@ public static function make(string $name, array $columns = null): self
4443
* WhereAny constructor.
4544
*
4645
* @param string $name
47-
* @param array<string> $columns
46+
* @param array<string>|null $columns
4847
*/
4948
public function __construct(string $name, array $columns = null)
5049
{
@@ -66,12 +65,8 @@ public function key(): string
6665
*/
6766
public function apply($query, $value)
6867
{
69-
if (!$this->isQualified()){
70-
$this->qualifyAs($query->getModel()->getTable());
71-
}
72-
7368
return $query->whereAny(
74-
$this->qualifiedColumns(),
69+
$this->qualifiedColumns($query->getModel()),
7570
$this->operator(),
7671
$this->deserialize($value)
7772
);

0 commit comments

Comments
 (0)