tooleks / laravel-presenter

Laravel Presenter 包

2.0.2 2017-06-02 15:26 UTC

This package is not auto-updated.

Last update: 2024-09-26 05:23:12 UTC


README

该包提供将模型对象包装成新表示的 Presenter 层。

特性

该包支持

  • 对象和数组表示
  • 数据映射
  • 嵌套属性
  • 属性覆盖
  • Laravel 5.2 集合
  • JSON 序列化
  • 转换为数组/JSON
  • 构造函数注入

要求

"php": "^7.0", "illuminate/support": "^5.2", "illuminate/contracts": "^5.2"

安装

包安装

执行以下命令获取最新版本的包

composer require tooleks/laravel-presenter

应用配置

要注册服务提供者,只需将 Tooleks\Laravel\Presenter\Providers\PresenterProvider::class 添加到您的 config/app.php 文件的 providers 数组末尾

'providers' => [
    ...
    Tooleks\Laravel\Presenter\Providers\PresenterProvider::class,
],

使用示例

模型表示

要定义您的表示类,您需要扩展基本 Tooleks\Laravel\Presenter\Presenter 类,如下例所示。

重写 getAttributesMap() 方法来构建属性映射定义。

<?php

namespace App\Presenters;

use Tooleks\Laravel\Presenter\Presenter;

/**
 * Class UserPresenter.
 *
 * @property string nickname
 * @property string short_name
 * @property string full_name
 * @property string role
 */
class UserPresenter extends Presenter
{
    /**
     * @inheritdoc
     */
    protected function getAttributesMap(): array
    {
        return [
            // 'presenter_attribute_name' => 'wrapped_model_attribute_name'
            'nickname' => 'username',
            'short_name' => 'first_name',
            'full_name' => function () {
                return $this->getWrappedModelAttribute('first_name') . ' ' . $this->getWrappedModelAttribute('last_name');
            },
            'role' => 'role.name',
        ];
    }
}

通过将包装的模型传递给 setWrappedModel 方法来创建表示对象实例,并使用具有 nicknameshort_namefull_namerole 属性的对象。包装的模型可以是 arrayobject

<?php

$user = [ 
    'username' => 'anna',
    'first_name' => 'Anna',
    'last_name' => 'P.',
    'role' => [
        'name' => 'User',
    ],
];

$userPresenter = app()->make(\App\Presenters\UserPresenter::class)->setWrappedModel($user);
// Create the presenter from the wrapped model array.

$userPresenter = app()->make(\App\Presenters\UserPresenter::class)->setWrappedModel((object) $user);
// Create the presenter from the wrapped model object.

echo $userPresenter->nickname;
// Prints 'anna' string, as we mapped the wrapped model 'username' attribute to the presenter 'nickname' attribute.
echo $userPresenter->short_name;
// Prints 'Anna' string, as we mapped the wrapped model 'first_name' attribute to the presenter 'short_name' attribute.
echo $userPresenter->full_name;
// Prints 'Anna P.' string, as we override the presenter 'full_name' attribute by the anonymous function.
echo $userPresenter->role;
// Prints 'User' string, as we mapped the wrapped model 'role.name' nested attribute to the presenter 'role' attribute.

集合表示

该包还提供了集合宏方法 present(),用于将集合中的每个项目包装到表示类中。

<?php

collect([$user])->present(\App\Presenters\UserPresenter::class);
// Create the collection of the 'UserPresenter' items.

数据映射

该包提供了一个简单的数据映射机制。您只需要在表示对象实例上调用 toArray() 方法。

<?php

$mappedDataArray = app()->make(\App\Presenters\UserPresenter::class)->setWrappedModel($user)->toArray();

$mappedDataObject = (object) app()->make(\App\Presenters\UserPresenter::class)->setWrappedModel($user)->toArray();

用作响应数据

Tooleks\Laravel\Presenter\Presenter 类实现了 Illuminate\Contracts\Support\ArrayableIlluminate\Contracts\Support\JsonableJsonSerializable 接口,因此您可以直接将此类对象传递给响应。

<?php

$content = app()->make(\App\Presenters\UserPresenter::class)->setWrappedModel($user);

response($content);

测试

执行以下命令运行测试

./vendor/bin/phpunit