From 61ebaeff70a54c0a12f30e28ba829ba1bf3f4428 Mon Sep 17 00:00:00 2001 From: Steve Bauman Date: Tue, 9 Sep 2025 15:03:33 -0400 Subject: [PATCH 1/5] Rename resolve to toAnonymized so anonymization is done consistently on all resources --- src/AnonymizedResource.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) 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; } From 2dc3b6ba4ed1896f6d671eb52ffeef198c79bde7 Mon Sep 17 00:00:00 2001 From: Steve Bauman Date: Tue, 9 Sep 2025 15:04:22 -0400 Subject: [PATCH 2/5] Fix merging of anonymized array attributes --- src/AnonymizesAttributes.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) 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; From 83c5981d179d59349056668c91969cd66fdd301f Mon Sep 17 00:00:00 2001 From: Steve Bauman Date: Tue, 9 Sep 2025 15:04:35 -0400 Subject: [PATCH 3/5] Add test --- tests/AnonymizedJsonResource.php | 15 +++++++++++ tests/NestedAnonymizedResource.php | 31 +++++++++++++++++++++ tests/Unit/AnonymizedJsonResourceTest.php | 33 +++++++++++++++++++++++ 3 files changed, 79 insertions(+) create mode 100644 tests/NestedAnonymizedResource.php 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..5d89fa2 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', 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'); +}); From 236131357f04ab29e254b1207dcfa2d40b951a9d Mon Sep 17 00:00:00 2001 From: Steve Bauman Date: Tue, 9 Sep 2025 15:04:38 -0400 Subject: [PATCH 4/5] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 From 45e2e07f192c8182f6d5e8711cc91e73c2281341 Mon Sep 17 00:00:00 2001 From: Steve Bauman Date: Tue, 9 Sep 2025 15:09:24 -0400 Subject: [PATCH 5/5] Update AnonymizedJsonResourceTest.php --- tests/Unit/AnonymizedJsonResourceTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Unit/AnonymizedJsonResourceTest.php b/tests/Unit/AnonymizedJsonResourceTest.php index 5d89fa2..f176a86 100644 --- a/tests/Unit/AnonymizedJsonResourceTest.php +++ b/tests/Unit/AnonymizedJsonResourceTest.php @@ -28,7 +28,7 @@ ->and($resource->resolve())->toHaveKey('address', '1600 Pennsylvania Avenue'); }); -it('anonymizes nested arrays', function () { +it('anonymizes nested arrays and resources', function () { Anonymize::enable(); $resource = new AnonymizedJsonResource([