Don't be a Faker - Use fzaninotto/faker

I recently wanted to create a CSV file of user data: names, ids, some geospatial data etc, that kind of thing. That’s not a huge deal when the size of data is small, but what if I wanted to have thousands of lines of data? That’s where Faker comes in. This short article will show you some of the basics of using this package and how easy it is to use.

Installing the Package

Let’s start with an empty project and then add in our required package:

1
2
3
> mkdir faker
> cd faker
> composer require fzaninotto/faker

Creating your Faker object

Edit a file called app.php in the root of your project and add the following lines:

1
2
3
require './vendor/autoload.php';

$faker = Faker\\Factory::create('en_GB');

That will ensure we can autoload our package before creating a Faker object using the Factory pattern. Notice the parameter of “en_GB“ in the method call? That is a really nice feature of Faker which ensures all the appropriate values returned are UK centric. No Zack and Claude with names, for example - just good old John and Jennys.

Basic values

Now the fun part. Let’s output some fake values in the form of a comma separated list.

1
2
3
4
5
6
7
8
9
10
11
for ($i = 0; $i < 10; $i++) {
echo $faker->randomNumber(5, true) . ',' .
$faker->isbn13 . ',' .
$faker->firstName . ',' .
$faker->lastName . ',' .
$faker->email . ',' .
$faker->dateTimeThisCentury->format('d-M-Y') . ',' .
$faker->latitude . ',' .
$faker->longitude . ',' .
$faker->imageUrl(80, 150, 'people') . PHP_EOL;
}

A lot of this are easy to guess so instead, let’s focus on some of the slightly unusual ones.

Random numbers

1
$faker->randomNumber(5, true)

This takes two parameters - the first is the number of digits and the second states whether it should strictly return that number. I wanted 5 and exactly 5, hence my choice. You can see exactly how this is implemented here.

Dates

1
$faker->dateTimeThisCentury->format('d-M-Y')

I really like this method. There are many, many date based fakes but this one obviously returns a date sometime this century.

Images

1
$faker->imageUrl(80, 150, 'people')

Another great method! Can you feel my excitement? This one returns an image with the width and height specified, using the theme given - people in this case. Finding out which other areas are available requires a quick peek of the code here. You can have: abstract, animals, business, cats, city, food, nightlife, fashion, people, nature, sports, technics, transport and is provided by: http://lorempixel.com. This is perfect if you want to fake some bio pictures for instance.

Seeding

Each time you run Faker, you will get a different value, but what if you wanted to make it predictable? That’s easy - just use a known seed before you make any calls to the package.

1
$faker->seed(1234);

Obtaining unique values

If you are using randomNumber to generate IDs, you definitely don’t want to have any appear more than once. To ensure that happens, just use some fluid syntax to require unique values:

1
$faker->unique()->randomNumber(5, true)

This can also be used on other values. Let’s apply it to names:

1
$faker->unique()->firstName

Validating results

This is another nice feature which helps you to filter your results to make sure they meet certain conditions. Let’s go back to our random numbers and only take even examples.

1
2
3
4
$evenValidator = function ($digit) {
return $digit % 2 === 0;
};
$faker->valid($evenValidator)->randomNumber(5, true);

valid() takes a function reference which returns true or false. If true, the value returned is accepted and if not, well, you know…chop!

Finding out more

That covers much of the basics but there are many other useful functions that are worth checking out. Don’t forget to keep it real, folks.


Hi! Did you find this useful or interesting? I have an email list coming soon, but in the meantime, if you ready anything you fancy chatting about, I would love to hear from you. You can contact me here or at stephen ‘at’ logicalmoon.com