larapack/attribute-manipulation

允许多个特性操作Eloquent模型的属性。

v1.0.0 2015-11-27 10:15 UTC

This package is auto-updated.

Last update: 2024-09-06 08:45:24 UTC


README

允许多个特性操作Eloquent模型的属性。

安装

使用Composer安装 composer require larapack/attribute-manipulation 1.*

为什么?

一些特性可能想要操作Eloquent模型的属性,但这可以通过创建getAttribute方法或setAttribute方法来完成。然而,如果有多个特性使用这些方法,它们将互相抱怨,因为一个类只能使用一个具有相同方法的特点。所以,如果你打算让多个特性使用相同的方法来操作Eloquent模型的属性,那么你可以使用我们的方法以获得更好的支持。

特性设置

下面是一个使用我们的方法的特性的样子

<?php

namespace Vendor\Package;

use Exception;
use Larapack\AttributeManipulation\Manipulateable;

trait ExampleTrait
{
    /**
     * Boot the ExampleTrait trait for a model.
     * @return void
     */
    public static function bootExampleTrait()
    {
        // first we will check if the model uses the Manipulateable trait, if not we will throw them an friendly error
        if (!isset(class_uses(get_called_class())[Manipulateable::class])) {
            throw new Exception(sprintf('You must use the '.Manipulateable::class.' trait in %s to use the ExampleTrait trait.', get_called_class()));
        }

        // then we will set our manipulator for the attribute setter
        static::addSetterManipulator(function($model, $key, $value) {
            // here we have both the model object ($model), attribute key ($key) and the current value to set ($value).
            // whatever we return is that value that will be sent to the next manipulator 
            // or if none then it will set set to the model object.
            // For this example we will return the encrypted value.
            // Example: $model->password = 'secret' will be saved as the encrypted value of `secret`.
            // For the real encryptable trait which this example is based on please view https://github.com/larapack/attribute-encryption
            return \Crypt::encrypt($value);
        });

        // then we will set our manipulator for the attribute getter
        static::addGetterManipulator(function($model, $key, $value) {
            // here we have both the model object ($model), attribute key ($key) and the current value to set ($value).
            // whatever we return is that value that will be sent to the next manipulator 
            // or if none then it will returned to the output.
            // For this example we will return the decrypted value.
            // Example: echo $model->password will output the decrypted value of `password`.
            // For the real encryptable trait which this example is based on please view https://github.com/larapack/attribute-encryption
            return \Crypt::decrypt($value);
        });
    }
}

示例模型

<?php

namespace App;

use Larapack\AttributeManipulation\Manipulateable;
use Vendor\Package\ExampleTrait;

class User
{
    use Manipulateable;
    use ExampleTrait;

    //...
}

示例测试

$user = new App\User;
$user->password = 'secret';
dump($user); // here you will see that the password is encrypted
echo $user->password; // this will output the decrypted password