PHP code example of friendsofhyperf / validated-dto

1. Go to this page and download the library: Download friendsofhyperf/validated-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/ */

    

friendsofhyperf / validated-dto example snippets




namespace App\DTO;

class UserDTO extends ValidatedDTO
{
    protected function rules(): array
    {
        return [
            'name'     => ['

$dto = UserDTO::fromArray([
    'name' => 'Deeka Wong',
    'email' => '[email protected]',
    'password' => 'D3Crft!@1b2A'
]);

$dto = UserDTO::fromJson('{"name": "Deeka Wong", "email": "[email protected]", "password": "D3Crft!@1b2A"}');

public function store(RequestInterface $request): JsonResponse
{
    $dto = UserDTO::fromRequest($request);
}

$user = new User([
    'name' => 'Deeka Wong',
    'email' => '[email protected]',
    'password' => 'D3Crft!@1b2A'
]);

$dto = UserDTO::fromModel($user);



use App\DTO\UserDTO;
use Hyperf\Command\Command;

class CreateUserCommand extends Command
{
    protected ?string $signature = 'create:user {name} {email} {password}';

    protected string $description = 'Create a new User';

    /**
     * Execute the console command.
     *
     * @return int
     *
     * @throws ValidationException
     */
    public function handle()
    {
        $dto = UserDTO::fromCommandArguments($this);
    }
}



use App\DTO\UserDTO;
use Hyperf\Command\Command;

class CreateUserCommand extends Command
{
    protected ?string $signature = 'create:user { --name= : The user name } { --email= : The user email } { --password= : The user password }';

    protected string $description = 'Create a new User';

    /**
     * Execute the console command.
     *
     * @return int
     * @throws ValidationException
     */
    public function handle()
    {
        $dto = UserDTO::fromCommandOptions($this);
    }
}



use App\DTO\UserDTO;
use Hyperf\Command\Command;

class CreateUserCommand extends Command
{
    protected ?string $signature = 'create:user {name}
                                        { --email= : The user email }
                                        { --password= : The user password }';

    protected string $description = 'Create a new User';

    /**
     * Execute the console command.
     *
     * @return int
     *
     * @throws ValidationException
     */
    public function handle()
    {
        $dto = UserDTO::fromCommand($this);
    }
}

$dto = UserDTO::fromArray([
    'name' => 'Deeka Wong',
    'email' => '[email protected]',
    'password' => 'D3Crft!@1b2A'
]);

$dto->name; // 'Deeka Wong'
$dto->email; // '[email protected]'
$dto->password; // 'D3Crft!@1b2A'

$dto = UserDTO::fromArray([
    'name' => 'Deeka Wong',
    'email' => '[email protected]',
    'password' => 'D3Crft!@1b2A',
    'username' => 'john_doe', 
]);

$dto->username; // THIS WON'T BE AVAILABLE IN YOUR DTO



namespace App\DTO;

use Hyperf\Stringable\Str;

class UserDTO extends ValidatedDTO
{
    protected function rules(): array
    {
        return [
            'name'     => [' defaults(): array
    {
        return [
            'username' => Str::snake($this->name),
        ];
    }
}

$dto = UserDTO::fromArray([
    'name' => 'Deeka Wong',
    'email' => '[email protected]',
    'password' => 'D3Crft!@1b2A'
]);

$dto->username; // 'deeka_wong'

$dto = UserDTO::fromArray([
    'name' => 'Deeka Wong',
    'email' => '[email protected]',
    'password' => 'D3Crft!@1b2A',
]);

$dto->toArray();
// [
//     "name" => "Deeka Wong",
//     "email" => "[email protected]",
//     "password" => "D3Crft!@1b2A",
// ]

$dto = UserDTO::fromArray([
    'name' => 'Deeka Wong',
    'email' => '[email protected]',
    'password' => 'D3Crft!@1b2A',
]);

$dto->toJson();
// '{"name":"Deeka Wong","email":"[email protected]","password":"D3Crft!@1b2A"}'

$dto->toJson(true); // YOU CAN CALL IT LIKE THIS TO PRETTY PRINT YOUR JSON
// {
//     "name": "Deeka Wong",
//     "email": "[email protected]",
//     "password": "D3Crft!@1b2A"
// }

$dto = UserDTO::fromArray([
    'name' => 'Deeka Wong',
    'email' => '[email protected]',
    'password' => 'D3Crft!@1b2A',
]);

$dto->toModel(\App\Model\User::class);
// App\Model\User {#3776
//     name: "Deeka Wong",
//     email: "[email protected]",
//     password: "D3Crft!@1b2A",
// }


/**
 * Defines the custom messages for validator errors.
 */
protected function messages() {
    return [];
}

/**
 * Defines the custom attributes for validator errors.
 */
protected function attributes() {
    return [];
}

/**
 * Defines the type casting for the properties of the DTO.
 *
 * @return array
 */
protected function casts(): array
{
    return [
        'name' => new StringCast(),
        'age'  => new IntegerCast(),
        'created_at' => new CarbonImmutableCast(),
    ];
}

protected function casts(): array
{
    return [
        'property' => new ArrayCast(),
    ];
}

protected function casts(): array
{
    return [
        'property' => new BooleanCast(),
    ];
}

protected function casts(): array
{
    return [
        'property' => new CarbonCast(),
    ];
}

protected function casts(): array
{
    return [
        'property' => new CarbonCast('Europe/Lisbon'),
    ];
}

protected function casts(): array
{
    return [
        'property' => new CarbonCast('Europe/Lisbon', 'Y-m-d'),
    ];
}

protected function casts(): array
{
    return [
        'property' => new CarbonImmutableCast(),
    ];
}

protected function casts(): array
{
    return [
        'property' => new CarbonImmutableCast('Europe/Lisbon'),
    ];
}

protected function casts(): array
{
    return [
        'property' => new CarbonImmutableCast('Europe/Lisbon', 'Y-m-d'),
    ];
}

protected function casts(): array
{
    return [
        'property' => new CollectionCast(),
    ];
}

protected function casts(): array
{
    return [
        'property' => new CollectionCast(new IntegerCast()),
    ];
}

protected function casts(): array
{
    return [
        'property' => new DTOCast(UserDTO::class),
    ];
}

protected function casts(): array
{
    return [
        'property' => new FloatCast(),
    ];
}

protected function casts(): array
{
    return [
        'property' => new IntegerCast(),
    ];
}

protected function casts(): array
{
    return [
        'property' => new ModelCast(User::class),
    ];
}

protected function casts(): array
{
    return [
        'property' => new ObjectCast(),
    ];
}

protected function casts(): array
{
    return [
        'property' => new StringCast(),
    ];
}

/**
 * Casts the given value.
 *
 * @param  string  $property
 * @param  mixed  $value
 * @return mixed
 */
public function cast(string $property, mixed $value): mixed;

class URLCast implements Castable
{
    /**
     * @param  string  $property
     * @param  mixed  $value
     * @return URLWrapper
     */
    public function cast(string $property, mixed $value): URLWrapper
    {
        return new URLWrapper($value);
    }
}

class CustomDTO extends ValidatedDTO
{
    protected function rules(): array
    {
        return [
            'url' => [' array
    {
        return [
            'url' => new URLCast(),
        ];
    }
}
shell
php bin/hyperf.php gen:dto UserDTO