|
2 | 2 |
|
3 | 3 | This file provides notes on how to upgrade between versions.
|
4 | 4 |
|
| 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 | + |
5 | 146 | ## v0.3 to v0.4
|
6 | 147 |
|
7 | 148 | This was a substantial reworking of the package based on our experience of using it in production environments.
|
|
0 commit comments