Skip to content

Commit

Permalink
refactor: tidy up where all and where any filters
Browse files Browse the repository at this point in the history
  • Loading branch information
lindyhopchris committed Oct 13, 2024
1 parent 3702578 commit 18559d9
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 34 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
52 changes: 37 additions & 15 deletions src/Filters/Concerns/HasColumns.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>
*/
Expand All @@ -33,47 +33,69 @@ 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;

return $this;
}

/**
* 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;
}

/**
* Get qualified columns.
*
* @return array<string>
*/
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;
Expand Down
15 changes: 5 additions & 10 deletions src/Filters/WhereAll.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

class WhereAll implements Filter
{

use Concerns\DeserializesValue;
use Concerns\HasColumns;
use Concerns\HasOperator;
Expand All @@ -32,19 +31,19 @@ class WhereAll implements Filter
* Create a new filter.
*
* @param string $name
* @param array<string> $columns
* @param array<string>|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<string> $columns
* @param array<string>|null $columns
*/
public function __construct(string $name, array $columns = null)
{
Expand All @@ -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)
);
Expand Down
13 changes: 4 additions & 9 deletions src/Filters/WhereAny.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

class WhereAny implements Filter
{

use Concerns\DeserializesValue;
use Concerns\HasColumns;
use Concerns\HasOperator;
Expand All @@ -32,10 +31,10 @@ class WhereAny implements Filter
* Create a new filter.
*
* @param string $name
* @param array<string> $columns
* @param array<string>|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);
}
Expand All @@ -44,7 +43,7 @@ public static function make(string $name, array $columns = null): self
* WhereAny constructor.
*
* @param string $name
* @param array<string> $columns
* @param array<string>|null $columns
*/
public function __construct(string $name, array $columns = null)
{
Expand All @@ -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)
);
Expand Down

0 comments on commit 18559d9

Please sign in to comment.