1. Go to this page and download the library: Download laxity7/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/ */
laxity7 / dto example snippets
use Laxity7\DataTransferObject;
class FooDto extends DataTransferObject
{
public int $id;
public string $name;
public array $data; // just array
/** @var BarDto[] */
public array $bars; // array of objects BarDto
public BarDto $bar;
/** @var BarDto */
public $baz;
public BarReadonlyDto $readonlyBar;
public ReadonlyDto $readonly;
/** @var ReadonlyDto[] */
public array $readonlyArr;
protected string $time;
// setter can be protected/public
// setter has higher priority than field
protected function setName(string $name): void
{
$this->name = 'Foo' . $name;
}
protected function getTime(): string
{
return (new DateTime($this->time))->format('H:i:s');
}
}
// optional to inherit DataTransferObject
class BarDto
{
public int $id;
}
// optional to inherit DataTransferObject
class BarReadonlyDto
{
public function __construct(
readonly public int $id
){}
}
class ReadonlyDto extends DataTransferObject
{
public function __construct(
readonly public string $foo,
readonly public string $bar,
) {
parent::__construct();
}
}
$fooDto = new FooDto([
'id' => 1,
'name' => 'Bar', // FooBar
'data' => [1, 2, 3],
'bars' => [ // array of objects
['id' => 1], // transforms into an object BarDto
['id' => 2], // transforms into an object BarDto
],
'bar' => ['id' => 3], // transforms into an object BarDto
'baz' => new BarReadonlyDto(4), // just set object
'readonlyBar' => ['id' => 5], // transforms into an object BarReadonlyDto
'readonly' => [ // transforms into an object ReadonlyDto
'bar' => 'baz',
'foo' => 'gaz',
],
'readonlyArr' => [ // array of objects
['bar' => 'baz', 'foo' => 'gaz'], // transforms into an object ReadonlyDto
['bar' => 'baz1', 'foo' => 'gaz1'], // transforms into an object ReadonlyDto
],
'time' => '05:59',
]);
// Get all attributes
$arr = $fooDto->toArray();
// Serialize to json (also serializes all nested DTOs)
$json = json_encode($fooDto);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.