nyamort/laravel-anonymizer

This is my package laravel-anonymizer

Fund package maintenance!
Nyamort

Installs: 201

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

Open Issues: 0

pkg:composer/nyamort/laravel-anonymizer

v1.0.0 2025-12-03 15:46 UTC

This package is auto-updated.

Last update: 2025-12-03 15:53:28 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

This is where your description should go. Limit it to a paragraph or two. Consider adding a small example.

Support us

We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.

Installation

You can install the package via composer:

composer require nyamort/laravel-anonymizer

You can publish the config file with:

php artisan vendor:publish --tag="anonymizer-config"

This is the contents of the published config file:

return [
    'mask_string' => '[anonymous]',

    'email_domain' => 'example.test',

    'hash_algorithm' => 'sha256',

    /*
    |--------------------------------------------------------------------------
    | Custom strategies
    |--------------------------------------------------------------------------
    |
    | You can register your own strategies and reference them in the $anonymize
    | array on your models. Each strategy receives the current value and the
    | model instance and must return the anonymized value.
    |
    */
    'strategies' => [
        // 'phone' => fn (string $value, $model) => '0000000000',
    ],
];

Usage

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Nyamort\LaravelAnonymizer\Concerns\Anonymize;

class User extends Model
{
    use Anonymize;
    use SoftDeletes;

    protected array $anonymize = [
        'name' => 'mask',               // becomes "[anonymous]"
        'email' => 'email',             // anonymous+<uuid>@example.test
        'note' => 'hash',               // hashed with the configured algorithm
        'meta' => fn () => [],          // closures receive the current value and the model
    ];
}

$user = User::create([...]);

// Will anonymize the configured attributes, then soft delete the row.
$user->delete();

// Can also be called manually without deleting:
$user->anonymize();

// You can also point to any invokable class without touching the config:
class Uppercase implements \Nyamort\LaravelAnonymizer\Contracts\AnonymizationStrategy
{
    public function __invoke(mixed $value, \Illuminate\Database\Eloquent\Model $model): mixed
    {
        return is_string($value) ? strtoupper($value) : $value;
    }
}

class Admin extends Model
{
    use Anonymize;
    use SoftDeletes;

    protected array $anonymize = [
        'name' => Uppercase::class, // resolved through the container automatically
    ];
}

// Faker is auto-injected as a 3rd argument if your strategy asks for it:
class FakeEmail implements \Nyamort\LaravelAnonymizer\Contracts\AnonymizationStrategy
{
    public function __invoke(mixed $value, \Illuminate\Database\Eloquent\Model $model, \Faker\Generator $faker): mixed
    {
        return $faker->unique()->safeEmail();
    }
}

Built-in strategies you can reference in $anonymize:

  • mask / string: replaces the value with mask_string
  • email: anonymous+<uuid>@<email_domain>
  • hash: hashes the current value with hash_algorithm
  • uuid, random_string, null, empty, empty_array, phone

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.