zachleigh/laravel-laundromat

在发送到客户端之前清理对象。

v1.2.1 2017-01-30 01:00 UTC

This package is auto-updated.

Last update: 2024-09-22 14:25:46 UTC


README

Latest Stable Version License Build Status SensioLabsInsight Quality Score StyleCI

在发送到客户端之前将对象送至洗衣店。

此包为您提供了一个简单的方式来过滤对象,在发送到客户端之前删除敏感数据。

内容

演示

我们的用户迁移如下所示

Schema::create('users', function (Blueprint $table) {
    $table->increments('id');
    $table->string('username')->unique();
    $table->string('email')->unique();
    $table->string('social_security_number');
    $table->date('birthday');
    $table->string('password');
    $table->integer('family_id')->unsigned();
    $table->rememberToken();
    $table->timestamps();

    $table->foreign('family_id')->references('id')->on('families');
});

我们的用户模型如下所示

class User extends Model
{
    protected $casts = [
        'birthday' => 'date'
    ];

    public function family()
    {
        return $this->belongsTo(Family::class);
    }

    public function readableBirthday()
    {
        return $this->birthday->toFormattedDateString();
    }
}

显然,我们不希望将像 social_security_number 这样的敏感数据发送到前端,这样任何人都可以查看。也许我们只想从用户模型中公开 username,然后是相关家族模型的 last_name。此外,我们想在模型上使用从 readableBirthday() 方法返回的值。

首先,创建一个新的Cleaner类。命名约定是'Clean'加上模型名称

php artisan laundromat:create CleanUser

这将为您在app/Cleaners中提供一个空的Cleaner类。只需在allowed数组中注册允许的属性,并在methods中注册您希望运行的任何方法。使用点符号来指示关系上的属性/方法。

class CleanUser extends Cleaner
{
    /**
     * Properties allowed on the clean object.
     *
     * @var array
     */
    protected $allowed = [
        'username',
        'family.last_name'
    ];

    /**
     * Methods to run. Returned value will be stored as a snake case property
     * on the clean object.
     *
     * @var array
     */
    protected $methods = [
        'readableBirthday'
    ];
}

在您的用户模型中使用Washable特质。

use LaravelLaundromat\Washable;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    use Washable;
    
    //
}

然后对用户对象调用clean()方法以获取CleanUser实例。

$user = User::find(1);

$clean = $user->clean();

var_dump($clean);

// App\Cleaners\CleanUser {
//  "username" => "bettylou"
//  "family" => LaravelLaundromat\EmptyCleaner {
//    "last_name" => "McGraw"
//  }
//  "readable_birthday" => "Jul 15, 1985"
//}

或者,使用集合宏clean()

$users = User::all(); // $users is a collection

$clean = $users->clean(); // All User objects in the collection are now CleanUser objects

将可选的清洁器名称传递给clean()(无论是普通方法还是集合宏)以覆盖默认行为。

$user = User::find(1);

$clean = $user->clean('App/MyDirectory/MyCustomCleaner'); // $clean is now an instance of MyCustomCleaner

或者,将属性defaultCleaner添加到模型中,以永久设置覆盖传统行为。

class User extends Model
{
    use Washable;

    protected $defaultCleaner = MyCustomCleaner::class;
    
    //
}

升级信息

从1.1.*到1.2.0

版本1.2.0添加了对Laravel 5.4的支持。对于Laravel 5.3,请使用版本1.1.0

composer require zachleigh/laravel-laundromat:1.1.*
从1.0.*到1.1.0

版本1.1.0添加了对Laravel 5.3的支持。对于Laravel 5.2,请使用版本1.0.2

composer require zachleigh/laravel-laundromat:1.0.*

安装

通过composer安装

composer require zachleigh/laravel-laundromat

在config/app.php中注册服务提供者

'providers' => [
    ...
    LaravelLaundromat\LaundromatServiceProvider::class,
];

贡献

欢迎贡献。分支、改进、添加测试,并提出拉取请求。