anteris-dev/faker-map
Composer 安装命令:
composer require anteris-dev/faker-map
包简介
Guesses which Faker provider method you might need based on the name passed.
README 文档
README
Have you ever wanted faker to just create some values for you automatically without having to instruct it on what they represent? Yeah, probably not, but at least you now know it's possible!
This package allows you to dynamically lookup the method you might be looking for by doing something like this: FakerMap::new()->closest('male name')->fake().
So how do you install it? How does it work? Let's dive in!
To Install
Run composer require anteris-dev/faker-map.
Getting Started
use Anteris\FakerMap\FakerMap; use Faker\Factory; // FakerMap will handle creating a faker instance for you here. $map = FakerMap::new(); // Or you can pass in your own instance. This is handy for seeding! $faker = Factory::create(); $faker->seed(123); $map = new FakerMap($faker);
This FakerMap class comes with three useful methods for dynamically figuring out what value should be generated. The first of these methods is closeEnough(). This method takes a single string and finds the first possible Faker method that even comes close to matching it.
The second method is closest(). This also takes a single string but it tries to find the closest possible match to the Faker value you are looking for.
Finally, the guess() method allows you to determine how close or distant the match is. Its first parameter is the query and the second is an integer that defines how many letters may be changed in your query to make the match.
Each of these methods returns an instance of Anteris\FakerMap\FakerMap which gives you various addon methods that can be called to help you with your search for the perfect method. These methods are:
default($value)- Lets you define a default value in case we cannot find an appropriate method on the Faker class.type(string $type)- Lets you define the type the returned value should have. If the faked value doesn't match the type, null or default will be returned instead.fake(...$parameters)- Takes all the information gathered and returns the end result. Any parameters passed will be used in the faker method.faker()- Returns the underlying instance of faker for any use you may have for it.
See examples of all of these below:
FakerMap::new()->closeEnough('female name')->fake(); FakerMap::new()->closest('female name')->fake(); FakerMap::new()->guess('female name', 3)->fake(); // A guess that passes some parameters // Returns: A number between 1 and 40 FakerMap::new()->closest('number between')->fake(1, 40); // A guess that definitely won't have a match so it defaults to "Yikes!" FakerMap::new()->closest('some unknown value')->default('Yikes!')->fake(); // Passes on to the underlying faker instance. FakerMap::new()->faker()->firstNameFemale();
But why?
Why did we choose to create this package? Well, we wanted to be able to fake realistic values based on the names of properties retrieved using PHPs reflection API. This would allow us to fill the following class with realistic data. Pretty cool right?
class Person { public string $firstName; public string $lastName; public string $companyName; public string $email; public string $address1; public string $address2; public string $socialSecurityNumber; }
Reference
There are some cool packages out there that do some cool things with Faker. While it did not suit our needs, Jason McCreary put together a nice Faker Registry package that does some similar things for generating Laravel Factory files. It may be worth checking out here. His registry array inspired our FakerDictionary class.
anteris-dev/faker-map 适用场景与选型建议
anteris-dev/faker-map 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 241.1k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 12 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 anteris-dev/faker-map 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 anteris-dev/faker-map 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 241.1k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-12-04