xactsystems/type-hint-hydrator

基于声明类型和注解类型提示的 Symfony 对象 hydrator。

安装: 775

依赖: 0

建议者: 0

安全: 0

星级: 0

关注者: 1

分支: 0

开放问题: 6

类型:symfony-bundle


README

一个使用声明类型和 @var 注解来确定 hydrate 类型信息的 Symfony 类型提示 hydrator。在底层,它使用 laminas-hydrator 进行映射过程。请参阅 https://github.com/laminas/laminas-hydrator/

它可以处理请求对象和数据数组,用于 hydrate 带注解的类和数据等。它还可以验证 hydrate 对象可能包含的任何 Assert 注解。

如果 hydrate 对象的属性被注解为 Doctrine 实体,hydrator 将尝试加载提供键值的实体。我们目前不支持复合键。

文档

1) 将 type-hint-hydrator 添加到您的项目中

composer require xactsystems/type-hint-hydrator

2) 将包添加到您的配置文件中

如果您从 Symfony 4 开始使用 Flex,则可以跳过此步骤。

Symfony 4.4 及以上版本 - bundles.php

    return [
        ...
        Xact\TypeHintHydrator\XactTypeHintHydrator::class => ['all' => true],
    ];

3) 声明类型或注解您的 hydrate 对象属性

namespace App;

class Author
{
    /**
     * @var int|null
     */
    public $objectId;

    public int $noneNullId;

    /**
     * @var string|null
     */
    public $fullName;

    public float $myFloat;

    /**
     * @var \App\Book[]
     */
    public $books;

    /**
     * @var \App\Authors[]
     */
    public array $authors;

    /**
     * @var array<\App\References>
     */
    public array $references;
}

4) 在您的控制器中 hydrate 对象

    use Xact\TypeHintHydrator\TypeHintHydrator;
    ...
    public function update(Request $request, EntityManagerInterface $em, TypeHintHydrator $hydrator): JsonResponse
    {
        $author = new Author();
        $hydrator->handleRequest($request, $author);
        if (!$hydrator->isValid()) {
            return JsonResponse::fromJsonString($hydrator->getJsonErrors(), JsonResponse::HTTP_BAD_REQUEST);
        }

        $em->persist($author);
        $em->flush();

        return new JsonResponse::fromJsonString(json_encode($author));
    }

或更新现有实体

    use Xact\TypeHintHydrator\TypeHintHydrator;
    ...
    /**
     * @Route("/author/{id}", methods={"POST"})
     * @ParamConverter("author", class="App\Entity\Author")
     */
    public function update(Author $author, Request $request, TypeHintHydrator $hydrator): Response
    {
        $hydrator->handleRequest($request, $author);
        if (!$hydrator->isValid()) {
            return JsonResponse::fromJsonString($hydrator->getJsonErrors(), JsonResponse::HTTP_BAD_REQUEST);
        }

        $em->persist($author);
        $em->flush();

        return new JsonResponse::fromJsonString(json_encode($author));
    }

5) 微笑并快乐

这真的很容易。不再需要表单类型!正确地使用类型和断言注解您的对象,确保提交的表单使用与对象属性相同的名称,它就会正常工作!

为您的数据创建合适的模型类并 hydrate 它们,让它们做 MVC 模型应该做的事情。

方法

hydrateObject

hydrateObject(array $values, object $target, bool $validate = true, $constraints = null, $groups = null): object

从值数组中 hydrate 对象。如果 $validate 为 true,则 hydrate 对象将根据注解和提供的验证约束和组进行验证。

handleRequest

handleRequest(Request $request, object $target, bool $validate = true, $constraints = null, $groups = null): object

从 Request 对象 hydrate 对象。属性映射基于提交的表单属性名称与 hydrate 对象的属性名称匹配。如果 $validate 为 true,则 hydrate 对象将根据注解和提供的验证约束和组进行验证。

isValid

isValid()

处理后验证约束后,hydrate 对象是否有效。如果没有发生验证,则该方法返回 true;

getErrors

getErrors()

返回任何验证错误的 Symfony\Component\Validator\ConstraintViolationListInterface 列表。

getJsonErrors

getJsonErrors()

返回 getErrors() 的 JSON 序列化版本。

致谢

许可证

此包在 MIT 许可证下发布。请在包中查看完整的许可证。

LICENSE