pfilsx/dto-param-converter-bundle
Composer 安装命令:
composer require pfilsx/dto-param-converter-bundle
包简介
This Symfony bundle provides a way to convert requests into dto easily
README 文档
README
Description
The bundle provides a simple way to map requests into DTO(Data Transfer Object), validate and inject into Your Symfony project controller. It automatically deserealize request content into provided DTO, validates it (if required) and injects DTO into your controller argument(Symfony Argument Resolver), and finally you have a fully valid DTO in your controller.
Features
- Request deserialization into dto with configurable serializer
- Automatic configurable validation using Symfony validator
- Easy to configure converter options for each request/DTO via annotations/PHP8 attributes(preload, serializer, validator options etc)
- Entity preload into DTO before request deserialization
Requirement
- PHP 7.4+|8.x
- Symfony 4.4+|5.3+|6.0+
Installation
Open a command console, enter your project directory and execute the following command to download the latest version of this bundle:
composer require pfilsx/dto-param-converter-bundle
Register bundle into config/bundles.php (Flex did it automatically):
return [ ... Pfilsx\DtoParamConverter\DtoParamConverterBundle::class => ['all' => true], ];
Documentation
Documentation can be found here.
Usage
- Create DTO class with converter annotation/attribute
use Pfilsx\DtoParamConverter\Annotation\Dto; use Symfony\Component\Validator\Constraints as Assert; /** * @Dto() */ final class SomeDto { /** * @Assert\NotBlank */ public ?string $title = null; ... }
- Use DTO in your controller
public function postAction(SomeDto $someDto): Response { // here dto already loaded and validated }
- Link DTO with entity(if preload required)
/** * @Dto(linkedEntity=SomeEntity::class) */ final class SomeDto { ... }
- Create entity-dto mapper(if preload required)
use Pfilsx\DtoParamConverter\Contract\DtoMapperInterface; final class SomeDtoMapper implements DtoMapperInterface { public static function getDtoClassName(): string { return SomeDto::class; } /** * @param object|SomeEntity $entity * @param SomeDto|object $dto */ public function mapToDto(object $entity, object $dto): void { // your entity to dto mapping logic $dto->title = $entity->getTitle(); ... } }
Configuration
You can configure bundle globally via config/packages/dto_param_converter.yaml:
dto_param_converter: preload: # entity preload into dto configuration enabled: true # enable/disable entity preloading before request mapping methods: ['GET', 'PATCH'] # request methods that require the entity preload optional: false # if false the converter will throw NotFoundHttpException on entity for preloading not found otherwise it will ignore preloading entity_manager_name: null # entity manager name to use for entity preloading. useful on multiple managers serializer: # request deserialization configuration service: serializer # serializer should be used for request deserialization normalizer_exception_class: 'Pfilsx\DtoParamConverter\Exception\NotNormalizableConverterValueException' # exception class that should be thrown on normalization errors. not actual after 5.4 symfony/serializer strict_types: # types enforcement on denormalization enabled: true excluded_methods: ['GET'] # excluded request methods for types enforcement validation: # dto validation configuration enabled: true # enable/disable validation of dto excluded_methods: ['GET'] # excluded request methods for validation exception_class: 'Pfilsx\DtoParamConverter\Exception\ConverterValidationException' # exception class that should be thrown on validation errors
Or You can configure converter for each action
/** * @DtoResolver(options={ * DtoArgumentResolver::OPTION_SERIALIZER_CONTEXT: {}, * DtoArgumentResolver::OPTION_VALIDATOR_GROUPS: {}, * DtoArgumentResolver::OPTION_PRELOAD_ENTITY: true, * DtoArgumentResolver::OPTION_STRICT_PRELOAD_ENTITY: true, * DtoArgumentResolver::OPTION_ENTITY_ID_ATTRIBUTE: null, * DtoArgumentResolver::OPTION_ENTITY_MANAGER: null, * DtoArgumentResolver::OPTION_ENTITY_MAPPING: {} * DtoArgumentResolver::OPTION_ENTITY_EXPR: null, * DtoArgumentResolver::OPTION_VALIDATE: false * }) */ public function someAction(SomeDto $someDto): Response { ... }
License
This bundle is released under the MIT license.
Contribute
If you'd like to contribute, feel free to propose a pull request, create issue or just contact me :)
pfilsx/dto-param-converter-bundle 适用场景与选型建议
pfilsx/dto-param-converter-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 101.84k 次下载、GitHub Stars 达 7, 最近一次更新时间为 2021 年 03 月 12 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「symfony」 「rest」 「api」 「bundle」 「param-converter」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 pfilsx/dto-param-converter-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 pfilsx/dto-param-converter-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 pfilsx/dto-param-converter-bundle 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
The bundle for easy using json-rpc api on your project
A PSR-7 compatible library for making CRUD API endpoints
Api bundle
Bundle Symfony DaplosBundle
A Flickr wrapper to allow you to call the Flickr api with Guzzle as the backend.Goal is to have 100% Flickr api coverage rather than just upload/display photos (currently at 23%).
BlockCypher's PHP SDK for REST API
统计信息
- 总下载量: 101.84k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 7
- 点击次数: 38
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-03-12