anteris-dev/data-transfer-object-factory
Composer 安装命令:
composer require anteris-dev/data-transfer-object-factory
包简介
Gives an easy-to-use API for mocking Data Transfer Objects.
README 文档
README
This package provides a fluent factory for Data Transfer Objects. Natively supported are POPOs (Plain Old PHP Objects), Getter / Setter Objects, and Spatie's Data Transfer Objects. If there is a DTO not represented, you can easily add an adapter to support it.
One thing that makes this package so powerful is that it integrates with Faker in an attempt to intelligently generate the correct content for your data based on its name. For example, a DTO with the property "$firstName" will get a Faker first name.
By default only built-in PHP types are supported, but this factory can easily be extended to support other types (e.g., Carbon) as well.
To Install
Run composer require anteris-dev/data-transfer-object-factory.
Getting Started
If you are simply using PHP default types in your DTOs, you can get started right away. Just pass your DTO FQDN to the static new() method. You can then use any of the following helper methods.
count()- Allows you to specify how many DTOs to be generated. They will be returned in an array.make()- Called when you are ready to generate the DTO. Returns the generated DTO[s].random()- Generates a random number of DTOssequence()- Alternates a specific state. (See below)state()- Manually sets properties based on the array of values passed.
Examples of these methods can be found below.
use Anteris\DataTransferObjectFactory\Factory; // Creates one DTO Factory::new(PersonData::class)->make(); // Creates two DTOs in an array Factory::new(PersonData::class)->count(2)->make(); // Sets the first name of every person to "Jim" Factory::new(PersonData::class) ->random() ->state([ 'firstName' => 'Jim', ]) ->make(); // Also sets the first name of every person to "Jim" Factory::dto(PersonData::class) ->random() ->make([ 'firstName' => 'Jim', ]); // Alternates the names of each person between "Jim" and "Susie" Factory::dto(PersonData::class) ->random() ->sequence( [ 'firstName' => 'Jim' ], [ 'firstName' => 'Susie' ] ) ->make();
Extending
Adapters
Adapters instruct the factory on how to retrieve properties for a specific type of class. Adapters must implement the Anteris\DataTransferObjectFactory\Adapter\AdapterInterface which requires the following methods.
handles(ReflectionClass $class)- Returns a bool if the adapter can handle the referenced reflection class.getProperties(ReflectionClass $class)- Returns a collection of properties found on the referenced reflection class.createClass(ReflectionClass $class, PropertyCollection $collection)- Creates and returns and instance of the reflection class using the properties passed.
To register an adapter on the factory, call its static registerAdapter() method. For example:
use Anteris\DataTransferObjectFactory\Factory; Factory::registerAdapter(new MyCustomAdapter);
For more information check out the Adapter directory in the source code.
Property Types
It used to be that you had to extend the factory class to utilize custom types. You can now do so through the static registerProvider() method on the PropertyFactory class. This method takes two arguments. The first should be the FQDN of the class you are providing (e.g. Carbon\Carbon) OR the built-in type (e.g. string). The second should be a callback that returns the generated value. This callback is passed two properties when called to assist in generating the value. The first is an instance of Anteris\FakerMap\FakerMap which can be used to help generate fake data. The second is the name of the property being generated or null if not provided.
For example, to support Carbon:
use Anteris\DataTransferObjectFactory\PropertyFactory; use Anteris\FakerMap\FakerMap; PropertyFactory::registerProvider('Carbon\Carbon', fn(FakerMap $fakerMap) => Carbon::parse( $fakerMap->closest('dateTime')->fake() ));
anteris-dev/data-transfer-object-factory 适用场景与选型建议
anteris-dev/data-transfer-object-factory 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 93.23k 次下载、GitHub Stars 达 40, 最近一次更新时间为 2020 年 09 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 anteris-dev/data-transfer-object-factory 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 anteris-dev/data-transfer-object-factory 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 93.23k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 40
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-09-04