-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
399 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
<?php | ||
/* | ||
* Copyright 2024 Cloud Creativity Limited | ||
* | ||
* Use of this source code is governed by an MIT-style | ||
* license that can be found in the LICENSE file or at | ||
* https://opensource.org/licenses/MIT. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaravelJsonApi\Eloquent\Filters\Concerns; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
|
||
trait HasColumns | ||
{ | ||
/** | ||
* @var string|null | ||
*/ | ||
private ?string $table = null; | ||
|
||
/** | ||
* @var array<string> | ||
*/ | ||
private array $columns = []; | ||
|
||
/** | ||
* @return array<string> | ||
*/ | ||
public function columns(): array | ||
{ | ||
return $this->columns; | ||
} | ||
|
||
/** | ||
* Add a column to the filter. | ||
* | ||
* @param string $column | ||
* @return $this | ||
*/ | ||
public function withColumn(string $column): static | ||
{ | ||
$this->columns[] = $column; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Add columns to the filter. | ||
* | ||
* @param string ...$columns | ||
* @return $this | ||
*/ | ||
public function withColumns(string ...$columns): static | ||
{ | ||
$this->columns = [ | ||
...$this->columns, | ||
...$columns, | ||
]; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Force the table name when qualifying the columns. | ||
* | ||
* This allows the developer to force the table that the columns are qualified with. | ||
* | ||
* @param string $table | ||
* @return $this | ||
*/ | ||
public function qualifyAs(string $table): static | ||
{ | ||
$this->table = $table; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Get qualified columns. | ||
* | ||
* @return array<string> | ||
*/ | ||
protected function qualifiedColumns(?Model $model = null): array | ||
{ | ||
if ($this->table) { | ||
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
/* | ||
* Copyright 2024 Cloud Creativity Limited | ||
* | ||
* Use of this source code is governed by an MIT-style | ||
* license that can be found in the LICENSE file or at | ||
* https://opensource.org/licenses/MIT. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaravelJsonApi\Eloquent\Filters; | ||
|
||
use Illuminate\Support\Traits\Conditionable; | ||
use LaravelJsonApi\Eloquent\Contracts\Filter; | ||
|
||
class WhereAll implements Filter | ||
{ | ||
use Concerns\DeserializesValue; | ||
use Concerns\HasColumns; | ||
use Concerns\HasOperator; | ||
use Concerns\IsSingular; | ||
use Conditionable; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private string $name; | ||
|
||
/** | ||
* Create a new filter. | ||
* | ||
* @param string $name | ||
* @param array<string>|null $columns | ||
* @return static | ||
*/ | ||
public static function make(string $name, array $columns = null): static | ||
{ | ||
return new static($name, $columns); | ||
} | ||
|
||
/** | ||
* WhereAll constructor. | ||
* | ||
* @param string $name | ||
* @param array<string>|null $columns | ||
*/ | ||
public function __construct(string $name, array $columns = null) | ||
{ | ||
$this->name = $name; | ||
$this->columns = $columns ?? []; | ||
$this->operator = '='; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function key(): string | ||
{ | ||
return $this->name; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function apply($query, $value) | ||
{ | ||
return $query->whereAll( | ||
$this->qualifiedColumns($query->getModel()), | ||
$this->operator(), | ||
$this->deserialize($value) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
/* | ||
* Copyright 2024 Cloud Creativity Limited | ||
* | ||
* Use of this source code is governed by an MIT-style | ||
* license that can be found in the LICENSE file or at | ||
* https://opensource.org/licenses/MIT. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaravelJsonApi\Eloquent\Filters; | ||
|
||
use Illuminate\Support\Traits\Conditionable; | ||
use LaravelJsonApi\Eloquent\Contracts\Filter; | ||
|
||
class WhereAny implements Filter | ||
{ | ||
use Concerns\DeserializesValue; | ||
use Concerns\HasColumns; | ||
use Concerns\HasOperator; | ||
use Concerns\IsSingular; | ||
use Conditionable; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private string $name; | ||
|
||
/** | ||
* Create a new filter. | ||
* | ||
* @param string $name | ||
* @param array<string>|null $columns | ||
* @return static | ||
*/ | ||
public static function make(string $name, array $columns = null): static | ||
{ | ||
return new static($name, $columns); | ||
} | ||
|
||
/** | ||
* WhereAny constructor. | ||
* | ||
* @param string $name | ||
* @param array<string>|null $columns | ||
*/ | ||
public function __construct(string $name, array $columns = null) | ||
{ | ||
$this->name = $name; | ||
$this->columns = $columns ?? []; | ||
$this->operator = '='; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function key(): string | ||
{ | ||
return $this->name; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function apply($query, $value) | ||
{ | ||
return $query->whereAny( | ||
$this->qualifiedColumns($query->getModel()), | ||
$this->operator(), | ||
$this->deserialize($value) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.