Skip to content

Commit 989c743

Browse files
committed
[Feature-WIP] Upgrade dependencies and change implementations
Update the cloudcreativity/json-api and neomerx/json-api dependencies and make immediate changes to classes that have broken as a result of the new versions. The changes are documented in the upgrade file but are: - Removing abstract hydrator - Updating abstract validator provider because of interface changes.
1 parent 2a1e896 commit 989c743

File tree

6 files changed

+316
-142
lines changed

6 files changed

+316
-142
lines changed

UPGRADE.md

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,147 @@
22

33
This file provides notes on how to upgrade between versions.
44

5+
## v0.4 to v0.5
6+
7+
### Authorizers
8+
9+
#### Abstract Authorizer
10+
11+
If you are extending the `AbstractAuthorizer` class and implementing your own constructor, you will need to
12+
inject an instance of `CloudCreativity\JsonApi\Contracts\Repositories\ErrorRepositoryInterface` into the parent
13+
constructor. This change means you can now add errors to the error collection on your authorizer using the string
14+
keys for error objects held within your `json-api-errors` config file. For example:
15+
16+
``` php
17+
public function canRead($record, EncodingParametersInterface $parameters)
18+
{
19+
if (!\Auth::check()) {
20+
$this->addError('access-denied');
21+
return false;
22+
}
23+
24+
return true;
25+
}
26+
```
27+
28+
Note that the recommended way to create error objects is via the error repository (which holds your error config)
29+
because it provides opportunities to add in translation etc of errors in the future.
30+
31+
You will also need to implement the `canModifyRelationship` method. This was previously implemented in the abstract
32+
class but the previous implementation no longer works because of the change below.
33+
34+
#### Update Authorization
35+
36+
Update authorization can now access the resource sent by the client. At the point the authorizer is invoked, the
37+
resource will have been validated to check that it complies with the JSON API spec but it will **not** have been
38+
checked that it is valid according to your business logic - i.e. attributes and relationships will not have
39+
been validated against the specific rules for that resource type.
40+
41+
Change the `canUpdate` method from this:
42+
43+
``` php
44+
canUpdate($record, EncodingParametersInterface $parameters)
45+
```
46+
47+
to:
48+
49+
``` php
50+
// CloudCreativity\JsonApi\Contracts\Object\ResourceInterface;
51+
canUpdate(
52+
$record,
53+
ResourceInterface $recource,
54+
EncodingParametersInterface $parameters
55+
)
56+
```
57+
58+
#### Modify Relationship Authorization
59+
60+
Relationship modification authroization can now access the relationship sent by the client, as per the update
61+
authorization changes above.
62+
63+
Change the `canModifyRelationship` method from this:
64+
65+
``` php
66+
public function canModifyRelationship(
67+
$relationshipKey,
68+
$record,
69+
EncodingParametersInterface $parameters
70+
)
71+
```
72+
73+
to:
74+
75+
``` php
76+
// CloudCreativity\JsonApi\Contracts\Object\RelationshipInterface
77+
public function canModifyRelationship(
78+
$relationshipKey,
79+
$record,
80+
RelationshipInterface $relationship,
81+
EncodingParametersInterface $parameters
82+
)
83+
```
84+
85+
### Hydrators
86+
87+
The namespace of the `AbstractHydrator` has changed from `CloudCreativity\LaravelJsonApi\Hydrator\AbstractHydrator`
88+
to `CloudCreativity\JsonApi\Hydrator\AbstractHydrator`.
89+
90+
### Validator Providers
91+
92+
The classes that provide validators for individual resource types generally extend `AbstractValidatorProvider`. This
93+
now receives the resource type that is being validated into its public methods, and this is now passed down through
94+
the internal methods. You'll therefore need to make the changes described below.
95+
96+
This change has been made so that a single validator provider can be used for multiple resource types if desired. In
97+
general you are likely to keep a validator provider per resource type (because attribute and relationship rules will
98+
be specific to a resource type), this has given us the capability to implement a generic validator provider capable of
99+
validating any resource type according to the JSON API spec.
100+
101+
#### Constructor
102+
103+
If you have implemented a constructor, you will need to type hint the following interface and pass it to the parent
104+
constructor:
105+
106+
```
107+
CloudCreativity\LaravelJsonApi\Contracts\Validators\ValidatorFactoryInterface
108+
```
109+
110+
#### Resource Type Property
111+
112+
You can remove the `$resourceType` property as it is no longer required.
113+
114+
#### Method Changes
115+
116+
You'll need to adjust the signature of the following abstract methods, from:
117+
118+
``` php
119+
attributeRules($record = null)
120+
relationshipRules(RelationshipsValidatorInterface $relationships, $record = null)
121+
filterRules()
122+
```
123+
124+
to:
125+
126+
``` php
127+
attributeRules($resourceType, $record = null)
128+
relationshipRules(RelationshipsValidatorInterface $relationships, $resourceType, $record = null)
129+
filterRules($resourceType)
130+
```
131+
132+
The `filterRules` method is actually no longer abstract, so if you are returning an empty array from it you can delete
133+
it.
134+
135+
The signatures of other `protected` methods have also changed to pass down this additional argument. If you have
136+
implemented any other methods, check the abstract class for the new argument order.
137+
138+
### Custom Validator Classes
139+
140+
If you have written any custom validator classes, you will need to refer to the `v0.5` to `v0.6` notes here:
141+
[cloudcreativty/json-api Upgrade Notes](https://github.com/cloudcreativity/json-api/blob/feature/v0.6/UPGRADE.md)
142+
143+
By custom validators, we mean the validators that validate individual parts of a JSON API document. You are unlikely
144+
to have done this!
145+
5146
## v0.3 to v0.4
6147

7148
This was a substantial reworking of the package based on our experience of using it in production environments.

composer.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@
2323
],
2424
"require": {
2525
"php": ">=5.5.0",
26-
"laravel/framework": "5.1.*|5.2.*",
27-
"cloudcreativity/json-api": "^0.5.2",
26+
"laravel/framework": "5.1.*|5.2.*|5.3.*",
27+
"neomerx/json-api": "^0.8.9",
28+
"cloudcreativity/json-api": "0.6.x-dev",
2829
"symfony/psr-http-message-bridge": "^0.2.0",
2930
"zendframework/zend-diactoros": "^1.3"
3031
},
@@ -41,5 +42,10 @@
4142
"psr-4": {
4243
"CloudCreativity\\LaravelJsonApi\\": "test/"
4344
}
45+
},
46+
"extra": {
47+
"branch-alias": {
48+
"dev-feature/v0.5": "0.5.x-dev"
49+
}
4450
}
4551
}

