首页 > 其他分享 >2.5 万 Star!专注造假而不被骂的开源项目:Faker

2.5 万 Star!专注造假而不被骂的开源项目:Faker

时间:2023-02-18 10:00:41浏览次数:46  
标签:5.0 Star Mozilla faker Faker array Hello 2.5


Faker 是一个 PHP 库,可用来生成虚假数据。它已在 GitHub 上获得 25.2k Star。

项目地址:

​https://github.com/fzaninotto/Faker​

如果你有如下的需求:引导数据库,创建好看的XML文档,充实你的压力测试,或者从生产服务中匿名化数据,Faker 可以满足你的要求。 

简介

Faker 灵感来自 Perl 语言的 Data::Faker 和 Ruby 的  Faker 两个项目的混合。现在受欢迎度已经超过了另外两个。

Faker 要求 PHP 版本 >= 5.3.3.

安装



composer require fzaninotto/faker
composer require fzaninotto/faker

自动加载

Faker 支持 PSR-0 和 PSR-4 自动载入。



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

你也可以载入Fakers传输PSR-0规范自动载入。



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

你也可以使用另外的PSR-4规范来兼容自动载入。

生成虚假数据

使用FakerFactory::create() 来创建和初始化一个faker生成器。这个生成器可以生成数据。生成数据的方式是通过访问你想要的数据类型的属性名。



// use the factory to create a FakerGenerator instance
$faker = FakerFactory::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.

即使这个例子展示了一个属性读取,每个对$faker->name的声明都会产生一个随机结果。这是因为Faker使用__get()魔法,将FakerGenerator->$property的声明转发到了FakerGenerator->format($property)。



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

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

建议:如果你想快速产生虚假数据,也可以借faker-cli来使用Faker命令行工具。

格式器

每个生成器属性(例如名字,地址,乱数假文)都被叫做“格式器”。一个Faker生成器有很多的格式器,被打包在“providers”中。这里是一些默认的格式器。

FakerProviderBase



randomDigit             // 7randomDigitNot(5)       // 0, 1, 2, 3, 4, 6, 7, 8, or 9randomDigitNotNull      // 5randomNumber($nbDigits = NULL, $strict = false) // 79907610randomFloat($nbMaxDecimals = NULL, $min = 0, $max = NULL) // 48.8932numberBetween($min = 1000, $max = 9000) // 8567randomLetter            // 'b'// returns randomly ordered subsequence of a provided arrayrandomElements($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._%+-]+@[A-Z0-9.-]+.[A-Z]{2,4}'); // [email protected]
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._%+-]+@[A-Z0-9.-]+.[A-Z]{2,4}'); // [email protected]

FakerProvideren_USPerson



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'
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'

这些方法都接受了$timezone参数默认传递到date_default_timezone_get()。你可以对每个方法传递一个timezone字符串参数,或者是在使用$faker::setDefaultTimezone($timezone)时,为所有的time方法自定义一个timezone。

FakerProviderInternet



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'

FakerProviderUserAgent



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)'
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)'

结束

关于本项目的更多详细信息,可去查看这里:

​https://github.com/fzaninotto/Faker ​

– EOF – 

标签:5.0,Star,Mozilla,faker,Faker,array,Hello,2.5
From: https://blog.51cto.com/u_15967457/6065077

相关文章