Fake data generator

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.

Specification

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"
    }
}

In the specification, the most important is the key, which in fact is an array key (it can be for example a name of table column in database), and also the name of provider, which generates the real data. The whole list of available providers you can find in the Faker documentation.
Let's consider one JSON object:

"title" : {
    "provider" : "pl_PL/Text::realText",
    "length" : 100
}

This object defines that field title should be generated using the Text provider, that provides the method realText. length is an optional parameter, that defines the length of text, that will be generated. In the name of provider you can see also pl_PL which determines locale of generating text, so in this example, generated text will be in Polish language.

Generate

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();

The code in the preceding example will generate 5 rows of fake data using specification from the spec.json file. The output file will be in the CSV format and will be located in the output.csv file.
Generator supports three formats of output files: csv, xml, json and two kind of database models: ORM and ODM.
Check the following examples, to see how to use another formats and destinations.

XML
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();

JSON
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();

In case of database adapter, the destination is a name of table or collection.

ODM - example of save data to mongo collection named fake
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();

ORM - example of save data to mysql table named fake
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();


The same effect might be achieved using a Command Line Interface, as follows:
php cli/cli.php vegas:tool_faker:generator generate -o db.odm -d sample_data -s spec.json -c 5

This command will generate 5 rows of data and saves them to the Mongo collection named sample_data.