kumuwai/data-transfer-object 问题修复 & 功能扩展

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

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

kumuwai/data-transfer-object

最新稳定版本:v0.0.4

Composer 安装命令:

composer require kumuwai/data-transfer-object

包简介

Load/view dto elements with object, array, json, or dot-notation

README 文档

README

Latest Stable Version Build Status Coverage Status Quality Score License

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

统计信息

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

GitHub 信息

  • Stars: 5
  • Watchers: 2
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-03-05