Faker 1.6 is Released

Yep, it’s here. Almost a year after the previous release, Faker 1.6 has been released, with a ton and a half of new fake data, ridiculously useful new formatters, new locales, and bug fixes. Of course, it’s totally backwards compatible with the previous version. Ready to dive in?

New Formatters

Faker 1.6 includes a bunch of new formatters, to help you generate even more data types:

<?php
$faker->e164PhoneNumber()    // '+27113456789'
$faker->jobTitle()           // 'Cashier'
$faker->dateTimeInInterval($startDate = '-30 years', $interval = '+ 5 days', $timezone = date_default_timezone_get()) // DateTime('2003-03-15 02:00:49', 'Antartica/Vostok')
$faker->iban($countryCode)   // 'IT31A8497112740YZ575DJ28BP4'
$faker->swiftBicNumber       // 'RZTIAT22263'

Not to mention the myriad of locale-specific formatters that grow at each release.

New valid() Modifier

If you want to generate random data matching special format validators, this modifier is a perfect fit:

<?php
// valid() only accepts valid values according to the passed validator functions
$values = array();
$evenValidator = function($digit) {
 return $digit % 2 === 0;
};
for ($i=0; $i < 10; $i++) {
 $values []= $faker->valid($evenValidator)->randomDigit;
}
print_r($values); // [0, 4, 8, 4, 2, 6, 0, 8, 8, 6]

// just like unique(), valid() throws an overflow exception when it can't generate a valid value
$values = array();
try {
  $faker->valid($evenValidator)->randomElement(1, 3, 5, 7, 9);
} catch (\OverflowException $e) {
  echo "Can't pick an even number in that set!";
}

New ORM Support

Faker could already create entities/records for Doctrine, Propel, Mandango and CakePHP. With version 1.6, Faker now offers ORM populators for Propel2 and Spot. The syntax is similar to the one used for other ORMs:

<?php
$generator = \Faker\Factory::create();
$populator = new Faker\ORM\Propel2\Populator($generator);
$populator->addEntity('Author', 5);
$populator->addEntity('Book', 10, array(
  'ISBN' => function() use ($generator) { return $generator->ean13(); }
  'CreatedAt' => null,
  'UpdatedAt' => null,
), , array(
  function($book) { $book->publish(); },
));
$insertedPKs = $populator->execute();
print_r($insertedPKs);
// array(
//   'Author' => (34, 35, 36, 37, 38),
//   'Book'   => (456, 457, 458, 459, 470, 471, 472, 473, 474, 475)
// )

New Locales

Faker grows in every part of the planet. Contributors from more than 59 different languages or countries (locales, in fact) have helped make Faker a true polyglot. Faker 1.6 introduces 9 new locales:

  • Arabian Saudi Arabia (ar_SA)
  • Swiss High German (de_CH)
  • English for India (en_IN)
  • English for Singapore (en_SG)
  • Franch for Switzerland (fr_CH)
  • Hebrew for Istrael (he_IL)
  • Croatian (hr_HR)
  • Italian for Switzerland (it_CH)
  • Lithuanian (lt_LT)

Besides, the Norwegian locale was renamed from nb_NO (which doesn’t exist) to no_NO

So Many Contributors

More and more people contribute to Faker, and more and more often. Since Faker 1.5, not less than 73 contributors chose to share their code with the Faker project. It’s just astounding. Let me list them all, and give them a huge thank you:

aivus ajbdev alesf AlexCutts ankitpokhrel asika32764 behramcelen belendel bessl byan daveblake deerawan denheck DIOHz0r endelwar fonsecas72 gido gietos glagola GrahamCampbell halaxa huy95 igorsantos07 ihsanudin ikwattro ivanmirson jadb JasonMortonNZ JeroenDeDauw JonathanKryza julien-c killerog kix kletellier kumamidori lintaba Lisso-Me lperto MatissJanis miclf mikehaertl nazar-pc netcarver pauledenburg paulvalla pearlc phaza philsturgeon piotrantosik Ragazzo ronanguilloux schmengler semanser SpaceK33z stelgenhof stof stoutZero svrnm swekaj terion-name tharoldD TimWolla TomasVotruba TomK totophe tzhuan ulrikjohansson wizardjedi YerlenZhubangaliyev ysramirez ZAYEC77 zoli zrashwani

You can see the integral list of changes in the CHANGELOG file.

One Last Word

Randomness is ultra hype these days. With the proper random SHA256 hash, you can earn more than $10,000 by generating a bitcoin block. Faker allows you to do it in an extremely elegant (albeit absolutely not performant) way, using Flaubert’s Madame Bovary as a source of randomness:

<?php
const DIFFICULTY = 2;
$faker = Faker\Factory::create('fr_FR');
$pattern = str_repeat('0', DIFFICULTY);
$i= 0;
while (true) {
    $faker->seed($i++);
    $hash = hash('sha256', $faker->realText());
    if (strpos($hash, $pattern) === 0) {
        echo $i, " ", $hash, "\n";
    }
}
52 00e132fc0f200ab2971f3c0f7408f5b779e819c3790801e2b5b1188ec6f51719
334 0052addd18677d04f57635eea602f46b4b8920f66216616ad12c912e78f4336f
562 0063a44ba60e9f872500c4b7976b90341c8b732edbcf3f918b8e374397fadc7f
1529 0012b07697882f6f48d64360563ab0336b7c41e6828add322703a45e6de770e8
1816 009832c52e63eeab7a8f43d43bade40fadf014aeccface6987923d317cc50d65
2033 000b52e38383a9306dc6bba878ba94b42f16dce98ff3dced9909899b1da812c9
2352 004223846a104f8c5b68afebe9fb30fd840cf075485e0ed5d19d24bfcdecc787
2394 00e643d56d201e17658cf5161f03afc0b125ce82be09f386bfb46700a34a4fb0
2449 00a28bc6fe0ecdfd41585682b4d1208ce3839ebd5e38471c5f07e32fd5f73e66
2614 009f5fe203eecd93aaebac414906c86fcef9675a7336b849d777d8c9831fcbf7
2744 00c11d561714aed4ea0b117f204954eebf27c006ff513435cb8f9f0fff9847d1
2837 00f6e29b6117ca940b7e3fe2e7a3939299e5cf3c690101ff02cfc6ee30689900
...

Happy Fake Mining!

Published on 29 Apr 2016 with tags faker php

comments powered by Disqus