FakerPHP / Faker
source link: https://fakerphp.github.io/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Faker#
Faker is a PHP library that generates fake data for you. Whether you need to bootstrap your database, create good-looking XML documents, fill-in your persistence to stress test it, or anonymize data taken from a production service, Faker is for you.
It's heavily inspired by Perl's Data::Faker, and by Ruby's Faker.
Installation#
Faker requires PHP >= 7.1.
Basic Usage#
Autoloading#
Faker supports both PSR-0
as PSR-4
autoloaders.
You can also load Fakers
shipped PSR-0
autoloader
alternatively, you can use any other PSR-4 compliant autoloader
Create fake data#
Use Faker\Factory::create()
to create and initialize a faker generator, which can generate data by calling methods named after the type of data you want.
Each call to $faker->name()
yields a different (random) result. This is because Faker uses __call()
magic, and forwards Faker\Generator->$method()
calls to Faker\Generator->format($method, $attributes)
.
Modifiers#
Faker provides three special providers, unique()
, optional()
, and valid()
, to be called before any provider.
If you would like to use a modifier with a value not generated by Faker, use the passthrough()
method. passthrough()
simply returns whatever value it was given.
Localization#
Faker\Factory
can take a locale as an argument, to return localized data. If no localized provider is found, the
factory falls back to the default locale (en_US).
You can check available Faker locales in the source code, under the Provider
directory. The localization of Faker is an ongoing process, for which we need your help. Don't hesitate to create localized providers to your own locale and submit a PR!
Seeding the Generator#
You may want to always get the same generated data - for instance when using Faker for unit testing purposes. The generator offers a seed()
method, which seeds the random number generator. Calling the same script twice with the same seed produces the same results.
Tip
DateTime formatters won't reproduce the same fake data if you don't fix the $max
value:
Tip
Formatters won't reproduce the same fake data if you use the rand()
php function. Use $faker
or mt_rand()
instead:
Faker Internals: Understanding Providers#
A Faker\Generator
alone can't do much generation. It needs Faker\Provider
objects to delegate the data generation to them. Faker\Factory::create()
actually creates a Faker\Generator
bundled with the default providers. Here is what happens under the hood:
Whenever you try to access a property on the $faker
object, the generator looks for a method with the same name in all the providers attached to it. For instance, calling $faker->name
triggers a call to Faker\Provider\Person::name()
. And since Faker starts with the last provider, you can easily override existing formatters: just add a provider containing methods named after the formatters you want to override.
That means that you can easily add your own providers to a Faker\Generator
instance. A provider is usually a class extending \Faker\Provider\Base
. This parent class allows you to use methods like lexify()
or randomNumber()
; it also gives you access to formatters of other providers, through the protected $generator
property. The new formatters are the public methods of the provider class.
Here is an example provider for populating Book data:
To register this provider, just add a new instance of \Faker\Provider\Book
to an existing generator:
Now you can use the two new formatters like any other Faker formatter:
Tip
A provider can also be a Plain Old PHP Object. In that case, all the public methods of the provider become available to the generator.
Language specific formatters#
Supported locales can be found under the "Locales" header on the left.
Misnamed locales#
Current name | Correct name |
---|---|
at_AT |
de_AT |
zh_CN |
zh_Hans_CN |
zh_TW |
zh_Hant_TW |
Source: https://www.localeplanet.com/icu/
License#
Faker is released under the MIT License. See the bundled LICENSE file for details.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK