Yep, it’s here. Almost a year after the previous release, Faker 1.5 has been released, with a ton and a half of new fake data, ridiculously useful new formatters, new locales, and bug fixes. Ready to dive in?
Faker 1.5 includes a bunch of new formatters, to help you generate even more data types:
$faker->shuffle('hello, world') // 'rlo,h eoldlw'
$faker->shuffle(array(1, 2, 3)) // array(2, 1, 3)
$faker->asciify('Hello ***') // 'Hello R6+'
$faker->regexify('[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}') // sm0@y8k96a.ej
$faker->password // 'k&|X+a45*2['
$faker->swiftBicNumber // RZTIAT22263
$faker->isbn13 // '9790404436093'
$faker->isbn10 // '4881416324'
$faker->currencyCode // EUR
$faker->biasedNumberBetween($min = 10, $max = 20, $function = 'sqrt') // 12
$faker->imageUrl($width, $height, 'cats', true, 'Faker') // 'http://lorempixel.com/800/400/cats/Faker', the url of a cat image with the word "Faker" in it
Let me focus on just three of them: shuffle
, regexigy
, and biasedNumberBetween
.
shuffle
already exists in PHP. The problem is that the native shuffle()
function doesn’t rely on the same randomizer as Faker. Faker’s randomizer, powered by mt_rand()
, uses the Mersenne Twister algorithm to provide more random, and seedable data. Faker’s shuffle
uses Faker’s randomizer, together with the Fisher-Yates algorithm, and works both on strings and arrays. It’s just a million times better than the native version.
// try that, and you'll get exactly the same random data
$faker = Faker\Factory::create();
$faker->seed(5);
for ($i=0; $i < 10; $i++) {
$faker->shuffle('hello, world');
}
// le ldrhwolo,
// lr,h oldeolw
// wllod lo,hre
// dh owo,rllel
// drleo wlh,lo
// dewolrll,oh
// wl edoollh,r
// lowd, lhrelo
// rolwd,lloeh
// ohole,dlwl r
regexigy
takes a regular expression as parameter, and returns a random string satisfying this regular expression. If the “string with wildcard” formatters (numerify('Hello ###')
, lexify('Hello ???')
, bothify('Hello ##??')
, asciify('Hello ***')
) aren’t enough for you, you can always use this powerful formatter.
$faker->regexify('\w') // k
$faker->regexify('(a|b)') // b
$faker->regexify('[aeiou]') // o
$faker->regexify('[a-z]') // p
$faker->regexify('[a-z1-9]') // w
$faker->regexify('a*b+c?') // abc
$faker->regexify('a{2}') // aa
$faker->regexify('a{2,3}') // aa
$faker->regexify('[aeiou]{2,3}') // ui
$faker->regexify('[a-z]{2,3}') // rw
$faker->regexify('(a|b){2,3}') // abb
$faker->regexify('\.\*\?\+') // .*?+
$faker->regexify('[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}') // a8%1-_@zvk.sgha
regexify
supports a subset of the RegExp syntax, usually enough to match your needs. But Unicode, negated classes, unbounded ranges, subpatterns, back references, assertions, recursive patterns, and comments are not supported. Escaping support is extremely fragile.
This formatter is also VERY slow. Use it only when no other formatter can generate the fake data you want. For instance, prefer calling $faker->email
rather than regexify
with the previous regular expression.
Also note than bothify
can probably do most of what regexify
does, but much faster. For instance, for a dummy email generation, try $faker->bothify('?????????@???.???')
.
biasedNumberBetween($min, $max, $function)
returns a random number between two boundaries, but with a non-uniform chance. You can pass a distribution function to have more chances to get a number close to the start, to the end, or to the middle.
The algorithm creates two doubles, x ∈ [0, 1], y ∈ [0, 1) and checks whether the return value of $function
for x is greater than or equal to y. If this is the case the number is accepted and x is mapped to the appropriate integer between $min
and $max
. Otherwise two new doubles are created until the pair is accepted.
$faker->biasedNumberBetween(1, 10, ['\Faker\Provider\Biased', 'unbiased']) // 5
$faker->biasedNumberBetween(1, 10, ['\Faker\Provider\Biased', 'linearLow']) // 3
$faker->biasedNumberBetween(1, 10, ['\Faker\Provider\Biased', 'linearHigh']) // 7
$function
can be any callable. The limit is your imagination!
People from all around the world use Faker. Version 1.5 comes with close to 60 locales, including 17 new ones:
ar_JO
)at_AT
)be_BE
)en_NZ
)en_UG
)es_VE
)fa_IR
)id_ID
)ka_GE
)kk_KZ
)ko_KR
)ne_NP
)no_NO
)sl_SI
)sv_SE
)vi_VN
)zh_TW
)I love when people create new formatters, but Faker’s destiny isn’t to host them all. The core Faker repository should provide the providers and formatters required for most usages, but not all. Now, if you create a particularly smart Faker provider for a rare use case, send me a pull request and I’ll mention it in the README. Here is a list of tools and formatters that you can already use:
Between Faker 1.4 and 1.5, more than 100 pull requests were merged. They’re all listed in the CHANGELOG file. Suffice to say that they make Faker more stable, faster, and easier to use. For instance, your IDE will know how to suggest more formatters, you will be able to seed the generator properly, you will get more up to date user agents, color names in Dutch, or get valid fake emails whatever the locale.
Faker 1.5 works on PHP 5.3.3 through PHP 7.0. Update your local installation using composer
, there is no BC break.
I didn’t generate the section title with Faker. I didn’t use copy/paste either. I typed it all. That’s how much I want to thank the 70+ contributors who fixed bugs, sent pull requests, reviewed code, commented, helped newcomers, etc. These people simply made Faker better. They deserve to be mentioned here. Please give a huge round of applause to the Faker 1.5 crowd:
I recently received a file containing over 10,000,000 stolen passwords, leaked from a famous gaming network. Please check on the following list if your own password hasn’t been compromised:
i0K\3IL)gN`H= | In$k |
uZ3~]OB6I`hfDohm’0 |
X>4/9A~=?#rGIdWG | 1,” |
Qf}6k | $<QN |
Happy faking!
Tweet
Published on 29 May 2015
with tags faker php