Faker System

Lightpack's Faker provides a lightweight fake data generator for tests, seeding, and development.


Getting Started

use Lightpack\Faker\Faker;

$faker = new Faker(); // Uses the default 'en' locale

Basic Usage

$name = $faker->name();         // 'John Smith'
$email = $faker->email();       // 'johnsmith@example.com'
$city = $faker->city();         // 'London'
$sentence = $faker->sentence(); // 'People over you have are we you come up.'
$uuid = $faker->uuid();         // 'e7b8...-...'

Locale System

Faker supports locale-based data via simple PHP arrays. You can:

Using a Custom Locale File

$faker = new Faker('custom', '/path/to/custom-locale.php');

A locale file returns an array of data lists:

<?php
return [
    'firstNames' => ['Jean'],
    'lastNames' => ['Dupont'],
    'domains' => ['monsite.fr'],
    'cities' => ['Paris'],
    'states' => ['Île-de-France'],
    'countries' => ['France'],
    'streets' => ['Rue de la Paix'],
    'companies' => ['Acme Corp'],
    'jobTitles' => ['Engineer'],
    'productNames' => ['Widget'],
    'phonePrefixes' => ['+33'],
    'words' => ['the', 'be', 'to', 'of', 'and', ...], // Common English words
    // ...
];

Note: If a locale key is missing, Faker automatically falls back to the English (en) locale data for that key.

Injecting Locale Data Directly

$faker = new Faker('custom');
$faker->setLocaleData([
    'firstNames' => ['Anna'],
    'lastNames' => ['Smith'],
    'domains' => ['example.test'],
    // ...
]);

Faker API Methods

All methods are explicit, chainable, and deterministic (with seeding):

Identity & Contact

Business & Commerce

Internet & Tech

Numbers & Types

Text

Dates & Time

Security & Finance

Geo

Utilities


UniqueFaker: Generating Unique Values

For unique fake data (e.g., unique emails), use unique():

$uniqueFaker = $faker->unique();
$email1 = $uniqueFaker->email();
$email2 = $uniqueFaker->email(); // Always different from $email1

Important Details:

Example of exhaustion:

$unique = $faker->unique();
// This will eventually throw RuntimeException after 100 failed attempts
for ($i = 0; $i < 1000; $i++) {
    $unique->enum(['A', 'B']); // Only 2 possible values
}

Deterministic Seeding

You can make all fake data deterministic for repeatable tests:

$faker = new Faker();
$faker->seed(1234);
echo $faker->name(); // Always the same output for the same seed

Practical Usage Examples

Generate a Batch of Fake Users

$faker = new Faker();
$users = $faker->arrayOf('name', 5);
// [ 'John Smith', 'Priya Sharma', ... ]

Unique Emails for Seeding

$unique = $faker->unique();
$emails = [];
for ($i = 0; $i < 10; $i++) {
    $emails[] = $unique->email();
}
// All emails are unique

Generate User Profile

$user = [
    'first_name' => $faker->firstName(),
    'last_name' => $faker->lastName(),
    'email' => $faker->email(),
    'username' => $faker->username(),
    'password' => $faker->password(16),
    'phone' => $faker->phone(),
    'address' => $faker->address(),
    'registered_at' => $faker->datetime(),
];

Generate Blog Post

$post = [
    'title' => ucwords($faker->sentence(6)),
    'slug' => $faker->slug(5),
    'content' => $faker->paragraph(5),
    'author' => $faker->name(),
    'published_at' => $faker->datetime('Y-m-d H:i:s'),
];