[1.x] Allow control over feature serialization #125
Merged
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 PR adds a contract that objects may implement to control how they are serialized.
This is kinda already possible via the existing
FeatureScopeable
interface but is not really the intention of that contract.The exciting
FeatureScopeable
interface allows you to return a value, which may be an object or a scalar, that represents the scope. This value is passed to the feature resolver.With the above in place we can now call
Feature::active($user)
and the feature resolver will receive the user's email address. The email address also ends up stored in the database.But what if you want to be able to pass in the entire user object and still control how the object is serialized? This is where the new contract comes in.
Now the feature resolver will receive the user object, not the email. However, the email will end up stored in the database.
One contract does not necessarily replace the other. I can imagine a set up where objects return a rich object from
toFeatureIdentifier
which then implements theFeatureScopeSerializable
contract.Naming
I matched PHP's
JsonSerializable
interface naming for this.interface JsonSerializable
=>interfaceFeatureScopeSerializable
function jsonSerialize()
=>function featureScopeSerialize()