Faker is a simply tool that generates random data and stores them in file or database. Tool uses the great PHP library created by Francois Zaninotto called Faker.
Our tool requires to provide a file in JSON format, that contains specification, about generating data.
Consider the following example, to see how the specification file should look like:
{
"title" : {
"provider" : "pl_PL/Text::realText",
"length" : 100
},
"content" : {
"provider" : "Lorem::text",
"length" : 300
},
"email" : {
"provider" : "Internet::safeEmail"
},
"first_name" : {
"provider" : "pl_PL/Person::firstName"
},
"last_name" : {
"provider" : "pl_PL/Person::lastName"
},
"address" : {
"provider" : "Address::address"
},
"date_of_birth" : {
"provider" : "DateTime::date",
"format" : "Y-m-d"
},
"company" : {
"provider" : "pl_PL/Company::company"
},
"phone_number" : {
"provider" : "pl_PL/PhoneNumber::phoneNumber"
},
"number_of_cars" : {
"provider" : "Base::randomNumber",
"from" : 0,
"to" : 3
},
"homepage" : {
"provider" : "Internet::domainName"
},
"last_visited_page" : {
"provider" : "Internet::url"
},
"user_agent" : {
"provider" : "UserAgent::userAgent"
},
"color" : {
"provider" : "Color::hexcolor"
},
"picture" : {
"provider" : "Image::image",
"dir" : "/tmp",
"width" : 200,
"height" : 200
},
"background" : {
"provider" : "Image::imageUrl",
"width" : 800,
"height" : 600
},
"password_hash" : {
"provider" : "Miscellaneous::sha1"
},
"locale" : {
"provider" : "Miscellaneous::locale"
}
}
"title" : {
"provider" : "pl_PL/Text::realText",
"length" : 100
}
Once we have a specification file, we can start generating fake data. We can do this, by writing a php script,
or just using the command line interface.
use Vegas\Tool\Faker\Generator;
//....
$path = dirname(__FILE__) . '/spec.json';
$outputPath = dirname(__FILE__) . '/output.csv';
$generator = new Generator();
$generator->setAdapter('file');
$generator->setAdapterType('csv');
$generator->setCount(5);
$generator->setSpecFilePath($path);
$generator->setDestination($outputPath);
$generator->generate();
use Vegas\Tool\Faker\Generator;
//....
$path = dirname(__FILE__) . '/spec.json';
$outputPath = dirname(__FILE__) . '/output.xml';
$generator = new Generator();
$generator->setAdapter('file');
$generator->setAdapterType('xml');
$generator->setCount(5);
$generator->setSpecFilePath($path);
$generator->setDestination($outputPath);
$generator->generate();
use Vegas\Tool\Faker\Generator;
//....
$path = dirname(__FILE__) . '/spec.json';
$outputPath = dirname(__FILE__) . '/output.json';
$generator = new Generator();
$generator->setAdapter('file');
$generator->setAdapterType('json');
$generator->setCount(5);
$generator->setSpecFilePath($path);
$generator->setDestination($outputPath);
$generator->generate();
use Vegas\Tool\Faker\Generator;
//....
$path = dirname(__FILE__) . '/spec.json';
$generator = new Generator();
$generator->setAdapter('db');
$generator->setAdapterType('odm');
$generator->setCount(5);
$generator->setSpecFilePath($path);
$generator->setDestination('fake');
$generator->generate();
use Vegas\Tool\Faker\Generator;
//....
$path = dirname(__FILE__) . '/spec.json';
$generator = new Generator();
$generator->setAdapter('db');
$generator->setAdapterType('orm');
$generator->setCount(5);
$generator->setSpecFilePath($path);
$generator->setDestination('fake');
$generator->generate();
php cli/cli.php vegas:tool_faker:generator generate -o db.odm -d sample_data -s spec.json -c 5