Skip to content

Commit 9a7cb63

Browse files
committed
[Feature-WIP] Fix bugs from previous commit
Identified these bugs when testing out the changes in the demo Laravel JSON API application. At the same time, simplified the paginator classes so they are directly injected with the encoding parameters. This is now possible because the encoding parameters for the current request are registered directly in the service container.
1 parent 7c7ef0e commit 9a7cb63

File tree

7 files changed

+40
-42
lines changed

7 files changed

+40
-42
lines changed

src/Http/Middleware/BootJsonApi.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public function handle($request, Closure $next, $namespace)
8585
$serverRequest = $this->container->make(ServerRequestInterface::class);
8686

8787
/** Parse headers */
88-
$initiator->doContentNegotiation($api->getCodecMatcher(), $serverRequest);
88+
$initiator->doContentNegotiation($api, $serverRequest);
8989

9090
/** Set the encoding parameters into the container */
9191
$parameters = $initiator->parseParameters($serverRequest);

src/Http/Middleware/CreateRequest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ private function record($resourceType, $resourceId)
112112
{
113113
/** @var StoreInterface $store */
114114
$store = $this->container->make(StoreInterface::class);
115-
$identifier = new ResourceIdentifier($resourceType, $resourceId);
115+
$identifier = ResourceIdentifier::create($resourceType, $resourceId);
116116

117117
return $store->find($identifier);
118118
}

