PHP code example of wakeapp / dto-resolver

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);

$dto = new AcmeUserDto([
    'email' => '[email protected]', 
    'username' => 'test_user', 
    'fullName' => 'Test User'
]);

echo $dto->getUsername(); // test_user
echo $dto->getEmail(); // [email protected]
echo $dto->getFullName(); // Test User

echo json_encode($dto); 
// {"email":"[email protected]","username":"test_user","fullName":"Test User"}

 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;
    }
}

 declare(strict_types=1);

/** @var \Wakeapp\Component\DtoResolver\Dto\CollectionDtoResolverInterface $collectionDto */
$collectionDto = new AcmeUserCollectionDto();
$collectionDto->add([
    'email' => '[email protected]',
    'username' => '1_test_user',
    'fullName' => '1 Test User'
]);
$collectionDto->add([
    'email' => '[email protected]',
    'username' => '2_test_user',
    'fullName' => '2 Test User'
]);

echo json_encode($collectionDto);
// [
//      {"email":"[email protected]","username":"1_test_user","fullName":"1 Test User"},
//      {"email":"[email protected]","username":"2_test_user","fullName":"2 Test User"}
// ]

 declare(strict_types=1);

$customResolver = new \Symfony\Component\OptionsResolver\OptionsResolver();
$entryDto = new AcmeUserDto(['email' => '[email protected]'], $customResolver);
$collectionDto = new AcmeUserCollectionDto($customResolver);

 declare(strict_types=1);

$collectionDto = new AcmeUserCollectionDto(null, 'email');
$collectionDto->add([
    'email' => '[email protected]',
     'username' => '1_test_user',
     'fullName' => '1 Test User'
 ]);
$collectionDto->add([
    'email' => '[email protected]',
    'username' => '2_test_user',
    'fullName' => '2 Test User'
]);