|
17 | 17 | use JsonApiPhp\JsonApi\Link\PrevLink;
|
18 | 18 | use Psr\Http\Message\ResponseInterface;
|
19 | 19 | use Psr\Http\Message\ServerRequestInterface as Request;
|
20 |
| -use Tobyz\JsonApiServer\Adapter\AdapterInterface; |
| 20 | +use Tobyz\JsonApiServer\Context; |
21 | 21 | use Tobyz\JsonApiServer\Exception\BadRequestException;
|
22 | 22 | use Tobyz\JsonApiServer\Exception\ForbiddenException;
|
23 | 23 | use Tobyz\JsonApiServer\JsonApi;
|
24 | 24 | use Tobyz\JsonApiServer\ResourceType;
|
25 | 25 | use Tobyz\JsonApiServer\Schema\Attribute;
|
26 |
| -use Tobyz\JsonApiServer\Context; |
27 |
| -use Tobyz\JsonApiServer\Schema\HasMany; |
28 |
| -use Tobyz\JsonApiServer\Schema\HasOne; |
29 | 26 | use Tobyz\JsonApiServer\Schema\Meta;
|
30 | 27 | use Tobyz\JsonApiServer\Serializer;
|
31 | 28 | use function Tobyz\JsonApiServer\evaluate;
|
@@ -59,13 +56,16 @@ public function handle(Context $context): ResponseInterface
|
59 | 56 |
|
60 | 57 | $query = $adapter->newQuery($context);
|
61 | 58 |
|
62 |
| - run_callbacks($schema->getListeners('scope'), [$query, $context]); |
| 59 | + $this->resource->scope($query, $context); |
63 | 60 |
|
64 | 61 | $include = $this->getInclude($context);
|
65 | 62 |
|
66 | 63 | [$offset, $limit] = $this->paginate($query, $context);
|
67 | 64 | $this->sort($query, $context);
|
68 |
| - $this->filter($query, $context); |
| 65 | + |
| 66 | + if ($filter = $context->getRequest()->getQueryParams()['filter'] ?? null) { |
| 67 | + $this->resource->filter($query, $filter, $context); |
| 68 | + } |
69 | 69 |
|
70 | 70 | run_callbacks($schema->getListeners('listing'), [$query, $context]);
|
71 | 71 |
|
@@ -234,71 +234,4 @@ private function paginate($query, Context $context)
|
234 | 234 |
|
235 | 235 | return [$offset, $limit];
|
236 | 236 | }
|
237 |
| - |
238 |
| - private function filter($query, Context $context) |
239 |
| - { |
240 |
| - if (! $filter = $context->getRequest()->getQueryParams()['filter'] ?? null) { |
241 |
| - return; |
242 |
| - } |
243 |
| - |
244 |
| - if (! is_array($filter)) { |
245 |
| - throw new BadRequestException('filter must be an array', 'filter'); |
246 |
| - } |
247 |
| - |
248 |
| - $schema = $this->resource->getSchema(); |
249 |
| - $adapter = $this->resource->getAdapter(); |
250 |
| - $filters = $schema->getFilters(); |
251 |
| - $fields = $schema->getFields(); |
252 |
| - |
253 |
| - foreach ($filter as $name => $value) { |
254 |
| - if ($name === 'id') { |
255 |
| - $adapter->filterByIds($query, explode(',', $value)); |
256 |
| - continue; |
257 |
| - } |
258 |
| - |
259 |
| - if (isset($filters[$name]) && evaluate($filters[$name]->getVisible(), [$context])) { |
260 |
| - $filters[$name]->getCallback()($query, $value, $context); |
261 |
| - continue; |
262 |
| - } |
263 |
| - |
264 |
| - if (isset($fields[$name]) && evaluate($fields[$name]->getFilterable(), [$context])) { |
265 |
| - if ($fields[$name] instanceof Attribute) { |
266 |
| - $this->filterByAttribute($adapter, $query, $fields[$name], $value); |
267 |
| - } elseif ($fields[$name] instanceof HasOne) { |
268 |
| - $value = array_filter(explode(',', $value)); |
269 |
| - $adapter->filterByHasOne($query, $fields[$name], $value); |
270 |
| - } elseif ($fields[$name] instanceof HasMany) { |
271 |
| - $value = array_filter(explode(',', $value)); |
272 |
| - $adapter->filterByHasMany($query, $fields[$name], $value); |
273 |
| - } |
274 |
| - continue; |
275 |
| - } |
276 |
| - |
277 |
| - throw new BadRequestException("Invalid filter [$name]", "filter[$name]"); |
278 |
| - } |
279 |
| - } |
280 |
| - |
281 |
| - private function filterByAttribute(AdapterInterface $adapter, $query, Attribute $attribute, $value) |
282 |
| - { |
283 |
| - if (preg_match('/(.+)\.\.(.+)/', $value, $matches)) { |
284 |
| - if ($matches[1] !== '*') { |
285 |
| - $adapter->filterByAttribute($query, $attribute, $value, '>='); |
286 |
| - } |
287 |
| - if ($matches[2] !== '*') { |
288 |
| - $adapter->filterByAttribute($query, $attribute, $value, '<='); |
289 |
| - } |
290 |
| - |
291 |
| - return; |
292 |
| - } |
293 |
| - |
294 |
| - foreach (['>=', '>', '<=', '<'] as $operator) { |
295 |
| - if (strpos($value, $operator) === 0) { |
296 |
| - $adapter->filterByAttribute($query, $attribute, substr($value, strlen($operator)), $operator); |
297 |
| - |
298 |
| - return; |
299 |
| - } |
300 |
| - } |
301 |
| - |
302 |
| - $adapter->filterByAttribute($query, $attribute, $value); |
303 |
| - } |
304 | 237 | }
|
0 commit comments