feat!: add context to transformers #589
Closed
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.
This pull request is an alternative to #588, which I personally prefer.
Along with also allowing multiple transformers for a class, it adds a context property to the
Transformer
contract, which can be an arbitrary string.This allows to transform data differently depending on the context (
response
,eloquent
, a custom context...).Unfortunately, this is a breaking change if using a custom
Transformer
implementation. I couldn't find a way to implement the feature in a backwards-compatible way.Example
Usage:
App-specific stuff:
Concerns:
$context
as astring
might be restrictive, this could be an array or even a dedicated classtransform
with the same context everywhere would be repetitive, but this could be attenuated by making theBaseData
classMacroable