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
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\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;
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.