1. Go to this page and download the library: Download cerbero/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/ */
cerbero / dto example snippets
use Cerbero\Dto\Dto;
use Sample\Dtos\AddressDto;
/**
* A sample user DTO.
*
* @property string $name
* @property bool $isAdmin
* @property mixed $something
* @property \DateTime|null $birthday
* @property UserDto[] $friends
* @property AddressDto $address
*/
class UserDto extends Dto
{
//
}
use Cerbero\Dto\Dto;
/**
* A sample user DTO.
*
* @property string $name
*/
class UserDto extends Dto
{
protected static $defaultValues = [
'name' => 'John',
];
}
// $user1->name will return: John
$user1 = new UserDto();
// $user2->name will return: Jack
$user2 = new UserDto(['name' => 'Jack']);
// as an object
$user->address->street;
// as an array
$user['address']['street'];
// via dot notation
$user->get('address.street');
// via nested DTO
$user->address->get('street');
// as an object
isset($user->address->street);
// as an array
isset($user['address']['street']);
// via dot notation
$user->has('address.street');
// via nested DTO
$user->address->has('street');
// throw Cerbero\Dto\Exceptions\ImmutableDtoException if immutable
$user->address->street = 'King Street';
// throw Cerbero\Dto\Exceptions\ImmutableDtoException if immutable
$user['address']['street'] = 'King Street';
// set the new value in the same instance if mutable or in a new instance if immutable
$user->set('address.street', 'King Street');
// set the new value in the same instance if mutable or in a new instance if immutable
$user->address->set('street', 'King Street');
// throw Cerbero\Dto\Exceptions\ImmutableDtoException if immutable
unset($user->address->street);
// throw Cerbero\Dto\Exceptions\ImmutableDtoException if immutable
unset($user['address']['street']);
// unset the new value in the same instance if mutable or in a new instance if immutable
$user->unset('address.street');
// unset the new value in the same instance if mutable or in a new instance if immutable
$user->address->unset('street');
use Cerbero\Dto\Dto;
use const Cerbero\Dto\PARTIAL;
use const Cerbero\Dto\IGNORE_UNKNOWN_PROPERTIES;
use const Cerbero\Dto\MUTABLE;
/**
* A sample user DTO.
*
* @property string $name
*/
class UserDto extends Dto
{
protected static $defaultFlags = PARTIAL | IGNORE_UNKNOWN_PROPERTIES;
}
// $user->getFlags() will return: PARTIAL | IGNORE_UNKNOWN_PROPERTIES | MUTABLE
$user = UserDto::make($data, MUTABLE);
// ['name', 'isAdmin', ...]
$names = $user->getPropertyNames();
// [Cerbero\Dto\DtoProperty, Cerbero\Dto\DtoProperty, ...]
$properties = $user->getProperties();
// Cerbero\Dto\DtoProperty instance for the property "name"
$nameProperty = $user->getProperty('name');
// TRUE as long as the property "name" is set (even if its value is NULL)
$hasName = $user->hasProperty('name');
foreach($dto as $propertyName => $propertyValue) {
// ...
}
use Cerbero\Dto\Manipulators\ArrayConverter;
use Cerbero\Dto\Manipulators\ValueConverter;
class DateTimeConverter implements ValueConverter
{
public function fromDto($value)
{
return $value->format('Y-m-d');
}
public function toDto($value)
{
return new DateTime($value);
}
}
ArrayConverter::instance()->setConversions([
DateTime::class => DateTimeConverter::class,
]);
$user = UserDto::make(['birthday' => '01/01/2000']);
$user->birthday; // instance of DateTime
$user->toArray(); // ['birthday' => '01/01/2000']