25000 stars! Faker: an open source project focusing on counterfeiting without being scolded

Time:2021-7-13

[introduction]: faker is a PHP library, which can be used to generate false data. It has achieved 25.2k stars on GitHub.

Project address:

https://github.com/fzaninotto…

If you have the following requirements: boot database, create nice XML documents, enrich your stress tests, or anonymize data from production services, faker can meet your requirements.

brief introduction

Faker is inspired by data:: faker in Perl and ruby   Faker is a mixture of the two projects. Now it’s more popular than the other two.

Faker requires PHP version  >= 5.3.3.

install

composer require fzaninotto/faker

Autoload

Faker supports psr-0   And psr-4.

<?php
# When installed via composer
require_once 'vendor/autoload.php';

You can also load the fakers transport psr-0 specification automatically.

<?php
# Load Fakers own autoloader
require_once '/path/to/Faker/src/autoload.php';

You can also use another psr-4 specification to be compatible with autoload.

Generating false data

Use_ Faker\Factory::create()_   To create and initialize a faker generator. This generator can generate data. The way to generate data is by accessing the property name of the data type you want.

<?php
// use the factory to create a Faker\Generator instance
$faker = Faker\Factory::create();

// generate data by accessing properties
echo $faker->name;
  // 'Lucy Cechtelar';
echo $faker->address;
  // "426 Jordy Lodge
  // Cartwrightshire, SC 88120-6700"
echo $faker->text;
  // Dolores sit sint laboriosam dolorem culpa et autem. Beatae nam sunt fugit
  // et sit et mollitia sed.
  // Fuga deserunt tempora facere magni omnis. Omnis quia temporibus laudantium
  // sit minima sint.

Even if this example shows a property read, each pair of & dollar; Faker > name will produce a random result. This is because faker uses\_\_ Get() magic, faker / generator – > & dollar; The property declaration is forwarded to faker / generator > format & dollar; property)。

<?php
for ($i = 0; $i < 10; $i++) {
  echo $faker->name, "\n";
}
  // Adaline Reichel
  // Dr. Santa Prosacco DVM
  // Noemy Vandervort V
  // Lexi O'Conner
  // Gracie Weber
  // Roscoe Johns
  // Emmett Lebsack
  // Keegan Thiel
  // Wellington Koelpin II
  // Ms. Karley Kiehn V

Suggestion: if you want to generate false data quickly, you can also borrow faker cli to use the faker command line tool.

Formatter

Each generator property (such as name, address, random number, pseudolites) is called a formatter. A faker generator has many formatters, which are packaged in “providers”. Here are some default formatters.

Faker\Provider\Base

randomDigit             // 7
randomDigitNot(5)       // 0, 1, 2, 3, 4, 6, 7, 8, or 9
randomDigitNotNull      // 5
randomNumber($nbDigits = NULL, $strict = false) // 79907610
randomFloat($nbMaxDecimals = NULL, $min = 0, $max = NULL) // 48.8932
numberBetween($min = 1000, $max = 9000) // 8567
randomLetter            // 'b'
// returns randomly ordered subsequence of a provided array
randomElements($array = array ('a','b','c'), $count = 1) // array('c')
randomElement($array = array ('a','b','c')) // 'b'
shuffle('hello, world') // 'rlo,h eoldlw'
shuffle(array(1, 2, 3)) // array(2, 1, 3)
numerify('Hello ###') // 'Hello 609'
lexify('Hello ???') // 'Hello wgt'
bothify('Hello ##??') // 'Hello 42jz'
asciify('Hello ***') // 'Hello R6+'
regexify('[A-Z0-9._%+-][email protected][A-Z0-9.-]+\.[A-Z]{2,4}'); // [email protected]

Faker\Provider\en\_US\Person

title($gender = null|'male'|'female')     // 'Ms.'
titleMale                                 // 'Mr.'
titleFemale                               // 'Ms.'
suffix                                    // 'Jr.'
name($gender = null|'male'|'female')      // 'Dr. Zane Stroman'
firstName($gender = null|'male'|'female') // 'Maynard'
firstNameMale                             // 'Maynard'
firstNameFemale                           // 'Rachel'
lastName                                  // 'Zulauf'

These methods all accept & dollar; The timezone parameter is passed to date by default\_ default\_ timezone\_ get()。 You can pass a timezone string parameter to each method, or use & dollar; faker::setDefaultTimezone(&dollar; Time zone) for all time methods.

Faker\Provider\Internet

email                   // '[email protected]'
safeEmail               // '[email protected]'
freeEmail               // '[email protected]'
companyEmail            // '[email protected]'
freeEmailDomain         // 'yahoo.com'
safeEmailDomain         // 'example.org'
userName                // 'wade55'
password                // 'k&|X+a45*2['
domainName              // 'wolffdeckow.net'
domainWord              // 'feeney'
tld                     // 'biz'
url                     // 'http://www.skilesdonnelly.biz/aut-accusantium-ut-architecto-sit-et.html'
slug                    // 'aut-repellat-commodi-vel-itaque-nihil-id-saepe-nostrum'
ipv4                    // '109.133.32.252'
localIpv4               // '10.242.58.8'
ipv6                    // '8e65:933d:22ee:a232:f1c1:2741:1f10:117c'
macAddress              // '43:85:B7:08:10:CA'

Faker\Provider\UserAgent

userAgent              // 'Mozilla/5.0 (Windows CE) AppleWebKit/5350 (KHTML, like Gecko) Chrome/13.0.888.0 Safari/5350'
chrome                 // 'Mozilla/5.0 (Macintosh; PPC Mac OS X 10_6_5) AppleWebKit/5312 (KHTML, like Gecko) Chrome/14.0.894.0 Safari/5312'
firefox                // 'Mozilla/5.0 (X11; Linuxi686; rv:7.0) Gecko/20101231 Firefox/3.6'
safari                 // 'Mozilla/5.0 (Macintosh; U; PPC Mac OS X 10_7_1 rv:3.0; en-US) AppleWebKit/534.11.3 (KHTML, like Gecko) Version/4.0 Safari/534.11.3'
opera                  // 'Opera/8.25 (Windows NT 5.1; en-US) Presto/2.9.188 Version/10.00'
internetExplorer       // 'Mozilla/5.0 (compatible; MSIE 7.0; Windows 98; Win 9x 4.90; Trident/3.0)'

end

For more details about this project, please visit here:https://github.com/fzaninotto… 

Open source outpostShare popular, interesting and practical open source projects everyday. Participate in the maintenance of 100000 + star open source technology resource library, including python, Java, C / C + +, go, JS, CSS, node.js, PHP,. Net, etc.