pfilsx/dto-param-converter-bundle

此Symfony包提供了一种简单地将请求转换为DTO的方法

安装次数: 65,040

依赖项: 0

建议者: 0

安全: 0

星标: 8

关注者: 2

分支: 1

开放问题: 0

类型:symfony-bundle

v2.0.3 2023-03-27 12:27 UTC

This package is auto-updated.

Last update: 2024-09-07 11:41:38 UTC


README

PHP Version Require Latest Stable Version
Tests Total Downloads

描述

该包提供了一种简单的方法将请求映射到DTO(数据传输对象),验证并将其注入到您的Symfony项目控制器中。它自动将请求内容反序列化为提供的DTO,如果需要则验证它,并将DTO注入到您的控制器参数中(Symfony参数解析器),最后您在控制器中就有一个完全有效的DTO。

功能

  • 使用可配置序列化器将请求反序列化为DTO
  • 使用Symfony验证器自动可配置的验证
  • 通过注解/PHP8属性(预加载、序列化器、验证器选项等)轻松配置每个请求/DTO的转换器选项
  • 在请求反序列化之前将实体预加载到DTO中

要求

  • PHP 7.4+|8.x
  • Symfony 4.4+|5.3+|6.0+

安装

打开命令行,进入您的项目目录并执行以下命令以下载此包的最新版本

composer require pfilsx/dto-param-converter-bundle

将包注册到config/bundles.php(Flex已自动完成)

return [
    ...
    Pfilsx\DtoParamConverter\DtoParamConverterBundle::class => ['all' => true],
];

文档

文档可以在这里找到。

用法

  1. 使用转换器注解/属性创建DTO类
use Pfilsx\DtoParamConverter\Annotation\Dto;
use Symfony\Component\Validator\Constraints as Assert;

/**
* @Dto() 
*/
final class SomeDto 
{
  /**
  * @Assert\NotBlank
  */
  public ?string $title = null;
  
  ...
}
  1. 在您的控制器中使用DTO
public function postAction(SomeDto $someDto): Response
{
    // here dto already loaded and validated
}
  1. (如果需要预加载)将DTO与实体链接
/**
* @Dto(linkedEntity=SomeEntity::class) 
*/
final class SomeDto 
{
    ...
}
  1. (如果需要预加载)创建实体-DTO映射器
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();
        ...
    }
}

配置

您可以通过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

或为每个操作配置转换器

/**
* @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
{
    ...
}

许可证

此包是在MIT许可证下发布的。

贡献

如果您想做出贡献,请随时提出拉取请求、创建问题或直接联系我 :)