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' => ['
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);
}
}
/**
* 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
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.