jdcode/dto-php
最新稳定版本:v1.0.0
Composer 安装命令:
composer require jdcode/dto-php
包简介
Simple and flexible Data Transfer Object library with validations
README 文档
README
Dto-Php
Simple and flexible Data Transfer Object library with validations
Installation
Composer
composer require jdcode/dto-php
Usage
Class Dto
use JDCode\DtoPhp\DtoPhp; class StoreUserDto extends DtoPhp { public string $username; public string $password; public int $age; }
In your UseCase class
$dto = new StoreUserDto([ 'username' => 'admin', 'password => '123456' 'age' => 99 ]);
Validation
Currently it only has 3 basic validations
Class Dto
use JDCode\DtoPhp\DtoPhp; use JDCode\DtoPhp\Validations\MinLength; use JDCode\DtoPhp\Validations\MaxLength; use JDCode\DtoPhp\Validations\Email; class StoreUserDto extends DtoPhp { #[MinLength(length: 3), MaxLength(length: 10)] public string $username; public string $password; #[Email] public string $email public int $age; }
You can use $dto->validated() to know if the validation was correct and $dto->getErrors() to get the error messages.
Type Class object
If your property type is a DTO object, just placing it will automatically cast it.
Class Dto
use JDCode\DtoPhp\DtoPhp; class StoreUserDto extends DtoPhp { public string $username; public string $password; public int $age; public RoleDto $role }
use JDCode\DtoPhp\DtoPhp; class RoleDto extends DtoPhp { public int $id; public string $name; }
In your UseCase class
$dto = new StoreUserDto([ 'username' => 'admin', 'password => '123456' 'age' => 99, 'roles' => [ 'id' => 1, 'name' => 'Administrator' ] ]);
Array Object
If you need an object array, just by placing the corresponding attribute you will get your object array.
use JDCode\DtoPhp\DtoPhp; class RoleDto extends DtoPhp { public int $id; public string $name; }
use JDCode\DtoPhp\DtoPhp; use JDCode\DtoPhp\Casters\CastArrayWithObject; class StoreUserDto extends DtoPhp { public string $username; public string $password; #[CastArrayWithObject(Role::class)] public array $roles; }
In your UseCase class
$dto = new StoreUserDto([ 'username' => 'admin', 'password => '123456' 'age' => 99, 'roles' => [ [ 'id' => 1, 'name' => 'Administrator' ], [ 'id' => 2, 'name' => 'Moderator' ] ] ]); var_dump($dto->roles) // returns RoleDto[]
License
MIT
统计信息
- 总下载量: 39
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-06-22