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
5 changes: 5 additions & 0 deletions src/Anonymizable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
90 changes: 1 addition & 89 deletions src/Anonymized.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<string, mixed> $attributes
* @return array<string, mixed>
*/
protected function addAnonymizedAttributesToArray(array $attributes): array
{
foreach ($this->getCachedAnonymizedAttributes() as $key => $value) {
if (! array_key_exists($key, $attributes)) {
continue;
}

$attributes[$key] = $value;
}

return $attributes;
}
}
25 changes: 25 additions & 0 deletions src/AnonymizedResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace DirectoryTree\Anonymize;

trait AnonymizedResource
{
use AnonymizesAttributes;

/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request|null $request
* @return array<string, mixed>
*/
public function resolve($request = null): array
{
$attributes = parent::resolve($request);

if (! $this->anonymizeEnabled || ! static::getAnonymizeManager()->isEnabled()) {
return $attributes;
}

return $this->addAnonymizedAttributesToArray($attributes);
}
}
107 changes: 107 additions & 0 deletions src/AnonymizesAttributes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php

namespace DirectoryTree\Anonymize;

use Illuminate\Support\Facades\App;

trait AnonymizesAttributes
{
/**
* Whether anonymization is enabled for the current instance.
*/
protected bool $anonymizeEnabled = true;

/**
* The anonymized attributes for the current instance and seed.
*/
protected array $anonymizedAttributeCache;

/**
* The seed for the cached anonymized attributes.
*/
protected string $anonymizedAttributeCacheSeed;

/**
* Execute a callback without anonymization.
*
* @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;
}
}

/**
* Get the key for the anonymizable instance.
*/
public function getAnonymizableKey(): ?string
{
return $this->getKey();
}
Comment on lines +45 to +51
Copy link
Member Author

@stevebauman stevebauman Sep 9, 2025

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.


/**
* 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<string, mixed> $attributes
* @return array<string, mixed>
*/
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);
}
}
26 changes: 26 additions & 0 deletions tests/AnonymizedJsonResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace DirectoryTree\Anonymize\Tests;

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

class AnonymizedJsonResource extends JsonResource implements Anonymizable
{
use AnonymizedResource;

public function getAnonymizedAttributes(Generator $faker): array
{
return [
'name' => $faker->name(),
'address' => $faker->address(),
];
}

public function getAnonymizableKey(): string
{
return 1;
}
}
28 changes: 28 additions & 0 deletions tests/Unit/AnonymizedJsonResourceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

use DirectoryTree\Anonymize\Facades\Anonymize;
use DirectoryTree\Anonymize\Tests\AnonymizedJsonResource;

it('anonymizes json resource when anonymization is enabled', function () {
Anonymize::enable();

$resource = new AnonymizedJsonResource([
'name' => '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');
});
File renamed without changes.