2017 © Pedro Peláez
 

library data-transfer-object

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

image

kumuwai/data-transfer-object

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

  • Wednesday, April 8, 2015
  • by kumuwai
  • Repository
  • 2 Watchers
  • 5 Stars
  • 776 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 5 Versions
  • 6 % Grown

The README.md

DataTransferObject

Latest Stable Version Build Status Coverage Status Quality Score License, (*1)

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., (*2)

Usage

You can instantiate the class with an array, arrayable object, or json string. These are all equivalent:, (*3)

$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:, (*4)

echo $dto['x'];
echo $dto->x;
echo $dto->get('x');

These will also handle nested sets:, (*5)

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:, (*6)

$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:, (*7)

$dto['x'] = 'y';
$dto->x = 'y';

Count and iterate the properties:, (*8)

$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., (*9)

  • 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:, (*10)

$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:, (*11)

"require": {
    "kumuwai/data-transfer-object": "dev-master"
}

Next, update Composer from the terminal:, (*12)

composer update

TODO

None at this time, (*13)

The Versions

08/04 2015

dev-master

9999999-dev https://github.com/kumuwai/data-transfer-object

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

  Sources   Download

MIT

The Development Requires

by Joel Hatch

dot json array dto

08/04 2015

v0.0.4

0.0.4.0 https://github.com/kumuwai/data-transfer-object

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

  Sources   Download

MIT

The Development Requires

by Joel Hatch

dot json array dto

07/03 2015

v0.0.3

0.0.3.0 https://github.com/kumuwai/data-transfer-object

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

  Sources   Download

MIT

The Development Requires

by Joel Hatch

dot json array dto

07/03 2015

dev-dev

dev-dev https://github.com/kumuwai/data-transfer-object

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

  Sources   Download

MIT

The Development Requires

by Joel Hatch

dot json array dto

05/03 2015

v0.0.2

0.0.2.0

  Sources   Download

MIT

The Development Requires

by Joel Hatch

dto