-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathResolve.php
230 lines (204 loc) · 5.76 KB
/
Resolve.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<?php
namespace Abbasudo\Purity\Filters;
use Abbasudo\Purity\Exceptions\FieldNotSupported;
use Abbasudo\Purity\Exceptions\NoOperatorMatch;
use Closure;
use Exception;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
class Resolve
{
/**
* List of relations and the column.
*
* @var array
*/
private array $fields = [];
/**
* List of available filters.
*
* @var filterList
*/
private FilterList $filterList;
private Model $model;
/**
* @param FilterList $filterList
* @param Model $model
*/
public function __construct(FilterList $filterList, Model $model)
{
$this->filterList = $filterList;
$this->model = $model;
}
/**
* @param Builder $query
* @param string $field
* @param array|string $values
*
* @throws Exception
*
* @return void
*/
public function apply(Builder $query, string $field, array|string $values): void
{
if (!$this->safe(fn () => $this->validate([$field => $values]))) {
return;
}
$this->filter($query, $field, $values);
}
/**
* run functions with or without exception.
*
* @param Closure $closure
*
* @throws Exception
*
* @return bool
*/
private function safe(Closure $closure): bool
{
try {
$closure();
return true;
} catch (Exception $exception) {
if (config('purity.silent')) {
return false;
} else {
throw $exception;
}
}
}
/**
* @param array|string $values
*
* @return void
*/
private function validate(array|string $values = [])
{
if (empty($values) or is_string($values)) {
throw NoOperatorMatch::create($this->filterList->keys());
}
if (in_array(key($values), $this->filterList->keys())) {
return;
} else {
$this->validate(array_values($values)[0]);
}
}
/**
* Apply a single filter to the query builder instance.
*
* @param Builder $query
* @param string $field
* @param array|string|null $filters
*
* @throws Exception
*
* @return void
*/
private function filter(Builder $query, string $field, array|string|null $filters): void
{
// Ensure that the filter is an array
if (!is_array($filters)) {
$filters = [$filters];
}
// Resolve the filter using the appropriate strategy
if ($this->filterList->get($field) !== null) {
//call apply method of the appropriate filter class
$this->safe(fn () => $this->applyFilterStrategy($query, $field, $filters));
} else {
// If the field is not recognized as a filter strategy, it is treated as a relation
$this->safe(fn () => $this->applyRelationFilter($query, $field, $filters));
}
}
/**
* @param Builder $query
* @param string $operator
* @param array $filters
*
* @return void
*/
private function applyFilterStrategy(Builder $query, string $operator, array $filters): void
{
$filter = $this->filterList->get($operator);
$field = end($this->fields);
$callback = (new $filter($query, $field, $filters))->apply();
$this->filterRelations($query, $callback);
}
/**
* @param Builder $query
* @param Closure $callback
*
* @return void
*/
private function filterRelations(Builder $query, Closure $callback): void
{
array_pop($this->fields);
$this->applyRelations($query, $callback);
}
/**
* Resolve nested relations if any.
*
* @param Builder $query
* @param Closure $callback
*
* @return void
*/
private function applyRelations(Builder $query, Closure $callback): void
{
if (empty($this->fields)) {
// If there are no more filterable fields to resolve, apply the closure to the query builder instance
$callback($query);
} else {
// If there are still filterable fields to resolve, apply the closure to a sub-query
$this->relation($query, $callback);
}
}
/**
* @param Builder $query
* @param Closure $callback
*
* @return void
*/
private function relation(Builder $query, Closure $callback)
{
// remove last field until its empty
$field = array_shift($this->fields);
$query->whereHas($field, function ($subQuery) use ($callback) {
$this->applyRelations($subQuery, $callback);
});
}
/**
* @param Builder $query
* @param string $field
* @param array $filters
*
* @throws Exception
*
* @return void
*/
private function applyRelationFilter(Builder $query, string $field, array $filters): void
{
foreach ($filters as $subField => $subFilter) {
$relation = end($this->fields);
if ($relation !== false) {
$this->model = $this->model->$relation()->getRelated();
}
$this->validateField($field);
$this->fields[] = $this->model->getField($field);
$this->filter($query, $subField, $subFilter);
}
array_pop($this->fields);
}
/**
* @param string $field
*
* @return void
*/
private function validateField(string $field): void
{
$availableFields = $this->model->availableFields();
if (!in_array($field, $availableFields)) {
throw FieldNotSupported::create($field, $this->model::class, $availableFields);
}
}
}