Created
April 16, 2021 08:03
-
-
Save s-chizhik/804c9fdd78b039ee3adf97684acdf159 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
<?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