Download the PHP package tomaj/faker without Composer
On this page you can find all versions of the php package tomaj/faker. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package faker
Faker
Faker is a PHP library that generates fake data for you. Whether you need to bootstrap your database, create good-looking XML documents, fill-in your persistence to stress test it, or anonymize data taken from a production service, Faker is for you.
Faker is heavily inspired by Perl's Data::Faker, and by ruby's Faker.
Faker requires PHP >= 5.3.3.
Basic Usage
Use Faker\Factory::create()
to create and initialize a faker generator, which can generate data by accessing properties named after the type of data you want.
Even if this example shows a property access, each call to $faker->name
yields a different (random) result. This is because Faker uses __get()
magic, and forwards Faker\Generator->$property
calls to Faker\Generator->format($property)
.
Formatters
Each of the generator properties (like name
, address
, and lorem
) are called "formatters". A faker generator has many of them, packaged in "providers". Here is a list of the bundled formatters in the default locale.
Faker\Provider\Base
randomDigit // 7
randomDigitNotNull // 5
randomNumber($nbDigits = NULL) // 79907610
randomNumber($from, $to) // 39049
randomFloat($nbMaxDecimals = NULL, $min = 0, $max = NULL) // 48.8932
randomLetter // 'b'
randomElements($array = array ('a','b','c'), $count = 1) // array('c')
randomElement($array = array ('a','b','c')) // 'b'
numerify($string = '###') // '609'
lexify($string = '????') // 'wgts'
bothify($string = '## ??') // '42 jz'
Faker\Provider\Lorem
word // 'aut'
words($nb = 3) // array('porro', 'sed', 'magni')
sentence($nbWords = 6) // 'Sit vitae voluptas sint non voluptates.'
sentences($nb = 3) // array('Optio quos qui illo error.', 'Laborum vero a officia id corporis.', 'Saepe provident esse hic eligendi.')
paragraph($nbSentences = 3) // 'Ut ab voluptas sed a nam. Sint autem inventore aut officia aut aut blanditiis. Ducimus eos odit amet et est ut eum.'
paragraphs($nb = 3) // array('Quidem ut sunt et quidem est accusamus aut. Fuga est placeat rerum ut. Enim ex eveniet facere sunt.', 'Aut nam et eum architecto fugit repellendus illo. Qui ex esse veritatis.', 'Possimus omnis aut incidunt sunt. Asperiores incidunt iure sequi cum culpa rem. Rerum exercitationem est rem.')
text($maxNbChars = 200) // 'Fuga totam reiciendis qui architecto fugiat nemo. Consequatur recusandae qui cupiditate eos quod.'
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'
Faker\Provider\en_US\Address
cityPrefix // 'Lake'
secondaryAddress // 'Suite 961'
state // 'NewMexico'
stateAbbr // 'OH'
citySuffix // 'borough'
streetSuffix // 'Keys'
buildingNumber // '484'
city // 'West Judge'
streetName // 'Keegan Trail'
streetAddress // '439 Karley Loaf Suite 897'
postcode // '17916'
address // '8888 Cummings Vista Apt. 101, Susanbury, NY 95473'
country // 'Falkland Islands (Malvinas)'
latitude // '77.147489'
longitude // '86.211205'
Faker\Provider\en_US\PhoneNumber
phoneNumber // '132-149-0269x3767'
Faker\Provider\en_US\Company
catchPhrase // 'Monitored regional contingency'
bs // 'e-enable robust architectures'
company // 'Bogan-Treutel'
companySuffix // 'and Sons'
Faker\Provider\en_US\Text
realText($maxNbChars = 200, $indexSize = 2) // "And yet I wish you could manage it?) 'And what are they made of?' Alice asked in a shrill, passionate voice. 'Would YOU like cats if you were never even spoke to Time!' 'Perhaps not,' Alice replied."
Faker\Provider\DateTime
unixTime($max = 'now') // 58781813
dateTime($max = 'now') // DateTime('2008-04-25 08:37:17')
dateTimeAD($max = 'now') // DateTime('1800-04-29 20:38:49')
iso8601($max = 'now') // '1978-12-09T10:10:29+0000'
date($format = 'Y-m-d', $max = 'now') // '1979-06-09'
time($format = 'H:i:s', $max = 'now') // '20:49:42'
dateTimeBetween($startDate = '-30 years', $endDate = 'now') // DateTime('2003-03-15 02:00:49')
dateTimeThisCentury($max = 'now') // DateTime('1915-05-30 19:28:21')
dateTimeThisDecade($max = 'now') // DateTime('2007-05-29 22:30:48')
dateTimeThisYear($max = 'now') // DateTime('2011-02-27 20:52:14')
dateTimeThisMonth($max = 'now') // DateTime('2011-10-23 13:46:23')
amPm($max = 'now') // 'pm'
dayOfMonth($max = 'now') // '04'
dayOfWeek($max = 'now') // 'Friday'
month($max = 'now') // '06'
monthName($max = 'now') // 'January'
year($max = 'now') // '1993'
century // 'VI'
timezone // 'Europe/Paris'
Faker\Provider\Internet
email // '[email protected]'
safeEmail // '[email protected]'
freeEmail // '[email protected]'
companyEmail // '[email protected]'
freeEmailDomain // 'yahoo.com'
safeEmailDomain // 'example.org'
userName // 'wade55'
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)'
Faker\Provider\Payment
creditCardType // 'MasterCard'
creditCardNumber // '4485480221084675'
creditCardExpirationDate // 04/13
creditCardExpirationDateString // '04/13'
creditCardDetails // array('MasterCard', '4485480221084675', 'Aleksander Nowak', '04/13')
Faker\Provider\Color
hexcolor // '#fa3cc2'
rgbcolor // '0,255,122'
rgbColorAsArray // array(0,255,122)
rgbCssColor // 'rgb(0,255,122)'
safeColorName // 'fuchsia'
colorName // 'Gainsbor'
Faker\Provider\File
fileExtension // 'avi'
mimeType // 'video/x-msvideo'
// Copy a random file from the source to the target directory and returns the fullpath or filename
file($sourceDir = '/tmp', $targetDir = '/tmp') // '/path/to/targetDir/13b73edae8443990be1aa8f1a483bc27.jpg'
file($sourceDir, $targetDir, false) // '13b73edae8443990be1aa8f1a483bc27.jpg'
Faker\Provider\Image
// Image generation provided by LoremPixel (http://lorempixel.com/)
imageUrl($width = 640, $height = 480) // 'http://lorempixel.com/640/480/'
imageUrl($width, $height, 'cats') // 'http://lorempixel.com/800/600/cats/'
image($dir = '/tmp', $width = 640, $height = 480) // '/tmp/13b73edae8443990be1aa8f1a483bc27.jpg'
image($dir, $width, $height, 'cats') // 'tmp/13b73edae8443990be1aa8f1a483bc27.jpg' it's a cat!
Faker\Provider\Uuid
uuid // '7e57d004-2b97-0e7a-b45f-5387367791cd'
Faker\Provider\Barcode
ean13 // '4006381333931'
ean8 // '73513537'
Faker\Provider\Miscellaneous
boolean($chanceOfGettingTrue = 50) // true
md5 // 'de99a620c50f2990e87144735cd357e7'
sha1 // 'f08e7f04ca1a413807ebc47551a40a20a0b4de5c'
sha256 // '0061e4c60dac5c1d82db0135a42e00c89ae3a333e7c26485321f24348c7e98a5'
locale // en_UK
countryCode // UK
languageCode // en
Unique and Optional modifiers
Faker provides two special providers, unique()
and optional()
, to be called before any provider. optional()
can be useful for seeding non-required fields, like a mobile telephone number; unique()
is required to populate fields that cannot accept twice the same value, like primary identifiers.
Localization
Faker\Factory
can take a locale as an argument, to return localized data. If no localized provider is found, the factory fallbacks to the default locale (en_EN).
You can check available Faker locales in the source code, under the Provider
directory. The localization of Faker is an ongoing process, for which we need your help. Don't hesitate to create localized providers to your own locale and submit a PR!
Populating Entities Using an ORM or an ODM
Faker provides adapters for Object-Relational and Object-Document Mappers (currently, Propel, Doctrine2, and Mandango are supported). These adapters ease the population of databases through the Entity classes provided by an ORM library (or the population of document stores using Document classes provided by an ODM library).
To populate entities, create a new populator class (using a generator instance as parameter), then list the class and number of all the entities that must be generated. To launch the actual data population, call the execute()
method.
Here is an example showing how to populate 5 Author
and 10 Book
objects:
The populator uses name and column type guessers to populate each column with relevant data. For instance, Faker populates a column named first_name
using the firstName
formatter, and a column with a TIMESTAMP
type using the dateTime
formatter. The resulting entities are therefore coherent. If Faker misinterprets a column name, you can still specify a custom closure to be used for populating a particular column, using the third argument to addEntity()
:
In this example, Faker will guess a formatter for all columns except ISBN
, for which the given anonymous function will be used.
Tip: To ignore some columns, specify null
for the column names in the third argument of addEntity()
. This is usually necessary for columns added by a behavior:
Of course, Faker does not populate autoincremented primary keys. In addition, Faker\ORM\Propel\Populator::execute()
returns the list of inserted PKs, indexed by class:
In the previous example, the Book
and Author
models share a relationship. Since Author
entities are populated first, Faker is smart enough to relate the populated Book
entities to one of the populated Author
entities.
Lastly, if you want to execute an arbitrary function on an entity before insertion, use the fourth argument of the addEntity()
method:
Seeding the Generator
You may want to get always the same generated data - for instance when using Faker for unit testing purposes. The generator offers a seed()
method, which seeds the random number generator. Calling the same script twice with the same seed produces the same results.
Tip: DateTime formatters won't reproduce the same fake data if you don't fix the
$max
value:php
## Language specific formatters
### `Faker\Provider\fr_FR\Company`
### `Faker\Provider\fr_FR\Address`
### `Faker\Provider\da_DK\Person`
### `Faker\Provider\da_DK\Address`
### `Faker\Provider\da_DK\Company`
### `Faker\Provider\pl_PL\Person`
### `Faker\Provider\pl_PL\Company`
### `Faker\Provider\pl_PL\Payment`
### `Faker\Provider\ja_JP\Person`
### `Faker\Provider\ro_RO\Person`
### `Faker\Provider\ro_RO\PhoneNumber`
### `Faker\Provider\pt_PT\Person`
## License
Faker is released under the MIT Licence. See the bundled LICENSE file for details.