定制 locomotivemtl/charcoal-presenter 二次开发

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

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

locomotivemtl/charcoal-presenter

最新稳定版本:0.3.2

Composer 安装命令:

composer require locomotivemtl/charcoal-presenter

包简介

The missing charcoal layer between models and views.

README 文档

README

The missing layer between models and views. The Presenter takes any data model (objects or arrays) and serializes them into a presentation array according to a transformer.

Usage

Simplest usage, with a simple array transformer:

$presenter = new Presenter([
    'id',
    'name',
    'display_date'
]);

$model = $factory->create(Model::class);
$viewData = $presenter->transform($model);

A callable is preferred if operations on objects are required:

The callable signature must array: callable(mixed $model).

$presenter = new Presenter(function($model) {
    return [
        'id',
        'name',
        'display_date' => $model->date->format('Y-m-d')
    ];
});

$model = $factory->create(Model::class);
$viewData = $presenter->transform($model);

Common transformers (or customizable transformers, shown below) should be self-contained inside their own Callable classes:

class MyTransformer
{
    /**
     * @var string $dateFormat
     */
    private $dateFormat;
    
    /**
     * @param string $dateFormat The date format.
     */
    public function __construct($dateFormat)
    {
        $this->dateFormat = $dateFormat;
    }

    /**
     * @param mixed $model The model to transform.
     * @return array
     */
    public function __invoke($model)
    {
        $displayDate = $obj->date->format($this->dateFormat);
        return [
            'id',
            'name',
            'display_date'=>$displayDate
        ];
    }
}

$presenter = new Presenter(new MyTransformer('Y-m-d'));

$model = $factory->create(Model::class);
$viewData = $presenter->transform($model);

统计信息

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

GitHub 信息

  • Stars: 1
  • Watchers: 14
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-07-31