Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 3 additions & 5 deletions src/AnonymizedResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, mixed> $attributes
* @return array<string, mixed>
*/
public function resolve($request = null): array
public function toAnonymized(array $attributes): array
{
$attributes = parent::resolve($request);

if (! $this->anonymizeEnabled || ! static::getAnonymizeManager()->isEnabled()) {
return $attributes;
}
Expand Down
8 changes: 7 additions & 1 deletion src/AnonymizesAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
15 changes: 15 additions & 0 deletions tests/AnonymizedJsonResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
],
];
}

Expand Down
31 changes: 31 additions & 0 deletions tests/NestedAnonymizedResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace DirectoryTree\Anonymize\Tests;

use DirectoryTree\Anonymize\Anonymizable;
use DirectoryTree\Anonymize\AnonymizedResource;
use Faker\Generator;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class NestedAnonymizedResource 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(),
];
}

public function getAnonymizableKey(): string
{
return 1;
}
}
33 changes: 33 additions & 0 deletions tests/Unit/AnonymizedJsonResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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');
});