Skip to content

Commit

Permalink
Merge pull request #4 from hschimpf/1.x-dev
Browse files Browse the repository at this point in the history
Release the ability to customize the query with callbacks
  • Loading branch information
hschimpf committed Aug 18, 2023
2 parents 3c0abbc + 225f2cd commit a3f8941
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/ResourceFilters.php
Original file line number Diff line number Diff line change
Expand Up @@ -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))) {
Expand Down Expand Up @@ -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()))) {
Expand Down
8 changes: 8 additions & 0 deletions src/ResourceOrders.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);

Expand Down

0 comments on commit a3f8941

Please sign in to comment.