andrey-helldar/simple-data-transfer-object

This package is abandoned and no longer maintained. The author suggests using the dragon-code/simple-data-transfer-object package instead.

Simple Data Transfer Objects

v2.0.0 2021-11-11 11:13 UTC

This package is auto-updated.

Last update: 2021-11-16 13:13:29 UTC


README

Simple DTO

Stable Version Unstable Version Total Downloads License

Installation

To get the latest version of Simple Data Transfer Object, simply require the project using Composer:

$ composer require dragon-code/simple-data-transfer-object

Or manually update require block of composer.json and run composer update.

{
    "require": {
        "dragon-code/simple-data-transfer-object": "^2.0"
    }
}

Upgrade from andrey-helldar/simple-data-transfer-object

  1. Replace "andrey-helldar/simple-data-transfer-object": "^1.0" with "dragon-code/simple-data-transfer-object": "^2.0" in the composer.json file;
  2. Replace Helldar\SimpleDataTransferObject namespace prefix with DragonCode\SimpleDataTransferObject in your application;
  3. Call the composer update console command.

Using

Basic

namespace App\DTO;

use DragonCode\SimpleDataTransferObject\DataTransferObject;

class YourInstance extends DataTransferObject
{
    public $foo;
   
    public $bar;
    
    public $baz;
    
    public $qwerty;
}

$instance = YourInstance::make([
    'foo' => 'Foo',
    'bar' => 'Bar'
    'baz' => 'Baz'
]);


return $instance->foo;
// Foo

return $instance->bar;
// Bar

return $instance->baz;
// Baz

return $instance->qwerty;
// null

Mappings

namespace App\DTO;

use DragonCode\SimpleDataTransferObject\DataTransferObject;

class YourInstance extends DataTransferObject
{
    public $foo;
   
    public $bar;
    
    public $baz;
    
    public $qwerty;
   
    protected $map = [
        'data.foo' => 'foo',
        'data.bar' => 'bar',
    ];
}

$instance = YourInstance::make([
    'data' => [
        'foo' => 'Foo',
        'bar' => 'Bar'
    ],
    'baz' => 'Baz'
]);


return $instance->foo;
// Foo

return $instance->bar;
// Bar

return $instance->baz;
// Baz

return $instance->qwerty;
// null

Casts

namespace App\DTO;

use DragonCode\SimpleDataTransferObject\DataTransferObject;
use DragonCode\Support\Facades\Helpers\Str;

class YourInstance extends DataTransferObject
{
    public $foo;
   
    public $bar;
    
    public $baz;
    
    public $qwerty;
   
    protected $map = [
        'data.foo' => 'foo',
        'data.bar' => 'bar',
    ];
    
    protected function castFoo($value)
    {
        return Str::upper($value);
    }
}

$instance = YourInstance::make([
    'data' => [
        'foo' => 'Foo',
        'bar' => 'Bar'
    ],
    'baz' => 'Baz'
]);


return $instance->foo;
// FOO

return $instance->bar;
// Bar

return $instance->baz;
// Baz

return $instance->qwerty;
// null