diff --git a/README.md b/README.md index 093a2da..b5923a6 100644 --- a/README.md +++ b/README.md @@ -201,12 +201,12 @@ class UserResource extends JsonResource implements Anonymizable public function toArray(Request $request): array { - return [ + return $this->toAnonymized([ 'id' => $this->id, 'name' => $this->name, 'email' => $this->email, 'phone' => $this->phone, - ]; + ]); } public function getAnonymizedAttributes(Generator $faker): array diff --git a/src/AnonymizedResource.php b/src/AnonymizedResource.php index a8fa7c5..9b27fab 100644 --- a/src/AnonymizedResource.php +++ b/src/AnonymizedResource.php @@ -7,15 +7,13 @@ trait AnonymizedResource use AnonymizesAttributes; /** - * Transform the resource into an array. + * Anonymize the given attributes. * - * @param \Illuminate\Http\Request|null $request + * @param array $attributes * @return array */ - public function resolve($request = null): array + public function toAnonymized(array $attributes): array { - $attributes = parent::resolve($request); - if (! $this->anonymizeEnabled || ! static::getAnonymizeManager()->isEnabled()) { return $attributes; } diff --git a/src/AnonymizesAttributes.php b/src/AnonymizesAttributes.php index 089c353..f327a17 100644 --- a/src/AnonymizesAttributes.php +++ b/src/AnonymizesAttributes.php @@ -71,7 +71,13 @@ protected function addAnonymizedAttributesToArray(array $attributes): array continue; } - $attributes[$key] = $value; + if (! is_array($attributes[$key])) { + $attributes[$key] = $value; + + continue; + } + + $attributes[$key] = $this->addAnonymizedAttributesToArray($value) + $attributes[$key]; } return $attributes; diff --git a/tests/AnonymizedJsonResource.php b/tests/AnonymizedJsonResource.php index ae679b4..1f61664 100644 --- a/tests/AnonymizedJsonResource.php +++ b/tests/AnonymizedJsonResource.php @@ -5,17 +5,32 @@ use DirectoryTree\Anonymize\Anonymizable; use DirectoryTree\Anonymize\AnonymizedResource; use Faker\Generator; +use Illuminate\Http\Request; use Illuminate\Http\Resources\Json\JsonResource; class AnonymizedJsonResource extends JsonResource implements Anonymizable { use AnonymizedResource; + public function toArray(Request $request): array + { + return $this->toAnonymized($this->resource); + } + public function getAnonymizedAttributes(Generator $faker): array { return [ 'name' => $faker->name(), 'address' => $faker->address(), + 'role' => [ + 'name' => $faker->word(), + 'permissions' => [ + ['name' => $faker->word()], + ], + ], + 'nested' => [ + 'name' => $faker->name(), + ], ]; } diff --git a/tests/NestedAnonymizedResource.php b/tests/NestedAnonymizedResource.php new file mode 100644 index 0000000..e5d09d8 --- /dev/null +++ b/tests/NestedAnonymizedResource.php @@ -0,0 +1,31 @@ +toAnonymized($this->resource); + } + + public function getAnonymizedAttributes(Generator $faker): array + { + return [ + 'name' => $faker->name(), + ]; + } + + public function getAnonymizableKey(): string + { + return 1; + } +} diff --git a/tests/Unit/AnonymizedJsonResourceTest.php b/tests/Unit/AnonymizedJsonResourceTest.php index 6291881..f176a86 100644 --- a/tests/Unit/AnonymizedJsonResourceTest.php +++ b/tests/Unit/AnonymizedJsonResourceTest.php @@ -2,6 +2,7 @@ use DirectoryTree\Anonymize\Facades\Anonymize; use DirectoryTree\Anonymize\Tests\AnonymizedJsonResource; +use DirectoryTree\Anonymize\Tests\NestedAnonymizedResource; it('anonymizes json resource when anonymization is enabled', function () { Anonymize::enable(); @@ -26,3 +27,35 @@ expect($resource->resolve())->toHaveKey('name', 'Foo Bar') ->and($resource->resolve())->toHaveKey('address', '1600 Pennsylvania Avenue'); }); + +it('anonymizes nested arrays and resources', function () { + Anonymize::enable(); + + $resource = new AnonymizedJsonResource([ + 'name' => 'Foo Bar', + 'address' => '1600 Pennsylvania Avenue', + 'role' => [ + 'name' => 'Admin', + 'permissions' => [ + ['name' => 'View Users'], + ['name' => 'View Roles'], + ], + 'created_at' => '2023-01-01 00:00:00', + ], + 'nested' => new NestedAnonymizedResource([ + 'name' => 'Foo Bar', + ]), + ]); + + $attributes = $resource->resolve(); + + expect($attributes['role'])->toHaveKey('created_at'); + expect($attributes['role']['name'])->not->toBe('Admin'); + + expect($attributes['role']['permissions'])->toHaveCount(1); + expect($attributes['role']['permissions'][0])->toHaveKey('name'); + expect($attributes['role']['permissions'][0]['name'])->not->toBe('View Users'); + + expect($attributes['nested'])->toHaveKey('name'); + expect($attributes['nested']['name'])->not->toBe('Foo Bar'); +});