feffel/felfactory
Composer 安装命令:
composer require feffel/felfactory
包简介
README 文档
README
felfactory is a library that generates objects full of fake data for you. Whether you need to randomize test data, bootstrap your database, fill-in your persistence to stress test it, or anonymize data taken from a production service.
Powered by Faker's data generators.
Table of Contents
Installation
via composer
composer require feffel/felfactory
Basic Usage
Pass a class name to your factory instance, it will create and fill properties with the appropriate type of data.
class Person { private $firstName; private $lastName; private $address; } $factory = new \felfactory\Factory(); $person = $factory->generate(Person::class); var_dump($person); // class Person#2407 (3) { // private $firstName => string(6) "Breana" // private $lastName => string(7) "Okuneva" // private $address => string(43) "37382 Chanel Point Steuberchester, AR 83395" // }
Configuration
Configuration uses enviornment variables, add these to your environment or add a .env file to your root project dir.
| Variable | Default | Description |
|---|---|---|
| FACTORY_MAX_NEST_LEVEL | 3 | The maximum allowed level of object nesting, any objects found after this level will be ignored and set to null |
| FACTORY_CIRCLE_TOLERANCE | 1 | Circular references tolerance, default 1 does not allow the generation of any circular or self refrencing objects |
| FACTORY_PHP_FILE | null | Php file path for model definitions |
| FACTORY_YAML_FILE | null | Yaml file path for model definitions |
Model Definitions
You can provide model definitions to customize the generation of a model, the definition does not have to contain all of the properties, the factory will still guess the missing properties.
- Generate accepts a faker generator property [eg: firstName, lastName, phoneNumber, ...]
- Value accepts any php value and passes it down to property [eg: "string value", 15, null, ...]
- ObjectOf accepts a FQCN and generates an object of this type [eg: namespace\models\Person , Person::class, ...]
- ManyOf accepts any of the previous definitions as it's first parameter and generates an array of it bound by the inclusive range provided by it's second and third parameter.
Annotation definition
use felfactory\Annotation as FCT; class AnnotatedModel { /** * @FCT\Generate("firstName") */ public $firstName; /** * @FCT\Value("""felfel""") */ protected $lastName; /** * @FCT\ObjectOf(AddressModel::class) */ public $address; /** * @FCT\ManyOf(@FCT\Generate("phoneNumber"), 1, 3) */ public $phoneNos; }
Php defintion
return [ AnnotatedModel::class => [ 'firstName' => "generate('firstName')", 'lastName' => "value('\"felfel\"')", 'address' => "class(felfactory\models\AddressModel)", 'phoneNos' => "many(generate('phoneNumber'), 1, 3)" ], ];
How it works
The factory does not use the class's original constructor, nor the provided setters if any. All of the initiation process is handled by reflections.
The factory looks for the @var annotation on a property to determine it's type, if a definition is found for the property it will be used, otherwise it will be guessed based on type and name of the property.
Objects
If a property is found to be an object it will trigger another factory call to generate it, and the name will be ignored. Interfaces and abstract types will not be generated automatically and will be set to null.
Scalar types and Non-annotated properties
Scalar types will be guessed based on the name of the property first, if it doesn't match any of the supported data generators, it's generated based on type.
Contributing
Please see CONTRIBUTING and CODE_OF_CONDUCT for details.
Credits
License
The MIT License (MIT). Please see License File for more information.
feffel/felfactory 适用场景与选型建议
feffel/felfactory 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7 次下载、GitHub Stars 达 1, 最近一次更新时间为 2019 年 04 月 29 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「faker」 「fixtures」 「data」 「fake」 「generation」 「Dummy」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 feffel/felfactory 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 feffel/felfactory 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 feffel/felfactory 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Shoot aims to make providing data to your templates more manageable
Testing object factory for PHP
Testing object factory for PHP
Adds the EDTF data type to Wikibase
Plugin to compare fixtures with live DB tables
A simple library that allows transform any kind of data to native php data or whatever
统计信息
- 总下载量: 7
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 5
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-04-29