1. Go to this page and download the library: Download wakeapp/dto-resolver 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/ */
wakeapp / dto-resolver example snippets
declare(strict_types=1);
namespace AcmeBundle\Dto;
use Wakeapp\Component\DtoResolver\Dto\DtoResolverTrait;
use Wakeapp\Component\DtoResolver\Dto\DtoResolverInterface;
class AcmeUserDto implements DtoResolverInterface
{
use DtoResolverTrait;
/**
* @var string
*/
protected $email;
/**
* @var string|null
*/
protected $fullName = null;
/**
* @var string
*/
protected $username;
/**
* @return string
*/
public function getEmail(): string
{
return $this->email;
}
/**
* @return string|null
*/
public function getFullName(): ?string
{
return $this->fullName;
}
/**
* @return string
*/
public function getUsername(): string
{
return $this->username;
}
}
declare(strict_types=1);
namespace AcmeBundle\Dto;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Wakeapp\Component\DtoResolver\Dto\DtoResolverInterface;
use Wakeapp\Component\DtoResolver\Dto\DtoResolverTrait;
class AcmeUserDto implements DtoResolverInterface
{
use DtoResolverTrait;
// ...
/**
* {@inheritdoc}
*/
protected function configureOptions(OptionsResolver $options): void
{
$options->setRequired(['username']);
$options->addAllowedTypes('email', ['string', 'null']);
$options->addAllowedTypes('username', 'string');
}
}
declare(strict_types=1);
// ошибка: отсутвует обязательное смещение username
$entryDto = new AcmeUserDto([
'email' => '[email protected]'
]);
// ошибка: email имеет недопустимый тип
$entryDto = new AcmeUserDto([
'email' => 123,
'username' => 'test_user'
]);
// успех
$entryDto = new AcmeUserDto([
'email' => '[email protected]',
'username' => 'test_user'
]);
echo $entryDto->getUsername(); // test_user
echo $entryDto->getEmail(); // [email protected]
declare(strict_types=1);
namespace AcmeBundle\Dto;
use Wakeapp\Component\DtoResolver\Dto\CollectionDtoResolverTrait;
use Wakeapp\Component\DtoResolver\Dto\CollectionDtoResolverInterface;
class AcmeUserCollectionDto implements CollectionDtoResolverInterface
{
use CollectionDtoResolverTrait;
/**
* {@inheritdoc}
*/
public static function getItemDtoClassName(): string
{
return AcmeUserDto::class;
}
}