Faker

repository·2.0·Indexed 26 days ago

https://github.com/fakerphp/faker

A PHP library for generating realistic fake data for database bootstrapping, XML document creation, stress testing, and data anonymization. It provides a wide range of providers for generating names, emails, addresses, colors, dates, and localized data (such as Czech company and Danish address details), as well as utilities for random numbers, strings, and array manipulation.

Tokens
4.8K
Snippets
4
Records
55
Agent score
87%

What's inside fakerphp/faker

  1. Automated refactoring of deprecated properties using Rector

    2.0

    If your codebase uses Faker properties (which are deprecated), you can automate the migration to their equivalent methods using Rector.

    1. Install Rector:
    composer require --dev rector/rector
    1. Run the migration process (replace src/ with your actual source directory):
    vendor/bin/rector process src/ --config vendor/fakerphp/faker/rector-migrate.php

    Alternatively, you can import the Faker migration configuration directly into your existing rector.php file:

    <?php
    
    declare(strict_types=1);
    
    use Rector\Config;
    
    return static function (Config\RectorConfig $rectorConfig): void {
        $rectorConfig->import('vendor/fakerphp/faker/rector-migrate.php');
    };
  2. Basic Usage with Faker\Factory

    2.0

    To generate fake data, use Faker\Factory::create() to initialize a Faker\Generator instance. You can then call methods on the generator instance named after the type of data you wish to produce (e.g., name(), email(), text()). Each call produces a different random result.

    <?php
    
    declare(strict_types=1);
    
    require_once 'vendor/autoload.php';
    
    // use the factory to create a Faker\Generator instance
    $faker = Faker\Factory::create();
    
    // generate data by calling methods
    echo $faker->name();
    // 'Vince Sporer'
    echo $faker->email();
    // 'walter.sophia@hotmail.com'
    echo $faker->text();
    // 'Numquam ut mollitia at consequuntur inventore dolorem.'
  3. Backward compatibility and Semver policy

    2.0

    Faker follows Semantic Versioning (Semver).

    • Breaking Changes: Only introduced in MAJOR version updates.
    • BC Promise: Guarantees that the data type returned from a method will not change. However, the specific value returned is not guaranteed to be consistent.
    • Exclusions: Classes marked as @experimental or @internal are not covered by the BC promise.
    • Named Arguments: Due to PHP 8 named arguments, the names of method arguments are not included in the backward compatibility promise.
  4. Generate formatted date and time strings

    2.0

    Use these methods to get date/time components as strings:

    • date($format, $max): Returns a date string using the provided PHP date format (default is 'Y-m-d').
    • time($format, $max): Returns a time string using the provided PHP date format (default is 'H:i:s').
    • iso8601($max): Returns a date string formatted with ateTime::ISO8601.
    • amPm($max): Returns a random 'am' or 'pm' string.
    • dayOfMonth($max): Returns the day of the month (e.g., '07').
    • dayOfWeek($max): Returns the full textual representation of the day (e.g., 'Tuesday').
    • month($max): Returns the month with leading zeros (e.g., '09').
    • monthName($max): Returns the full textual representation of the month (e.g., 'September').
    • year($max): Returns the 4-digit year (e.g., '1987').
  5. Generate URLs and slugs

    2.0

    Generate web-related strings using these methods:

    • url(): Returns a random URL using various protocols and formats (e.g., https://www.example.com/slug).
    • slug($nbWords = 6, $variableNbWords = true): Generates a URL-friendly slug.
      • $nbWords: The number of words to include.
      • $variableNbWords: If true, the number of words will vary slightly around the $nbWords value.
  6. Generate usernames with Internet provider

    2.0

    Use userName() to generate a random username. The method uses various formats (e.g., lastName.firstName, firstName##) and ensures the result is transliterated to lowercase ASCII characters.

    Note: If the selected locale cannot be transliterated to ASCII, this method will throw an Exception. Ensure the intl PHP extension is activated for better compatibility.