From 18559d967caef4f8b439fd8feec1169dbc8ec38d Mon Sep 17 00:00:00 2001 From: Christopher Gammie Date: Sun, 13 Oct 2024 12:52:07 +0100 Subject: [PATCH] refactor: tidy up where all and where any filters --- CHANGELOG.md | 4 +++ src/Filters/Concerns/HasColumns.php | 52 ++++++++++++++++++++--------- src/Filters/WhereAll.php | 15 +++------ src/Filters/WhereAny.php | 13 +++----- 4 files changed, 50 insertions(+), 34 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e8d6704..2daf1e2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file. This projec ## Unreleased +### Added + +- [#38](https://github.com/laravel-json-api/eloquent/pull/38) Added `WhereAll` and `WhereAny` filters. + ## [4.2.0] - 2024-08-26 ### Added diff --git a/src/Filters/Concerns/HasColumns.php b/src/Filters/Concerns/HasColumns.php index 95ec744..7cd536d 100644 --- a/src/Filters/Concerns/HasColumns.php +++ b/src/Filters/Concerns/HasColumns.php @@ -11,15 +11,15 @@ namespace LaravelJsonApi\Eloquent\Filters\Concerns; +use Illuminate\Database\Eloquent\Model; + trait HasColumns { - /** * @var string|null */ private ?string $table = null; - /** * @var array */ @@ -33,7 +33,13 @@ public function columns(): array return $this->columns; } - public function withColumn(string $column): self + /** + * Add a column to the filter. + * + * @param string $column + * @return $this + */ + public function withColumn(string $column): static { $this->columns[] = $column; @@ -41,28 +47,34 @@ public function withColumn(string $column): self } /** - * Force the table name when qualifying the columns. + * Add columns to the filter. * - * This allows the developer to force the table that the columns are qualified with. - * - * @param string $table + * @param string ...$columns * @return $this */ - public function qualifyAs(string $table): self + public function withColumns(string ...$columns): static { - $this->table = $table; + $this->columns = [ + ...$this->columns, + ...$columns, + ]; return $this; } /** - * Determine if developer has forced a table to qualify columns as + * Force the table name when qualifying the columns. + * + * This allows the developer to force the table that the columns are qualified with. * - * @return bool + * @param string $table + * @return $this */ - public function isQualified(): bool + public function qualifyAs(string $table): static { - return $this->table === null; + $this->table = $table; + + return $this; } /** @@ -70,10 +82,20 @@ public function isQualified(): bool * * @return array */ - protected function qualifiedColumns(): array + protected function qualifiedColumns(?Model $model = null): array { if ($this->table) { - return array_map(fn($column) => $this->table . '.' . $column, $this->columns); + return array_map( + fn($column) => $this->table . '.' . $column, + $this->columns, + ); + } + + if ($model) { + return array_map( + static fn($column) => $model->qualifyColumn($column), + $this->columns, + ); } return $this->columns; diff --git a/src/Filters/WhereAll.php b/src/Filters/WhereAll.php index c6864fb..9acf9a2 100644 --- a/src/Filters/WhereAll.php +++ b/src/Filters/WhereAll.php @@ -16,7 +16,6 @@ class WhereAll implements Filter { - use Concerns\DeserializesValue; use Concerns\HasColumns; use Concerns\HasOperator; @@ -32,19 +31,19 @@ class WhereAll implements Filter * Create a new filter. * * @param string $name - * @param array $columns + * @param array|null $columns * @return static */ - public static function make(string $name, array $columns = null): self + public static function make(string $name, array $columns = null): static { return new static($name, $columns); } /** - * WhereAny constructor. + * WhereAll constructor. * * @param string $name - * @param array $columns + * @param array|null $columns */ public function __construct(string $name, array $columns = null) { @@ -66,12 +65,8 @@ public function key(): string */ public function apply($query, $value) { - if (!$this->isQualified()){ - $this->qualifyAs($query->getModel()->getTable()); - } - return $query->whereAll( - $this->qualifiedColumns(), + $this->qualifiedColumns($query->getModel()), $this->operator(), $this->deserialize($value) ); diff --git a/src/Filters/WhereAny.php b/src/Filters/WhereAny.php index 43fa0b0..8a08c42 100644 --- a/src/Filters/WhereAny.php +++ b/src/Filters/WhereAny.php @@ -16,7 +16,6 @@ class WhereAny implements Filter { - use Concerns\DeserializesValue; use Concerns\HasColumns; use Concerns\HasOperator; @@ -32,10 +31,10 @@ class WhereAny implements Filter * Create a new filter. * * @param string $name - * @param array $columns + * @param array|null $columns * @return static */ - public static function make(string $name, array $columns = null): self + public static function make(string $name, array $columns = null): static { return new static($name, $columns); } @@ -44,7 +43,7 @@ public static function make(string $name, array $columns = null): self * WhereAny constructor. * * @param string $name - * @param array $columns + * @param array|null $columns */ public function __construct(string $name, array $columns = null) { @@ -66,12 +65,8 @@ public function key(): string */ public function apply($query, $value) { - if (!$this->isQualified()){ - $this->qualifyAs($query->getModel()->getTable()); - } - return $query->whereAny( - $this->qualifiedColumns(), + $this->qualifiedColumns($query->getModel()), $this->operator(), $this->deserialize($value) );