src/Http/Requests/AbstractRequest.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ protected function authorizeBeforeValidation()
258258
$authorized = $this->authorizer->canRead($this->getRecord(), $parameters);
259259
} /** Update Resource */
260260
elseif ($this->isUpdateResource()) {
261-
$authorized = $this->authorizer->canUpdate($this->getRecord(), $parameters);
261+
$authorized = $this->authorizer->canUpdate($this->getRecord(), $this->getDocument()->getResource(), $parameters);
262262
} /** Delete Resource */
263263
elseif ($this->isDeleteResource()) {
264264
$authorized = $this->authorizer->canDelete($this->getRecord(), $parameters);
@@ -281,6 +281,7 @@ protected function authorizeBeforeValidation()
281281
$authorized = $this->authorizer->canModifyRelationship(
282282
$this->getRelationshipName(),
283283
$this->getRecord(),
284+
$this->getDocument()->getRelationship(),
284285
$parameters
285286
);
286287
}
@@ -361,7 +362,7 @@ protected function validateParameters()
361362

362363
/** If we are on an index route, we also validate the filter parameters. */
363364
$validator = ($this->isIndex() && $this->validators) ?
364-
$this->validators->filterResources() : null;
365+
$this->validators->filterResources($this->getResourceType()) : null;
365366

366367
if ($validator && !$validator->isValid((array) $parameters->getFilteringParameters())) {
367368
throw new ValidationException($validator->getErrors());
@@ -381,13 +382,13 @@ protected function validator()
381382

382383
/** Create Resource */
383384
if ($this->isCreateResource()) {
384-
return $this->validators->createResource();
385+
return $this->validators->createResource($this->getResourceType());
385386
} /** Update Resource */
386387
elseif ($this->isUpdateResource()) {
387-
return $this->validators->updateResource($this->getRecord(), $this->getResourceId());
388+
return $this->validators->updateResource($this->getResourceType(), $this->getResourceId(), $this->getRecord());
388389
} /** Replace Relationship */
389390
elseif ($this->isModifyRelationship()) {
390-
return $this->validators->modifyRelationship($this->getRelationshipName(), $this->getRecord());
391+
return $this->validators->modifyRelationship($this->getResourceType(), $this->getResourceId(), $this->getRelationshipName(), $this->getRecord());
391392
}
392393

393394
return null;

src/Hydrator/AbstractHydrator.php

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

src/Pagination/Paginator.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -173,16 +173,13 @@ protected function createLink($page, $perPage, array $parameters = [], $meta = n
173173

174174
/**
175175
* @param SortParameterInterface[] $parameters
176-
* @return string
176+
* @return string|null
177177
*/
178178
private function buildSortParams(array $parameters)
179179
{
180-
$sort = [];
181-
182-
/** @var SortParameterInterface $param */
183-
foreach ($parameters as $param) {
184-
$sort[] = $param->isAscending() ? $param->getField() : '-' . $param->getField();
185-
}
180+
$sort = array_map(function (SortParameterInterface $param) {
181+
return (string) $param;
182+
}, $parameters);
186183

187184
return !empty($sort) ? implode(',', $sort) : null;
188185
}

0 commit comments

Comments
 (0)