8000 [Feature-WIP] Remove search and add pagination validator · volldigital/laravel-json-api@890481d · GitHub
[go: up one dir, main page]

Skip to content 8000

Commit 890481d

Browse files
committed
[Feature-WIP] Remove search and add pagination validator
Search has been removed and merged into the Eloquent adapter. A pagination validator has been added so that pagination parameters can now be validated using a Laravel validator. This allows for the max range of the size of a page to be set in the validator, for example by using 'integer|between:1,50` as the page size rule.
1 parent d1981e0 commit 890481d

17 files changed

+627
-660
lines changed

config/json-api-errors.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@
145145
* parameter and error detail will be set using the message bag that the Laravel validator returns,
146146
* and a 400 response status will be set.
147147
*/
148-
V::FILTER_PARAMETERS_MESSAGES => [
148+
V::QUERY_PARAMETERS_MESSAGES => [
149149
Error::TITLE => 'Invalid Filter',
150150
],
151151

src/Contracts/Validators/FilterValidatorInterface.php

Lines changed: 0 additions & 38 deletions
This file was deleted.

src/Contracts/Validators/ValidatorErrorFactoryInterface.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ interface ValidatorErrorFactoryInterface extends BaseInterface
3131
{
3232

3333
const STATUS_INVALID_ATTRIBUTES = Response::HTTP_UNPROCESSABLE_ENTITY;
34-
const STATUS_INVALID_FILTERS = Response::HTTP_BAD_REQUEST;
34+
const STATUS_INVALID_PARAMETERS = Response::HTTP_BAD_REQUEST;
3535

3636
/**
3737
* @param MessageBag $messageBag
@@ -48,8 +48,9 @@ public function resourceInvalidAttributesMessages(
4848

4949
/**
5050
* @param MessageBag $messages
51+
* @param string $queryParam
5152
* @param int $statusCode
5253
* @return ErrorInterface[]
5354
*/
54-
public function filterParametersMessages(MessageBag $messages, $statusCode = self::STATUS_INVALID_FILTERS);
55+
public function queryParametersMessages(MessageBag $messages, $queryParam, $statusCode = self::STATUS_INVALID_PARAMETERS);
5556
}

src/Contracts/Validators/ValidatorFactoryInterface.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22

33
/**
4-
* Copyright 2016 Cloud Creativity Limited
4+
* Copyright 2017 Cloud Creativity Limited
55
*
66
* Licensed under the Apache License, Version 2.0 (the "License");
77
* you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@
1919
namespace CloudCreativity\LaravelJsonApi\Contracts\Validators;
2020

2121
use CloudCreativity\JsonApi\Contracts\Validators\AttributesValidatorInterface;
22+
use CloudCreativity\JsonApi\Contracts\Validators\QueryValidatorInterface;
2223
use CloudCreativity\JsonApi\Contracts\Validators\ValidatorFactoryInterface as BaseInterface;
2324

2425
/**
@@ -55,12 +56,29 @@ public function attributes(
5556
* @param array $messages
5657
* @param array $customAttributes
5758
* @param callable|null $callback
58-
* @return FilterValidatorInterface
59+
* @return QueryValidatorInterface
5960
*/
6061
public function filterParams(
6162
array $rules,
6263
array $messages = [],
6364
array $customAttributes = [],
6465
callable $callback = null
6566
);
67+
68+
69+
/**
70+
* Get a pagination parameters validator.
71+
*
72+
* @param array $rules
73+
* @param array $messages
74+
* @param array $customAttributes
75+
* @param callable|null $callback
76+
* @return QueryValidatorInterface
77+
*/
78+
public function paginationParams(
79+
array $rules,
80+
array $messages = [],
81+
array $customAttributes = [],
82+
callable $callback = null
83+
);
6684
}

src/Http/Controllers/EloquentController.php

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,36 +19,36 @@
1919
namespace CloudCreativity\LaravelJsonApi\Http\Controllers;
2020

