| 148 |
lars |
1 |
<p style="text-align: center"><img src="https://github.com/FakerPHP/Artwork/raw/main/src/socialcard.png" alt="Social card of FakerPHP"></p>
|
|
|
2 |
|
|
|
3 |
# Faker
|
|
|
4 |
|
|
|
5 |
[](https://packagist.org/packages/fakerphp/faker)
|
|
|
6 |
[](https://github.com/FakerPHP/Faker/actions)
|
|
|
7 |
[](https://shepherd.dev/github/FakerPHP/Faker)
|
|
|
8 |
[](https://codecov.io/gh/FakerPHP/Faker)
|
|
|
9 |
|
|
|
10 |
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.
|
|
|
11 |
|
|
|
12 |
It's heavily inspired by Perl's [Data::Faker](https://metacpan.org/pod/Data::Faker), and by Ruby's [Faker](https://rubygems.org/gems/faker).
|
|
|
13 |
|
|
|
14 |
## Getting Started
|
|
|
15 |
|
|
|
16 |
### Installation
|
|
|
17 |
|
|
|
18 |
Faker requires PHP >= 7.4.
|
|
|
19 |
|
|
|
20 |
```shell
|
|
|
21 |
composer require fakerphp/faker
|
|
|
22 |
```
|
|
|
23 |
|
|
|
24 |
### Documentation
|
|
|
25 |
|
|
|
26 |
Full documentation can be found over on [fakerphp.github.io](https://fakerphp.github.io).
|
|
|
27 |
|
|
|
28 |
### Basic Usage
|
|
|
29 |
|
|
|
30 |
Use `Faker\Factory::create()` to create and initialize a Faker generator, which can generate data by accessing methods named after the type of data you want.
|
|
|
31 |
|
|
|
32 |
```php
|
|
|
33 |
<?php
|
|
|
34 |
require_once 'vendor/autoload.php';
|
|
|
35 |
|
|
|
36 |
// use the factory to create a Faker\Generator instance
|
|
|
37 |
$faker = Faker\Factory::create();
|
|
|
38 |
// generate data by calling methods
|
|
|
39 |
echo $faker->name();
|
|
|
40 |
// 'Vince Sporer'
|
|
|
41 |
echo $faker->email();
|
|
|
42 |
// 'walter.sophia@hotmail.com'
|
|
|
43 |
echo $faker->text();
|
|
|
44 |
// 'Numquam ut mollitia at consequuntur inventore dolorem.'
|
|
|
45 |
```
|
|
|
46 |
|
|
|
47 |
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)`.
|
|
|
48 |
|
|
|
49 |
```php
|
|
|
50 |
<?php
|
|
|
51 |
for ($i = 0; $i < 3; $i++) {
|
|
|
52 |
echo $faker->name() . "\n";
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
// 'Cyrus Boyle'
|
|
|
56 |
// 'Alena Cummerata'
|
|
|
57 |
// 'Orlo Bergstrom'
|
|
|
58 |
```
|
|
|
59 |
|
|
|
60 |
## Automated refactoring
|
|
|
61 |
|
|
|
62 |
If you already used this library with its properties, they are now deprecated and needs to be replaced by their equivalent methods.
|
|
|
63 |
|
|
|
64 |
You can use the provided [Rector](https://github.com/rectorphp/rector) config file to automate the work.
|
|
|
65 |
|
|
|
66 |
Run
|
|
|
67 |
|
|
|
68 |
```bash
|
|
|
69 |
composer require --dev rector/rector
|
|
|
70 |
```
|
|
|
71 |
|
|
|
72 |
to install `rector/rector`.
|
|
|
73 |
|
|
|
74 |
Run
|
|
|
75 |
|
|
|
76 |
```bash
|
|
|
77 |
vendor/bin/rector process src/ --config vendor/fakerphp/faker/rector-migrate.php
|
|
|
78 |
```
|
|
|
79 |
|
|
|
80 |
to run `rector/rector`.
|
|
|
81 |
|
|
|
82 |
*Note:* do not forget to replace `src/` with the path to your source directory.
|
|
|
83 |
|
|
|
84 |
Another way is to use it in your `rector.php` file:
|
|
|
85 |
|
|
|
86 |
```php
|
|
|
87 |
$rectorConfig->import('vendor/fakerphp/faker/rector-migrate.php');
|
|
|
88 |
$faker($rectorConfig);
|
|
|
89 |
```
|
|
|
90 |
|
|
|
91 |
## License
|
|
|
92 |
|
|
|
93 |
Faker is released under the MIT License. See [`LICENSE`](LICENSE) for details.
|
|
|
94 |
|
|
|
95 |
## Backward compatibility promise
|
|
|
96 |
|
|
|
97 |
Faker is using [Semver](https://semver.org/). This means that versions are tagged
|
|
|
98 |
with MAJOR.MINOR.PATCH. Only a new major version will be allowed to break backward
|
|
|
99 |
compatibility (BC).
|
|
|
100 |
|
|
|
101 |
Classes marked as `@experimental` or `@internal` are not included in our backward compatibility promise.
|
|
|
102 |
You are also not guaranteed that the value returned from a method is always the
|
|
|
103 |
same. You are guaranteed that the data type will not change.
|
|
|
104 |
|
|
|
105 |
PHP 8 introduced [named arguments](https://wiki.php.net/rfc/named_params), which
|
|
|
106 |
increased the cost and reduces flexibility for package maintainers. The names of the
|
|
|
107 |
arguments for methods in Faker is not included in our BC promise.
|