Skip to content

Commit

Permalink
Merge pull request #5 from oooiik/4-if-not-setting
Browse files Browse the repository at this point in the history
fallback
  • Loading branch information
oooiik committed Nov 8, 2022
2 parents 139bf38 + 5de084a commit 9ba908b
Showing 1 changed file with 17 additions and 0 deletions.
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

0 comments on commit 9ba908b

Please sign in to comment.