PHP code example of dragon-code / simple-dto

1. Go to this page and download the library: Download dragon-code/simple-dto 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/ */

    

dragon-code / simple-dto example snippets


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

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

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

use Tests\Fixtures\Nested\Company;

$company = Company::make([
    'title' => 'First Company',

    'projects' => [
        [
            'title'  => 'Project 1',
            'domain' => 'https://example.com',

            'developers' => [
                [
                    'name'  => 'Andrey Helldar',
                    'email' => '[email protected]',
                ],
                [
                    'name'  => 'John Doe',
                    'email' => '[email protected]',
                ],
            ],
        ],
    ],
]);

$company->title;
// First Company

foreach ($company->projects as $project) {
    $project->title;
    // 0: Project 1

    foreach ($project->developers as $developer) {
        $developer->name;
        // 0: Andrey Helldar
        // 1: John Doe
    }
}

namespace Tests\Fixtures\Nested;

use DragonCode\SimpleDataTransferObject\DataTransferObject;

class Company extends DataTransferObject
{
    public string $title;

    /** @var \Tests\Fixtures\Nested\Project[] */
    public array $projects;

    protected function castProjects(array $projects): array
    {
        return array_map(static function (array $project) {
            return Project::make($project);
        }, $projects);
    }
}

use DragonCode\Contracts\DataTransferObject\DataTransferObject;
use Tests\Fixtures\Simple;

class Foo
{
    protected array $items = [
        'foo' => 'Foo',
        'bar' => 'Bar',
    ];

    protected function dto(): DataTransferObject
    {
        return Simple::fromArray($this->items);
    }
}

use DragonCode\Contracts\DataTransferObject\DataTransferObject;
use Tests\Fixtures\Simple;

class Foo
{
    protected string $json = '{"foo":"Foo","bar":"Bar"}';

    protected function dto(): DataTransferObject
    {
        return Simple::fromJson($this->json);
    }
}

use DragonCode\Contracts\DataTransferObject\DataTransferObject;
use Tests\Fixtures\CustomObject;
use Tests\Fixtures\Simple;

class Foo
{
    protected CustomObject $object;

    protected function dto(): DataTransferObject
    {
        return Simple::fromObject($this->object);
    }
}

use DragonCode\Contracts\DataTransferObject\DataTransferObject;
use Symfony\Component\HttpFoundation\Request;
use Tests\Fixtures\Simple;

class Foo
{
    protected Request $request;

    protected function dto(): DataTransferObject
    {
        return Simple::fromRequest($this->request)
    }
}

namespace App\DTO;

use DragonCode\SimpleDataTransferObject\DataTransferObject;

class YourInstance extends DataTransferObject
{
    public $foo;
}

$instance = YourInstance::make([
    'foo' => 'none',
]);

return $instance->foo;
// none

return $instance->get('foo');
// none

$instance->set('foo', 'Foo');

return $instance->foo;
// Foo

namespace App\Http\Requests\Companies;

use App\Objects\Company;
use DragonCode\Contracts\DataTransferObject\DataTransferObject;
use DragonCode\Contracts\DataTransferObject\Dtoable;

class CreateRequest implements Dtoable
{
    // ...

    public function dto(): DataTransferObject
    {
        return Company::fromRequest($this);
    }
}

// Other place
public function store(CreateRequest $request)
{
    $name = $request->dto()->name;
}