2121
use Closure;
22+
use CloudCreativity\JsonApi\Contracts\Http\ApiInterface;
2223
use CloudCreativity\JsonApi\Contracts\Http\Requests\RequestInterface as JsonApiRequest;
2324
use CloudCreativity\JsonApi\Contracts\Hydrator\HydratesRelatedInterface;
2425
use CloudCreativity\JsonApi\Contracts\Hydrator\HydratorInterface;
2526
use CloudCreativity\JsonApi\Contracts\Object\ResourceInterface;
27+
use CloudCreativity\JsonApi\Contracts\Store\StoreInterface;
2628
use CloudCreativity\JsonApi\Exceptions\RuntimeException;
27-
use CloudCreativity\LaravelJsonApi\Contracts\Search\SearchInterface;
29+
use CloudCreativity\LaravelJsonApi\Document\GeneratesLinks;
30+
use CloudCreativity\LaravelJsonApi\Http\Responses\ReplyTrait;
2831
use CloudCreativity\LaravelJsonApi\Utils\Str;
2932
use Exception;
30-
use Illuminate\Contracts\Pagination\Paginator;
31-
use Illuminate\Database\Eloquent\Collection;
3233
use Illuminate\Database\Eloquent\Model;
3334
use Illuminate\Http\Response;
3435

3536
/**
3637
* Class EloquentController
38+
*
3739
* @package CloudCreativity\LaravelJsonApi
3840
*/
39-
class EloquentController extends JsonApiController
41+
class EloquentController
4042
{
4143

44+
use ReplyTrait,
45+
GeneratesLinks;
46+
4247
/**
4348
* @var HydratorInterface
4449
*/
4550
protected $hydrator;
4651

47-
/**
48-
* @var SearchInterface
49-
*/
50-
protected $search;
51-
5252
/**
5353
* Map of URI relationship names to model relationship keys.
5454
*
@@ -71,25 +71,21 @@ class EloquentController extends JsonApiController
7171
*
7272
* @param Model $model
7373
* @param HydratorInterface|null $hydrator
74-
* @param SearchInterface|null $search
7574
*/
76-
public function __construct(
77-
Model $model,
78-
HydratorInterface $hydrator = null,
79-
SearchInterface $search = null
80-
) {
75+
public function __construct(Model $model, HydratorInterface $hydrator = null)
76+
{
8177
$this->model = $model;
8278
$this->hydrator = $hydrator;
83-
$this->search = $search;
8479
}
8580

8681
/**
82+
* @param ApiInterface $api
8783
* @param JsonApiRequest $request
8884
* @return Response
8985
*/
90-
public function index(JsonApiRequest $request)
86+
public function index(ApiInterface $api, JsonApiRequest $request)
9187
{
92-
$result = $this->search($request);
88+
$result = $this->search($api->getStore(), $request);
9389

9490
if ($result instanceof Response) {
9591
return $result;
@@ -203,17 +199,11 @@ public function readRelationship(JsonApiRequest $request)
203199

204200
/**
205201
* @param JsonApiRequest $request
206-
* @return Paginator|Collection|Model|Response|null
202+
* @return mixed
207203
*/
208-
protected function search(JsonApiRequest $request)
204+
protected function search(StoreInterface $store, JsonApiRequest $request)
209205
{
210-
if (!$this->search) {
211-
return $this->notImplemented();
212-
}
213-
214-
$builder = $this->model->newQuery();
215-
216-
return $this->search->search($builder, $request->getParameters());
206+
return $store->query($request->getResourceType(), $request->getParameters());
217207
}
218208

219209
/**
@@ -422,6 +412,16 @@ protected function getRecord(JsonApiRequest $request)
422412
return $record;
423413
}
424414

415+
/**
416+
* @return Response
417+
*/
418+
protected function notImplemented()
419+
{
420+
return $this
421+
->reply()
422+
->statusCode(Response::HTTP_NOT_IMPLEMENTED);
423+
}
424+
425425
/**
426426
* @return Response
427427
*/

src/Http/Query/ExtendedQueryChecker.php

Lines changed: 0 additions & 58 deletions
This file was deleted.

0 commit comments

Comments
 (0)
0