tangwei / dto
php hyperf dto
Installs: 74 108
Dependents: 2
Suggesters: 0
Security: 0
Stars: 12
Watchers: 2
Forks: 6
Open Issues: 0
Requires
- php: >=8.1
- hyperf/di: ~3.0.0|~3.1.0
- hyperf/http-server: ~3.0.0|~3.1.0
- hyperf/validation: ~3.0.0|~3.1.0
- netresearch/jsonmapper: ~4.4.1
- phpdocumentor/reflection-docblock: ^5.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.0
- mockery/mockery: ^1.0
- phpstan/phpstan: ^1.0
- phpunit/phpunit: >=7.0
- symfony/property-access: ^5.0|^6.0
- symfony/serializer: ^5.0|^6.0
- symfony/var-dumper: ^5.1
- dev-master / 3.0.x-dev
- v3.0.7
- v3.0.6
- v3.0.5
- v3.0.4
- v3.0.3
- v3.0.2
- v3.0.1
- v3.0.0
- v3.0.0-beta6
- v3.0.0-beta5
- v3.0.0-beta4
- v3.0.0-beta3
- v3.0.0-beta2
- v3.0.0-beta1
- v2.2.0-beta2
- v2.2.0-beta1
- 2.1.x-dev
- v2.1.11
- v2.1.10
- v2.1.9
- v2.1.8
- v2.1.7
- v2.1.6
- v2.1.5
- v2.1.4
- v2.1.3
- v2.1.2
- v2.1.1
- v2.1.0
- 2.0.x-dev
- v2.0.0
- v2.0.0-beta1
- v1.0.3
- v1.0.2
- v1.0.0
- v0.3.0
- v0.2.2
- v0.2.1
- v0.2.0
- v0.1.0
- dev-data
This package is auto-updated.
Last update: 2024-11-09 07:24:20 UTC
README
基于 Hyperf 框架的 DTO 类映射
优点
- 请求参数映射到PHP类
- 代码可维护性好,扩展性好
- 支持数组,递归,嵌套
- 支持框架数据验证器
注意
- 模型类需要手工编写
- php >= 8.1
安装
composer require tangwei/dto
使用
1. 使用
注解
命名空间:
Hyperf\DTO\Annotation\Contracts
RequestBody
- 获取Body参数
public function add(#[RequestBody] DemoBodyRequest $request){}
RequestQuery
- 获取GET参数
public function add(#[RequestQuery] DemoQuery $request){}
RequestFormData
- 获取表单请求
public function fromData(#[RequestFormData] DemoFormData $formData){}
- 获取文件(和表单一起使用)
#[ApiFormData(name: 'photo', type: 'file')]
- 获取Body参数和GET参数
public function add(#[RequestBody] DemoBodyRequest $request, #[RequestQuery] DemoQuery $query){}
注意: 同一个方法不能同时存在RequestBody和RequestFormData注解
示例
控制器
#[Controller(prefix: '/demo')] #[Api(tags: 'demo管理', position: 1)] class DemoController extends AbstractController { #[ApiOperation(summary: '查询')] #[PostMapping(path: 'index')] public function index(#[RequestQuery] #[Valid] DemoQuery $request): Contact { $contact = new Contact(); $contact->name = $request->name; var_dump($request); return $contact; } #[PutMapping(path: 'add')] public function add(#[RequestBody] DemoBodyRequest $request, #[RequestQuery] DemoQuery $query) { var_dump($query); return json_encode($request, JSON_UNESCAPED_UNICODE); } #[PostMapping(path: 'fromData')] public function fromData(#[RequestFormData] DemoFormData $formData): bool { $file = $this->request->file('photo'); var_dump($file); var_dump($formData); return true; } #[GetMapping(path: 'find/{id}/and/{in}')] public function find(int $id, float $in): array { return ['$id' => $id, '$in' => $in]; } }
验证器
基于框架的验证
安装hyperf框架验证器hyperf/validation, 并配置(已安装忽略)
- 注解
Required
Between
Date
Email
Image
Integer
Nullable
Numeric
Url
Validation
- 校验生效
只需在控制器方法中加上 #[Valid] 注解
public function index(#[RequestQuery] #[Valid] DemoQuery $request){}
class DemoQuery { public string $name; #[Required] #[Integer] #[Between(1,5)] public int $num; }
- Validation
rule 支持框架所有验证
- 自定义验证注解
只需继承
Hyperf\DTO\Annotation\Validation\BaseValidation
即可
#[Attribute(Attribute::TARGET_PROPERTY)] class Image extends BaseValidation { protected $rule = 'image'; }
RPC 返回PHP对象
aspects.php中配置
return [ \Hyperf\DTO\Aspect\ObjectNormalizerAspect::class ]
当框架导入 symfony/serializer (^5.0) 和 symfony/property-access (^5.0) 后,并在 dependencies.php 中配置一下映射关系
use Hyperf\Serializer\SerializerFactory; use Hyperf\Serializer\Serializer; return [ Hyperf\Contract\NormalizerInterface::class => new SerializerFactory(Serializer::class), ];