thirteen/fetchable

将类转换为动态对象。

此包的规范仓库似乎已丢失,因此该包已被冻结。

1.0.3 2016-03-12 16:25 UTC

This package is not auto-updated.

Last update: 2020-02-05 02:18:05 UTC


README

这只是一个特质,用于提供类似laravel的模型属性。

创建一个新的Fetchable类

class Doctor 
{
    use FetchableProperties;
}

$doctor = new Doctor([
    'name' => 'Doctor Who?',
]);

$doctor->name; // Doctor Who?

访问器

这是在获取属性时修改数据的一种方法。

class Doctor
{
    use FetchableProperties;

    public function getNameAttribute() : string
    {
        return 'John Smith';
    }
}

$doctor = new Doctor([
    'name' => 'Doctor Who?',
]);

$doctor->name; // John Smith

修改器

在这里,我们将更改在存储到属性数组之前的数据。

class Doctor 
{
    use FetchableProperties;

    public function setNameAttribute($value) 
    {
        $this->attributes['name'] = 'I am ' . $value;

        // or

        $this->name = 'I am ' . $value;
    }
}

$doctor = new Doctor([
    'name' => 'John Smith',
]);

$doctor->name; // I am John Smith

转换为数组

让我们将数据转换为数组。

class Doctor 
{
    use FetchableProperties;

    public function setNameAttribute($value) 
    {
        $this->attributes['name'] = 'I am ' . $value;

        // or

        $this->name = 'I am ' . $value;
    }
}

$doctor = new Doctor([
    'name' => 'John Smith',
]);


$doctor->toArray(); // ['name' => 'I am John Smith']