district5/formatter
最新稳定版本:1.0.0
Composer 安装命令:
composer require district5/formatter
包简介
District5 Formatter Library
关键字:
README 文档
README
Installation
Use composer to add this library as a dependency onto your project.
composer require district5/formatter
Usage
Using a formatter
Calling a formatter can be done with a single line of code:
// single object for format $formatted = MyFormatter::formatSingle($model); // multiple objects for format $formatted = MyFormatter::formatMultiple($models);
Creating a formatter
Create your own formatter by extending FormatterAbstract and implementing the formatSingle function. The abstract will automatically give you the formatMultiple functionality.
use District5\Formatter\FormatterAbstract; class MyFormatter extends FormatterAbstract { public static function formatSingle($item, array $options = null) { return [ 'field1' => 'someValue', 'field2' => 5 ]; } }
FormatterAbstract also includes a function to aid with more complex decisions for content inclusion:
use District5\Formatter\FormatterAbstract; class MyComplexFormatter extends FormatterAbstract { public static function formatSingle($item, array $options = null) { $formatted = array( 'id' => $item->getIdStr(), 'created' => $item->getCreatedDateMillis(), 'title' => $item->getTitle(), 'text' => $item->getText() ); if (false !== static::getOption('includeCoverImage', $options, false)) { $formatted['coverImage'] = $item->getCoverImagePath(); } if (false !== static::getOption('includeOwnerId', $options, false)) { $formatted['ownerId'] = $item->getUserId(true); } return $formatted; } }
This could then be called with the following:
// single object for format $formatted = MyComplexFormatter::formatSingle( $model, [ 'includeCoverImage' => true ] ); // multiple objects for format $formatted = MyComplexFormatter::formatMultiple( $models, [ 'includeCoverImage' => true, 'includeOwnerId' => true ] );
统计信息
- 总下载量: 99
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: mit
- 更新时间: 2024-04-29