kumuwai/data-transfer-object
Composer 安装命令:
composer require kumuwai/data-transfer-object
包简介
Load/view dto elements with object, array, json, or dot-notation
README 文档
README
This class is designed to make it easy to add and view data. Load objects, arrays, or json; read with object, array, or dot notation; output to json string.
Usage
You can instantiate the class with an array, arrayable object, or json string. These are all equivalent:
$object = new StdObject; $object->foo = 'bar'; $dto = new DTO($object); $dto = new DTO(['foo'=>'bar']); $dto = new DTO('{"foo":"bar"}'); $dto = DTO::make($object); $dto = DTO::make(['foo'=>'bar']); $dto = DTO::make('{"foo":"bar"}');
Read data with array, object, or dot notation:
echo $dto['x']; echo $dto->x; echo $dto->get('x');
These will also handle nested sets:
echo $dto['x']['y']['z']; echo $dto->x->y->z; echo $dto->get('x.y.z');
By default, an empty string will be returned if a missing property is accessed. Other possibilities:
$dto = new DTO([], 'x'); // instantiate with a given default $dto->setDefault('x'); // change the default $dto->get('path.to.key', 'x'); // override default for this method call $dto->setDefault(Null); // throw an UndefinedProperty exception
Add new data with array or object notation:
$dto['x'] = 'y'; $dto->x = 'y';
Count and iterate the properties:
$dto = new DTO([...]) $count = count($dto); foreach($dto as $key=>$value) // do something
Laravel Support
There are two versions of the data transfer object that implement Laravel-specific interfaces. Use one of these classes if you want Laravel to work with DTOs as first-class Laravel objects.
- Laravel4DTO implements JsonableInterface and ArrayableInterface
- Laravel5DTO implements Jsonable and Arrayable
You can use these to sanitize output before you send it to a view, eg:
$models = Model::all(); $output = []; foreach($models as $model) $output[] = new Laravel4DTO([ 'name' => $model->name, 'paid' => $model->payments->sum('payment_amount'), ... ]); return new Collection($output);
Installation
Install the package via Composer. Edit your composer.json file as follows:
"require": {
"kumuwai/data-transfer-object": "dev-master"
}
Next, update Composer from the terminal:
composer update
TODO
None at this time
kumuwai/data-transfer-object 适用场景与选型建议
kumuwai/data-transfer-object 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.41k 次下载、GitHub Stars 达 6, 最近一次更新时间为 2015 年 03 月 05 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「json」 「array」 「dot」 「dto」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 kumuwai/data-transfer-object 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 kumuwai/data-transfer-object 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 kumuwai/data-transfer-object 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A set of useful PHP classes.
Kinikit - PHP Application development framework MVC component
Access array data quickly/easily using dot-notation and asterisk.
ext-json wrapper with sane defaults
A GraphViz generator for PHP. PEAR Image_GraphViz rethought.
A package to cast json fields, each sub-keys is castable
统计信息
- 总下载量: 2.41k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 6
- 点击次数: 25
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-03-05