xactsystems/type-hint-hydrator
Composer 安装命令:
composer require xactsystems/type-hint-hydrator
包简介
A Symfony object hydrator based on declared types and annotation type-hints.
README 文档
README
A Symfony Type Hint hydrator that uses declared types and @var annotations to determine the hydrated type. Under the hood it uses the laminas-hydrator for the mapping process. See https://github.com/laminas/laminas-hydrator/
It can handle request objects and arrays of data to hydrate annotated classes and arrays etc. It can also validate against any Assert annotations the hydrated object may contain.
If properties of the hydrated object are annotated as Doctrine Entities, the hydrator will attempt to load the entity for the key value provided. We currently don't support composite keys.
Documentation
1) Add the type-hint-hydrator to your project
composer require xactsystems/type-hint-hydrator
2) Add the bundle to your configuration file
If you are using Symfony 4 onwards and using Flex you can skip this step.
Symfony 4.4 onwards - bundles.php
return [ ... Xact\TypeHintHydrator\XactTypeHintHydrator::class => ['all' => true], ];
3) Declare types or annotate your hydrated object properties
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 your object in your controller
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)); }
Or to update and existing entity:
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) Smile and be happy
It really is that easy. No more form types! Annotate your objects correctly with types and assertions, make sure your submitted forms use the same names as your object proprieties and it will just work!
Create proper Model classes for your data and hydrate them, and let them do the work a proper MVC model should.
Methods
hydrateObject
hydrateObject(array $values, object $target, bool $validate = true, $constraints = null, $groups = null): object
Hydrate an object from an array of values. If $validate is true, the hydrated object is validated against annotations and supplied validation constraints and groups.
handleRequest
handleRequest(Request $request, object $target, bool $validate = true, $constraints = null, $groups = null): object
Hydrate an object from the Request object. Property mapping is based on the submitted form property names matching the property names of the hydrated object. If $validate is true, the hydrated object is validated against annotations and supplied validation constraints and groups.
isValid
isValid()
Is the hydrated object valid after processing the validation constraints. If no validation has occurred the method returns true;
getErrors
getErrors()
Return a Symfony\Component\Validator\ConstraintViolationListInterface list of any validation errors.
getJsonErrors
getJsonErrors()
Return a JSON serialised version of getErrors().
Credits
- Ian Foulds as the creator of this package.
- Marco Pivetta (https://github.com/ocramius) for developing and maintaining the laminas-hydrator - https://github.com/laminas/laminas-hydrator.
License
This bundle is released under the MIT license. See the complete license in the bundle:
xactsystems/type-hint-hydrator 适用场景与选型建议
xactsystems/type-hint-hydrator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 977 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 11 月 01 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「symfony」 「doctrine」 「array」 「annotation」 「object」 「request」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 xactsystems/type-hint-hydrator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 xactsystems/type-hint-hydrator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 xactsystems/type-hint-hydrator 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A set of useful PHP classes.
The bundle for easy using json-rpc api on your project
Trait providing methods to set class properties with an array.
Traits to build collections of specific objects
This adds functions about array. If you feel like there few php built-in functions about array, this will be useful.
Convert array or camel case to underscore, or underscore to others.
统计信息
- 总下载量: 977
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 15
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-11-01