-
Notifications
You must be signed in to change notification settings - Fork 0
Add ability to anonymize JSON resources #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
src/AnonymizesAttributes.php
Outdated
| */ | ||
| public function getAnonymizableSeed(): string | ||
| { | ||
| return get_class($this).':'.$this->getKey(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This works automatically with JsonResource's because Laravel proxies undefined method and property calls to the underlying resource -- which will most of the time be a Model instance.
In other cases where a model instance is not used (or the resource does not have a getKey method), they may override this method to return something else:
public function getAnonymizableKey(): string
{
return '...';
}There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Clever! I tried this but totally forgot the resource will just proxy the method call 🤦
| /** | ||
| * Get the key for the anonymizable instance. | ||
| */ | ||
| public function getAnonymizableKey(): ?string | ||
| { | ||
| return $this->getKey(); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added this method to help prevent possible collisions so developers don't have to remember to provide a unique seed prefix (get_class($this)) and can instead just worry about the key itself when overriding.
J0sh0nat0r
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Clean implementation! LGTM 👍
This PR adds the ability to anonymize JSON resources by using the existing
Anonymizableinterface and a newAnonymizedResourcetrait:This helps in circumstances where a model cannot be used, such as Eloquent
Pivotmodels, since Laravel does not reliably associate existing models to pivot relation properties.What I mean by "reliably" is that I discovered Laravel does not set the
Pivotmodel'spivotParentorpivotRelatedproperties the same way when eager-loading on a collection of models vs a single model. This means when defining anonymized attributes on aPivotmodel, we can't always get thekeyon theMetanotemodel which is required to know what kind of fake data to generate. Here's some test cases illustrating this issue:Setup:
Using
pivotParentattribute on the Pivot model:Using
pivotRelatedattribute on the Pivot model: