locomotivemtl/charcoal-presenter 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱: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
  • 点击次数: 2
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

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

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固