PHP code example of kent013 / faker-json

1. Go to this page and download the library: Download kent013/faker-json library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

kent013 / faker-json example snippets


$faker = Faker\Factory::create();
$faker->numberBetween(20, 30);

// same as {"faker_json":true,"method":"numberBetween","parameters":{"min":20,"max":30}}

$json = FakerFormatter::instance()
    ->method('numberBetween')
    ->addParameter('min', 20)
    ->addParameter('max', 30)->toJson(); 
$fakerFormatter = FakerFormatter::fromJson($json);
$result = FakerJson::call($fakerFormatter);

// result is number between 20 and 30

$fakerFormatter = FakerFormatter::instance()
    ->locale('en_HK')
    ->method('direction');
$result = FakerJson::call($fakerFormatter);
$this->assertIsString($result);

FakerFormatterDefinition::listDefinitions()

FakerJson::formatterDefinitionsAsJson();

FakerFormatterDefinition::listLocales()

FakerJson::formatterLocalesAsJson();

FakerJson::addProvider(PHPFakerUtil::class);

 declare(strict_types=1);

namespace App\Providers;

use Faker\Provider\PHPFakerUtil;
use FakerJson\FakerJson;
use Illuminate\Support\ServiceProvider;

class FakerJsonServiceProvider extends ServiceProvider
{
    /**
     * Register services.
     */
    public function register(): void
    {
    }

    /**
     * Bootstrap services.
     */
    public function boot(): void
    {
        FakerJson::addProvider(PHPFakerUtil::class); 
    }
}

'providers' => [
    // ...
    App\Providers\FakerJsonServiceProvider::class,
],