Powerorm Faker Guide

PowerOrmFaker is an extension of Faker library that generates fake data for the PowerOrm Library.

This is just wrapper to the PHP Faker

Installation

Via composer (recommended):

composer require eddmash/powerormfaker

Or add this to the composer.json file:

{
   "require": {
       "eddmash/powerormfaker": "1.1"(check latest version)
   }
}

Setup

To enable the faker, just register it as a component with the orm on the as shown below.

use Eddmash\PowerOrmFaker\Generatedata;

$config = [
    // ..., other orm settings


    'components' =>[
        Faker::class,
    ],
];

Usage

php pmanager.php generatedata

Available options are :

-o, –only

The list of models to use when generating records.

php pmanager.php generatedata -o 'App\Models\Author' -o 'App\Models\Blog'

-i, –ignore

The list of models to ignore when generating records.

php pmanager.php generatedata -i 'App\Models\Entry'

-r, –records

The number of records to generate per model.

php pmanager.php generatedata -r 5

-s, –seed

To always get the same generated data. Calling generatedata twice with the same seed produces the same results

php pmanager.php generatedata -s 5000

Customizing the type of data generated.

This library guesses the kind of data to be set for each field on a model based on the

  • name of the field, if its not able it uses,
  • the type if model field is.

You have the ability to customize the kind of data generated by implementing the FakeableInterface in you model as shown below.

The FakeableInterface only has one method the registerFormatter method which should return an associative array of model field name as key and an anonymous function as its value.

The anonymous function should accept

  • a faker object see Using faker object and
  • model instance being populated as parameters and returns a valid php value e.g integer/string/date etc.

The registerFormatter method accepts one parameter, the generator object which you can use to register custom providers see Adding Providers

namespace App\Models;

use Eddmash\PowerOrm\Model\Model;
use Eddmash\PowerOrmFaker\FakeableInterface;

class User extends Model implements FakeableInterface
{
    public function unboundFields()
    {
        return [
            "username" => Model::CharField(['maxLength' => 50]),
            "age" => Model::CharField(['maxLength' => 50]),
        ];

    }

    public function registerFormatter(Generator $generator)
    {
        return [
            "age" => function ($faker, $object) {
                return $faker->ipv4;
            },
        ];
    }
}

Using the faker object

// generate data by accessing properties

    echo $faker->randomDigit             // 7
    echo $faker->phoneNumber             // '201-886-0269 x3767'
    echo $faker->jobTitle                // 'Cashier'
    echo $faker->name;
          // 'Lucy Cechtelar';
    echo $faker->randomElements($array = array ('a','b','c'), $count = 1)   // array('c')
    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.

See all available Localized Formatters and General Formatters on the faker object.

Adding Providers to Faker object

You can create you custom data providers for the faker as shown below.

The create the provider in this case we create a book provider.

namespace App\Provider;

use Faker\Provider\Base;

class BookProvider extends Base
{
    public function book_title($nbWords = 5)
    {
        $sentence = $this->generator->sentence($nbWords);
        return substr($sentence, 0, strlen($sentence) - 1);
    }

    public function book_isbn()
    {
        return $this->generator->ean13();
    }
}

Register the custom provider with the generator on the registerFormatter method and now you can use the new formaters as shown below.

namespace App\Models;

use App\Provider\BookProvider;
use Eddmash\PowerOrm\Model\Model;
use Eddmash\PowerOrmFaker\FakeableInterface;
use Faker\Generator;

class Book extends Model implements FakeableInterface
{
    public function unboundFields()
    {
        return [
            "title" => Model::CharField(['maxLength' => 50]),
            "isbn" => Model::CharField(['maxLength' => 50]),
            "summary" => Model::CharField(['maxLength' => 50]),
        ];
    }

    public function registerFormatter(Generator $generator)
    {
        $generator->addProvider(new BookProvider($generator));

        return [
            "title" => function ($faker, $object) {
                return $faker->book_title;
            },
            "isbn" => function ($faker, $object) {
                return $faker->book_isbn;
            },
        ];
    }

}

For in depth details of how this work see Faker Internals: Understanding Providers