Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fallback #5

Merged
merged 1 commit into from
Nov 8, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/Filters/QueryFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,18 @@ abstract class QueryFilter
/** @var Builder */
protected $realBuilder;

/**
* Here, default parameters for functions are saved
* @var array
*/
public array $default = [];

/**
* Here, if a function does not work, a helper function is shown for it
* @var array
*/
public array $fallback = [];

public function __construct(Builder $builder)
{
$this->realBuilder = $builder;
Expand All @@ -36,11 +46,18 @@ public function apply(array $validated)
{
$validatedKeys = array_keys($validated);
$defaultKeys = array_keys($this->default);
$fallbackKeys = array_keys($this->fallback);

foreach ($this->filters() as $filter) {
if (in_array($filter, $validatedKeys)) {
$this->$filter($validated[$filter], $validated);
} elseif (in_array($filter, $defaultKeys)) {
$this->$filter($this->default[$filter], $validated);
} elseif (in_array($filter, $fallbackKeys)) {
if(!in_array($this->fallback[$filter], $this->filters())){
throw new \BadMethodCallException("This method not found!", 500);
}
$this->{$this->fallback[$filter]}(null, $validated);
}
}
return $this;
Expand Down