ok/dto-annotation-mapper
Composer 安装命令:
composer require ok/dto-annotation-mapper
包简介
Data mapper
README 文档
README
This library intended for auto fill entity object from input dataset based on annotations.
Purpose
This library could be useful if you have several cases, when you fill entity from an array data.
For example, you retrieve data from client and create new entity and another way you get this entity from import file.
Instead of create separate services to filling entity from array data set you could just specify entity fields for filling and pass only those properties which you need.
Requirements
- PHP 7.4+
- Doctrine ORM 2+
Installation
You can install the package via composer:
composer require ok/dto-annotation-mapper
For Symfony project:
Register AnnotationMapper as service in services.yml:
OK\Dto\MapperInterface: class: OK\Dto\AnnotationMapper autowire: true
For other just init new mapper object
use Doctrine\Common\Annotations\AnnotationReader; $mapper = new AnnotationMapper(new AnnotationReader(), $entityManager);
Usage
At first you need to mark your setters in doctrine entity with DTO annotation
/** * @ORM\Entity(repositoryClass="App\Repository\ProductRepository") */ class Product { /** * @var string * * @ORM\Column(type="string", nullable=true) */ protected $article; /** * @var Material * * @ORM\ManyToOne(targetEntity="App\Entity\Product\Options\Material") */ protected $material; /** * @DTO(name="article", type="string") */ public function setArticle(?string $article) { $this->article = $article; } /** * @DTO(name="material", type="App\Entity\Product\Options\Material", relation="ManyToOne") */ public function setMaterial(?Material $material) { $this->material = $material; }
Then just pass input data with object to fillObject method in controller like this (Example for Symfony project, but you can use it with any framework or without it):
/** * @Route("/products/{id}", name="api_product_patch", methods={"PATCH"}) */ public function updateProductAction(Request $request, Product $product, MapperInterface $mapper): JsonResponse { $data = $this->getRequestData($request); $updatedProduct = $mapper->fillObject($product, $data); /* Do validation and other actions if need */ $this->getDoctrine()->getManager()->flush(); return $this->json($updatedProduct); }
That's it! Your entity will updated with new data.
Available simple types: string, float, bool, boolean, int, integer, datetime, array. Trying to use other types without relation will throw the OK\Dto\Exception\MapperInvalidTypeException.
You can use any entity class as type, you just need to specify relation for it: ManyToOne, OneToMany, ManyToMany
Testing
composer install vendor/bin/phpunit tests
Information
Basic usage
All useful information will be retrieved from Doctrine annotations if property set or type is not set in DTO annotation.
@DTO(name="material") @DTO(name="material_name", property="material")
Using only simple types
If you don't use Doctrine\orm you could use this mapper anyway.
Just create mapper without the second parameter and with you own Reader implementation if need.
new \OK\Dto\AnnotationMapper(new \Doctrine\Common\Annotations\AnnotationReader());
And then you can use only the simple types.
Case sensitivity
You can use both snake_case and camelCase in name field and mapper will check both of variants in dataset if doesn't find the strict key. For example:
If you have annotation:
@DTO(name="customerNumber", type="string")
and dataset:
$data = ['customer_number' => 123];
It's fine. At first mapper check the strict name customerNumber, then (if name doesn't exist) transforms name to customer_number and check it again.
It works other way too (from snake_case to camelCase).
Several naming
If you need to have several names for one Dto annotation just list them via | delimiter and then you can use different name in data for different cases:
@DTO(name="customerNumber|userNumber|managerNumber", type="string")
If in dataset existed more then one from this set of names, then mapper will return first found.
Using ManyToMany relation
For using ManyToMany relation you need to implement OK\Dto\Repository\SearchCollectionInterface for you entity repository or you can just extends from OK\Dto\Repository\EntityRepository
Using OneToMany relation
Here are two ways to use this relation:
The first one when related entity existed then you need to pass array of id to dataset:
$data = ['customers' => [1,2,3]];
Another way when you need to create new Entity, then you need to pass data to create:
$data = ['customers' => [['firstName' => 'John', 'lastName' => 'Smith']]];
You can combine both this way in one dataset:
$data = ['customers' => [1, 2, ['firstName' => 'John', 'lastName' => 'Smith']];
License
The MIT License (MIT). Please see License File for more information.
ok/dto-annotation-mapper 适用场景与选型建议
ok/dto-annotation-mapper 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.22k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2021 年 03 月 11 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「symfony」 「api」 「doctrine」 「mapper」 「dto」 「filling data」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 ok/dto-annotation-mapper 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ok/dto-annotation-mapper 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 ok/dto-annotation-mapper 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
The bundle for easy using json-rpc api on your project
A PSR-7 compatible library for making CRUD API endpoints
Make pimcore migration simple
Bundle Symfony DaplosBundle
Plugins for Tactician commands using Doctrine, like wrapping every command in a transaction
Use FileMaker through CWP as your backend database
统计信息
- 总下载量: 1.22k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 12
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-03-11