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:

Creating your Faker object

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

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.

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

Random numbers

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

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

Images

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.

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:

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

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.

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 our 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.

twittergoogle_plusredditpinteresttumblrmail

Written by Stephen Moon
email: stephen at logicalmoon.com
www: https://www.logicalmoon.com


Hey! Did you enjoy reading this? If you did and would like an email when I add new content, just subscribe to my list. You can unsubscribe at any time.


This entry was posted in: php. Bookmark the ➜ permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *