Skip to content

Commit

Permalink
Merge pull request #3 from oooiik/2-default-params
Browse files Browse the repository at this point in the history
default
  • Loading branch information
oooiik authored Nov 8, 2022
2 parents dbeeaa5 + be9c805 commit 139bf38
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/Filters/QueryFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Oooiik\LaravelQueryFilter\Filters;

use http\Exception\BadMethodCallException;
use Illuminate\Database\Eloquent\Builder;

abstract class QueryFilter
Expand All @@ -11,6 +12,8 @@ abstract class QueryFilter
/** @var Builder */
protected $realBuilder;

public array $default = [];

public function __construct(Builder $builder)
{
$this->realBuilder = $builder;
Expand All @@ -31,9 +34,13 @@ public function filters()

public function apply(array $validated)
{
$validatedKeys = array_keys($validated);
$defaultKeys = array_keys($this->default);
foreach ($this->filters() as $filter) {
if (in_array($filter, array_keys($validated))) {
if (in_array($filter, $validatedKeys)) {
$this->$filter($validated[$filter], $validated);
} elseif (in_array($filter, $defaultKeys)) {
$this->$filter($this->default[$filter], $validated);
}
}
return $this;
Expand Down

0 comments on commit 139bf38

Please sign in to comment.