src/Http/Requests/AbstractRequest.php renamed to src/Http/Requests/AbstractRequestHandler.php

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
use CloudCreativity\JsonApi\Contracts\Authorizer\AuthorizerInterface;
2222
use CloudCreativity\JsonApi\Contracts\Http\RequestInterpreterInterface;
23+
use CloudCreativity\JsonApi\Contracts\Validators\FilterValidatorInterface;
2324
use CloudCreativity\JsonApi\Contracts\Validators\ValidatorProviderInterface;
2425
use CloudCreativity\LaravelJsonApi\Contracts\Http\Requests\RequestHandlerInterface;
2526
use CloudCreativity\LaravelJsonApi\Contracts\Pagination\PageParameterHandlerInterface;
@@ -30,7 +31,7 @@
3031
* Class Request
3132
* @package CloudCreativity\LaravelJsonApi
3233
*/
33-
abstract class AbstractRequest implements RequestHandlerInterface
34+
abstract class AbstractRequestHandler implements RequestHandlerInterface
3435
{
3536

3637
use ChecksUrlParameters,
@@ -99,13 +100,13 @@ abstract class AbstractRequest implements RequestHandlerInterface
99100

100101
/**
101102
* AbstractRequest constructor.
102-
* @param ValidatorProviderInterface $validators
103103
* @param AuthorizerInterface|null $authorizer
104+
* @param ValidatorProviderInterface|null $validators
104105
* @param HttpFactoryInterface|null $factory
105106
*/
106107
public function __construct(
107-
ValidatorProviderInterface $validators,
108-
AuthorizerInterface $authorizer,
108+
AuthorizerInterface $authorizer = null,
109+
ValidatorProviderInterface $validators = null,
109110
HttpFactoryInterface $factory = null
110111
) {
111112
$this->validators = $validators;
@@ -118,8 +119,6 @@ public function __construct(
118119
*/
119120
public function handle(RequestInterpreterInterface $interpreter, JsonApiRequest $request)
120121
{
121-
$resourceType = $this->getResourceType();
122-
123122
/** Check that a resource id has resolved to a record */
124123
$this->checkResourceId($interpreter, $request);
125124

@@ -129,15 +128,17 @@ public function handle(RequestInterpreterInterface $interpreter, JsonApiRequest
129128
}
130129

131130
/** Check request parameters are acceptable */
132-
$filterValidator = $interpreter->isIndex() ?
133-
$this->validators->filterResources($resourceType) : null;
134-
$this->checkQueryParameters($this->factory, $request, $filterValidator);
131+
$this->checkQueryParameters($this->factory, $request, $this->filterValidator());
135132

136133
/** Authorize the request */
137-
$this->authorize($interpreter, $this->authorizer, $request);
134+
if ($this->authorizer) {
135+
$this->authorize($interpreter, $this->authorizer, $request);
136+
}
138137

139138
/** Check the document content is acceptable */
140-
$this->checkDocumentIsAcceptable($this->validators, $interpreter, $request);
139+
if ($this->validators) {
140+
$this->checkDocumentIsAcceptable($this->validators, $interpreter, $request);
141+
}
141142
}
142143

143144
/**
@@ -199,4 +200,11 @@ protected function allowedPagingParameters()
199200
return $param->getAllowedPagingParameters();
200201
}
201202

203+
/**
204+
* @return FilterValidatorInterface|null
205+
*/
206+
private function filterValidator()
207+
{
208+
return $this->validators ? $this->validators->filterResources($this->getResourceType()) : null;
209+
}
202210
}

src/Pagination/PageParameterHandler.php

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
namespace CloudCreativity\LaravelJsonApi\Pagination;
2020

2121
use CloudCreativity\LaravelJsonApi\Contracts\Pagination\PageParameterHandlerInterface;
22-
use CloudCreativity\LaravelJsonApi\Services\JsonApiService;
2322
use Illuminate\Support\Arr;
23+
use Neomerx\JsonApi\Contracts\Encoder\Parameters\EncodingParametersInterface;
2424

2525
/**
2626
* Class PageParameterHandler
@@ -29,10 +29,7 @@
2929
class PageParameterHandler implements PageParameterHandlerInterface
3030
{
3131

32-
/**
33-
* @var JsonApiService
34-
*/
35-
private $service;
32+
private $parameters;
3633

3734
/**
3835
* @var PaginatorConfiguration
@@ -41,12 +38,12 @@ class PageParameterHandler implements PageParameterHandlerInterface
4138

4239
/**
4340
* PageParameter constructor.
44-
* @param JsonApiService $service
41+
* @param EncodingParametersInterface $parameters
4542
* @param PaginatorConfiguration $config
4643
*/
47-
public function __construct(JsonApiService $service, PaginatorConfiguration $config)
44+
public function __construct(EncodingParametersInterface $parameters, PaginatorConfiguration $config)
4845
{
49-
$this->service = $service;
46+
$this->parameters = $parameters;
5047
$this->config = $config;
5148
}
5249

@@ -124,9 +121,7 @@ public function getAllowedPagingParameters()
124121
protected function getParams()
125122
{
126123
return (array) $this
127-
->service
128-
->getRequest()
129-
->getEncodingParameters()
124+
->parameters
130125
->getPaginationParameters();
131126
}
132127

src/Pagination/Paginator.php

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@
2020

2121
use CloudCreativity\LaravelJsonApi\Contracts\Document\LinkFactoryInterface;
2222
use CloudCreativity\LaravelJsonApi\Contracts\Pagination\PaginatorInterface;
23-
use CloudCreativity\LaravelJsonApi\Services\JsonApiService;
2423
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
2524
use Illuminate\Contracts\Pagination\Paginator as IlluminatePaginator;
2625
use Neomerx\JsonApi\Contracts\Document\LinkInterface;
26+
use Neomerx\JsonApi\Contracts\Encoder\Parameters\EncodingParametersInterface;
2727
use Neomerx\JsonApi\Contracts\Encoder\Parameters\SortParameterInterface;
2828
use Neomerx\JsonApi\Contracts\Http\Query\QueryParametersParserInterface;
2929

@@ -35,9 +35,9 @@ class Paginator implements PaginatorInterface
3535
{
3636

3737
/**
38-
* @var JsonApiService
38+
* @var EncodingParametersInterface
3939
*/
40-
private $service;
40+
private $parameters;
4141

4242
/**
4343
* @var LinkFactoryInterface
@@ -51,16 +51,16 @@ class Paginator implements PaginatorInterface
5151

5252
/**
5353
* Paginator constructor.
54-
* @param JsonApiService $service
54+
* @param EncodingParametersInterface $parameters
5555
* @param LinkFactoryInterface $links
5656
* @param PaginatorConfiguration $config
5757
*/
5858
public function __construct(
59-
JsonApiService $service,
59+
EncodingParametersInterface $parameters,
6060
LinkFactoryInterface $links,
6161
PaginatorConfiguration $config
6262
) {
63-
$this->service = $service;
63+
$this->parameters = $parameters;
6464
$this->links = $links;
6565
$this->config = $config;
6666
}
@@ -137,16 +137,11 @@ public function addLinks(IlluminatePaginator $results, array $links = [])
137137
*/
138138
protected function buildParams()
139139
{
140-
$encodingParameters = $this
141-
->service
142-
->getRequest()
143-
->getEncodingParameters();
144-
145140
return array_filter([
146141
QueryParametersParserInterface::PARAM_FILTER =>
147-
$encodingParameters->getFilteringParameters(),
142+
$this->parameters->getFilteringParameters(),
148143
QueryParametersParserInterface::PARAM_SORT =>
149-
$this->buildSortParams((array) $encodingParameters->getSortParameters())
144+
$this->buildSortParams((array) $this->parameters->getSortParameters())
150145
]);
151146
}
152147

src/ServiceProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use CloudCreativity\JsonApi\Contracts\Store\StoreInterface;
2828
use CloudCreativity\JsonApi\Contracts\Utils\ConfigurableInterface;
2929
use CloudCreativity\JsonApi\Contracts\Utils\ReplacerInterface;
30+
use CloudCreativity\JsonApi\Contracts\Validators\ValidatorFactoryInterface as BaseValidatorFactoryInterface;
3031
use CloudCreativity\JsonApi\Http\ApiFactory;
3132
use CloudCreativity\JsonApi\Repositories\CodecMatcherRepository;
3233
use CloudCreativity\JsonApi\Repositories\ErrorRepository;
@@ -232,6 +233,7 @@ protected function bindResponses()
232233
protected function bindValidatorFactory()
233234
{
234235
$this->app->singleton(ValidatorFactoryInterface::class, ValidatorFactory::class);
236+
$this->app->alias(ValidatorFactoryInterface::class, BaseValidatorFactoryInterface::class);
235237
}
236238

237239
/**

src/Validators/AbstractValidatorProvider.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ public function updateResource($resourceType, $resourceId, $record)
144144
public function modifyRelationship($resourceType, $resourceId, $relationshipName, $record)
145145
{
146146
$validator = $this
147-
->resourceRelationships($resourceType, $resourceId, $record)
147+
->resourceRelationships($resourceType, $record)
148148
->get($relationshipName);
149149

150150
return $this->factory->relationshipDocument($validator);
@@ -195,7 +195,7 @@ protected function extractAttributes(ResourceInterface $resource, $resourceType,
195195

196196
/**
197197
* @param string $resourceType
198-
* @param string|int|null $resourceId
198+
* @param string|null $resourceId
199199
* @param object|null $record
200200
* @return ResourceValidatorInterface
201201
*/
@@ -258,13 +258,11 @@ protected function attributeCustomAttributes($resourceType, $record = null)
258258
*
259259
* @param string $resourceType
260260
* the resource type being validated.
261-
* @param string $resourceId
262-
* the resource id that is being validated.
263261
* @param object|null $record
264262
* the record being updated, or null if it is a create request.
265263
* @return RelationshipsValidatorInterface
266264
*/
267-
protected function resourceRelationships($resourceType, $resourceId, $record = null)
265+
protected function resourceRelationships($resourceType, $record = null)
268266
{
269267
$validator = $this->factory->relationships();
270268
$this->relationshipRules($validator, $resourceType, $record);

0 commit comments

Comments
 (0)