PHP code example of kumuwai / data-transfer-object

1. Go to this page and download the library: Download kumuwai/data-transfer-object library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

kumuwai / data-transfer-object example snippets


$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"}');

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

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

$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

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

$dto = new DTO([...])
$count = count($dto);
foreach($dto as $key=>$value)
    // do something

$models = Model::all();
$output = [];
foreach($models as $model)
    $output[] = new Laravel4DTO([
        'name' => $model->name,
        'paid' => $model->payments->sum('payment_amount'),
        ...
    ]);
return new Collection($output);