diff --git a/src/Anonymizable.php b/src/Anonymizable.php index 992560b..cd48267 100644 --- a/src/Anonymizable.php +++ b/src/Anonymizable.php @@ -6,6 +6,11 @@ interface Anonymizable { + /** + * Get the key value used to ensure consistent fake data generation. + */ + public function getAnonymizableKey(): ?string; + /** * Get the seed value used to ensure consistent fake data generation. */ diff --git a/src/Anonymized.php b/src/Anonymized.php index 976e794..4874e0f 100644 --- a/src/Anonymized.php +++ b/src/Anonymized.php @@ -3,35 +3,13 @@ namespace DirectoryTree\Anonymize; use Faker\Generator; -use Illuminate\Support\Facades\App; /** * @mixin Anonymizable */ trait Anonymized { - /** - * Whether to enable anonymization for the current model instance. - */ - protected bool $anonymizeEnabled = true; - - /** - * The anonymized attributes for the current model instance and seed. - */ - protected array $anonymizedAttributeCache; - - /** - * The seed for the cached anonymized attributes. - */ - protected string $anonymizedAttributeCacheSeed; - - /** - * Get the anonymize manager instance. - */ - protected static function getAnonymizeManager(): AnonymizeManager - { - return App::make(AnonymizeManager::class); - } + use AnonymizesAttributes; /** * Get the anonymized attributes. @@ -67,70 +45,4 @@ public function getAttributeValue($key): mixed return $this->getCachedAnonymizedAttributes()[$key] ?? parent::getAttributeValue($key); } - - /** - * Get the seed for the anonymizable model. - */ - public function getAnonymizableSeed(): string - { - return get_class($this).':'.$this->getAttributeValue('id'); - } - - /** - * @template TReturn - * - * @param callable($this): TReturn $callback - * @return TReturn - */ - public function withoutAnonymization(callable $callback): mixed - { - $previous = $this->anonymizeEnabled; - - $this->anonymizeEnabled = false; - - try { - return $callback($this); - } finally { - $this->anonymizeEnabled = $previous; - } - } - - /** - * Make the anonymized attributes. - */ - protected function getCachedAnonymizedAttributes(): array - { - return $this->withoutAnonymization(function (): array { - $seed = $this->getAnonymizableSeed(); - - if (! isset($this->anonymizedAttributeCache) || $this->anonymizedAttributeCacheSeed !== $seed) { - $this->anonymizedAttributeCache = $this->getAnonymizedAttributes( - static::getAnonymizeManager()->faker($seed) - ); - - $this->anonymizedAttributeCacheSeed = $seed; - } - - return $this->anonymizedAttributeCache; - }); - } - - /** - * Add the anonymized attributes to the attribute array. - * - * @param array $attributes - * @return array - */ - protected function addAnonymizedAttributesToArray(array $attributes): array - { - foreach ($this->getCachedAnonymizedAttributes() as $key => $value) { - if (! array_key_exists($key, $attributes)) { - continue; - } - - $attributes[$key] = $value; - } - - return $attributes; - } } diff --git a/src/AnonymizedResource.php b/src/AnonymizedResource.php new file mode 100644 index 0000000..a8fa7c5 --- /dev/null +++ b/src/AnonymizedResource.php @@ -0,0 +1,25 @@ + + */ + public function resolve($request = null): array + { + $attributes = parent::resolve($request); + + if (! $this->anonymizeEnabled || ! static::getAnonymizeManager()->isEnabled()) { + return $attributes; + } + + return $this->addAnonymizedAttributesToArray($attributes); + } +} diff --git a/src/AnonymizesAttributes.php b/src/AnonymizesAttributes.php new file mode 100644 index 0000000..089c353 --- /dev/null +++ b/src/AnonymizesAttributes.php @@ -0,0 +1,107 @@ +anonymizeEnabled; + + $this->anonymizeEnabled = false; + + try { + return $callback($this); + } finally { + $this->anonymizeEnabled = $previous; + } + } + + /** + * Get the key for the anonymizable instance. + */ + public function getAnonymizableKey(): ?string + { + return $this->getKey(); + } + + /** + * Get the seed for the anonymizable instance. + */ + public function getAnonymizableSeed(): string + { + return get_class($this).':'.$this->getAnonymizableKey(); + } + + /** + * Add the anonymized attributes to the attribute array. + * + * @param array $attributes + * @return array + */ + protected function addAnonymizedAttributesToArray(array $attributes): array + { + foreach ($this->getCachedAnonymizedAttributes() as $key => $value) { + if (! array_key_exists($key, $attributes)) { + continue; + } + + $attributes[$key] = $value; + } + + return $attributes; + } + + /** + * Make the anonymized attributes. + */ + protected function getCachedAnonymizedAttributes(): array + { + return $this->withoutAnonymization(function (): array { + $seed = $this->getAnonymizableSeed(); + + if (! isset($this->anonymizedAttributeCache) || $this->anonymizedAttributeCacheSeed !== $seed) { + $this->anonymizedAttributeCache = $this->getAnonymizedAttributes( + static::getAnonymizeManager()->faker($seed) + ); + + $this->anonymizedAttributeCacheSeed = $seed; + } + + return $this->anonymizedAttributeCache; + }); + } + + /** + * Get the anonymize manager instance. + */ + protected static function getAnonymizeManager(): AnonymizeManager + { + return App::make(AnonymizeManager::class); + } +} diff --git a/tests/AnonymizedJsonResource.php b/tests/AnonymizedJsonResource.php new file mode 100644 index 0000000..ae679b4 --- /dev/null +++ b/tests/AnonymizedJsonResource.php @@ -0,0 +1,26 @@ + $faker->name(), + 'address' => $faker->address(), + ]; + } + + public function getAnonymizableKey(): string + { + return 1; + } +} diff --git a/tests/Unit/AnonymizedJsonResourceTest.php b/tests/Unit/AnonymizedJsonResourceTest.php new file mode 100644 index 0000000..6291881 --- /dev/null +++ b/tests/Unit/AnonymizedJsonResourceTest.php @@ -0,0 +1,28 @@ + 'Foo Bar', + 'address' => '1600 Pennsylvania Avenue', + ]); + + expect($resource->resolve())->not->toHaveKey('name', 'Foo Bar') + ->and($resource->resolve())->not->toHaveKey('address', '1600 Pennsylvania Avenue'); +}); + +it('does not anonymize json resource when anonymization is disabled', function () { + Anonymize::disable(); + + $resource = new AnonymizedJsonResource([ + 'name' => 'Foo Bar', + 'address' => '1600 Pennsylvania Avenue', + ]); + + expect($resource->resolve())->toHaveKey('name', 'Foo Bar') + ->and($resource->resolve())->toHaveKey('address', '1600 Pennsylvania Avenue'); +}); diff --git a/tests/Unit/AnonymizedTest.php b/tests/Unit/AnonymizedModelTest.php similarity index 100% rename from tests/Unit/AnonymizedTest.php rename to tests/Unit/AnonymizedModelTest.php