定制 nyamort/laravel-anonymizer 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

nyamort/laravel-anonymizer

最新稳定版本:v1.0.0

Composer 安装命令:

composer require nyamort/laravel-anonymizer

包简介

This is my package laravel-anonymizer

README 文档

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.

统计信息

  • 总下载量: 228
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-12-03