[Feature] Serialize resources using schemas #2
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Makes the
Core\Resources\JsonApiResource
class non-abstract, by injecting the schema as well as the model into the class constructor. Attributes and relationships can then be serialized using the schema. To do this, the schema's fields must implement serializable contracts:Contracts\Resources\Serializer\Attribute
Contracts\Resources\Serializer\Relation
These are separate interfaces so that serializable fields are always optional - for example, a non-Eloquent implementation might always need resources to exist rather than supporting serialization via the schema.
To wire this in, the
Core\Resources\ResourceResolver
now checks whether the computed resource class name exists, and if it doesn't it falls back on the default implementation - i.e. using theJsonApiResource
class. The resource factory has been updated so that it injects both the schema and the model into the resource class that it is building.There were some additional changes implemented at the same time:
JsonApiResource
method signatures forattributes
,relationships
,meta
andlinks
have been updated to receive the request as a parameter. This brings it in line with Laravel's Eloquent resources, though in our implementation the request can benull
(for example, if serializing for broadcasting).Encoder
contract now has awithRequest
method to inject the request into the encoding process. (The encoder now needs access to the request because of the changes in the previous bullet point.)