diff --git a/src/ResourceFilters.php b/src/ResourceFilters.php index caee88b..03d59e3 100644 --- a/src/ResourceFilters.php +++ b/src/ResourceFilters.php @@ -58,7 +58,11 @@ final public function __construct( protected Request $request, ) {} + protected function before(Builder $query): void {} + final public function handle(Builder $query, Closure $next): Builder | Collection | LengthAwarePaginator { + $this->before($query); + foreach ($this->allowed_columns as $column => $operators) { // ignore filter if not specified in params if (is_null($param = $this->request->query($column))) { @@ -101,9 +105,13 @@ final public function handle(Builder $query, Closure $next): Builder | Collectio } } + $this->after($query); + return $next($query); } + protected function after(Builder $query): void {} + private function addQueryFilter(Builder $query, string $column, string $operator, $value): void { // check if a method with the param name exists if (method_exists($this, $method = lcfirst((string) str($column)->studly()))) { diff --git a/src/ResourceOrders.php b/src/ResourceOrders.php index 40f13a2..7c9648e 100644 --- a/src/ResourceOrders.php +++ b/src/ResourceOrders.php @@ -30,7 +30,11 @@ final public function __construct( protected Request $request, ) {} + protected function before(Builder $query): void {} + final public function handle(Builder $query, Closure $next): Builder | Collection | LengthAwarePaginator { + $this->before($query); + // check if query param was not defined if (null === $order = $this->request->query('order')) { // add default sorting fields @@ -56,9 +60,13 @@ final public function handle(Builder $query, Closure $next): Builder | Collectio $this->addQueryOrder($query, $value[$direction], $direction); } + $this->after($query); + return $next($query); } + protected function after(Builder $query): void {} + private function clean(array &$order): void { sort($order);