Skip to content

Instantly share code, notes, and snippets.

@s-chizhik
Created April 16, 2021 08:03
Show Gist options
  • Save s-chizhik/804c9fdd78b039ee3adf97684acdf159 to your computer and use it in GitHub Desktop.
Save s-chizhik/804c9fdd78b039ee3adf97684acdf159 to your computer and use it in GitHub Desktop.
<?php declare(strict_types=1);
namespace App\Http\Api\Filters;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
abstract class Filter
{
protected Request $request;
protected Builder $builder;
public function __construct(Request $request)
{
$this->request = $request;
}
public function filters(): array
{
return $this->request->keys();
}
public function apply(Builder $builder): Builder
{
$this->builder = $builder;
foreach ($this->filters() as $fieldName) {
$value = $this->request->get($fieldName);
if ($value && method_exists($this, $fieldName)) {
$this->$fieldName($value);
}
}
return $this->builder;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment