-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAbstractFilter.php
170 lines (144 loc) · 4.98 KB
/
AbstractFilter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
namespace FreeBuu\LaravelFilterable;
use FreeBuu\LaravelFilterable\Params\FilterParam;
use FreeBuu\LaravelFilterable\Params\FilterCaseEnum;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
/**
* @mixin Builder
*/
abstract class AbstractFilter implements Arrayable
{
protected const PARAM_LIMIT = 'limit';
protected const PARAM_OFFSET = 'offset';
protected const FIELD_DELIMITER = '_';
protected const FIELD_VALUE_DELIMITER = '__';
public static int $defaultMaxLimit = 30;
protected ?int $maxLimit = null;
/** @var callable[] */
private array $queryCallbacks = [];
/** @var FilterParam[] */
private array $filters = [];
final private function __construct(
protected readonly Builder $builder,
protected readonly Request $request
) {
}
public static function create(Builder $builder, Request $request): static
{
return new static($builder, $request);
}
final public function apply(Builder $builder): void
{
$this->parseFilters();
$this->prepareQueryForFiltering($builder);
$this->applyFilters($builder);
$this->applyQueryCallback($builder);
$this->finalize($builder);
}
private function finalize(Builder $builder): void
{
$builder
->offset($this->request->input(self::PARAM_OFFSET, 0))
->limit($this->getLimit());
}
private function parseFilters(): void
{
$this->filters = [];
foreach ($this->request->query() as $param => $value) {
[$case, $field, $fieldValue] = $this->parseQueryParam($param);
if (!$case || !$case = FilterCaseEnum::tryFrom($case)) {
continue;
}
if ($case === FilterCaseEnum::FILTER) {
if (!method_exists($this, $method = 'filter' . Str::studly($field))) {
continue;
}
$field = $this->$method(...);
}
$filter = new FilterParam($case, $field, $value, $fieldValue);
if (!$this->isFilterSuitable($filter)) {
continue;
}
$this->filters[] = $filter;
}
}
private function parseQueryParam(string $param): ?array
{
$case = strtok($param, self::FIELD_DELIMITER);
if ($case === $param) {
return null;
}
$field = substr($param, strlen($case . self::FIELD_DELIMITER));
$fieldValue = null;
$fieldValueCheck = explode(self::FIELD_VALUE_DELIMITER, $field);
if (count($fieldValueCheck) === 2) {
$field = $fieldValueCheck[0];
$fieldValue = $fieldValueCheck[1];
}
return [$case, $field, $fieldValue];
}
private function applyFilters(Builder $builder): void
{
foreach ($this->filters as $filter) {
$filter->apply($builder);
}
}
abstract protected function getFilterableFields(FilterCaseEnum $case): array;
protected function prepareQueryForFiltering(Builder $builder): void
{
}
private function getLimit(): int
{
$maxLimit = $this->maxLimit ?? static::$defaultMaxLimit;
return $this->request->input(self::PARAM_LIMIT, 0) > $maxLimit
? $maxLimit
: $this->request->input(self::PARAM_LIMIT, $maxLimit);
}
final public function __call(string $name, array $arguments): static
{
return $this->addQueryCallback(fn ($builder) => $builder->$name(...$arguments));
}
final public function addQueryCallback(callable $queryCallback): static
{
$this->queryCallbacks[] = $queryCallback;
return $this;
}
private function applyQueryCallback(Builder $builder): void
{
foreach ($this->queryCallbacks as $callback) {
$callback($builder);
}
}
final public function response(string|callable $resource = null): FilteredResponse
{
$this->apply($this->builder);
$response = new FilteredResponse($this->builder);
$response->setResource($resource);
return $response;
}
final public function toArray(): array
{
return $this->response()->toArray();
}
private function isFilterSuitable(FilterParam $filterParam): bool
{
$fields = $this->getFilterableFields($filterParam->case);
if ($filterParam->case === FilterCaseEnum::FILTER) {
return true;
}
if ($filterParam->fieldValueMandatory()) {
if (!$filterParam->fieldValue) {
return false;
}
if (!$fieldValues = $fields[$filterParam->field] ?? null) {
return false;
}
return is_array($fieldValues) && array_is_list($fieldValues) && in_array($filterParam->fieldValue, $fieldValues);
} else {
return array_is_list($fields) && in_array($filterParam->field, $fields);
}
}
}