Skip to content

Commit

Permalink
bind resolve to custom resolver and model
Browse files Browse the repository at this point in the history
  • Loading branch information
abbasudo committed Oct 13, 2024
1 parent 2a81796 commit 687973c
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/Traits/Filterable.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
use ReflectionClass;

/**
* List of available filters, can be set on the model otherwise it will be read from config.
* The List of available filters can be set on the model otherwise it will be read from config.
*
* @property array $filters
*
* List of available fields, if not declared will accept every thing.
* List of available fields, if not declared, will accept everything.
* @property array $filterFields
*
* Fields will restrict to defined filters.
Expand Down Expand Up @@ -64,7 +64,7 @@ public function scopeFilter(Builder $query, array|null $params = null): Builder
// Apply each filter to the query builder instance

foreach ($params as $field => $value) {
app($this->getFilterResolver())->apply($query, $field, $value);
app(Resolve::class)->apply($query, $field, $value);
}

return $query;
Expand All @@ -81,7 +81,10 @@ private function bootFilter(): void
return (new FilterList())->only($this->getFilters());
});

app()->when($this->getFilterResolver())->needs(Model::class)->give(fn () => $this);
app()->bind(Resolve::class, function () {
$resolver = $this->getFilterResolver();
return new $resolver(app(FilterList::class), $this);
});
}

/**
Expand Down Expand Up @@ -210,8 +213,7 @@ public function getRestrictedFilters(): array
foreach ($this->restrictedFilters ?? $this->filterFields ?? [] as $key => $value) {
if (is_int($key) && Str::contains($value, ':')) {
$tKey = str($value)->before(':')->squish()->toString();
$tValue = str($value)->after(':')->squish()->explode(',')->all();
$restrictedFilters[$tKey] = $tValue;
$restrictedFilters[$tKey] = str($value)->after(':')->squish()->explode(',')->all();
}
if (is_string($key)) {
$restrictedFilters[$key] = Arr::wrap($value);
Expand Down

0 comments on commit 687973c

Please sign in to comment.