PHPackages                             kent013/faker-json - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. kent013/faker-json

ActiveLibrary

kent013/faker-json
==================

Call faker with json

v0.0.18(3y ago)045MITPHPPHP ^8.0.2

Since Nov 13Pushed 3y ago1 watchersCompare

[ Source](https://github.com/kent013/faker-json)[ Packagist](https://packagist.org/packages/kent013/faker-json)[ RSS](/packages/kent013-faker-json/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (8)Versions (19)Used By (0)

faker-json
==========

[](#faker-json)

Call [FakerPHP/Faker](https://github.com/FakerPHP/Faker) faker formatter with JSON.

The JSON is looks like,

```
{
  "method": "numberBetween",
  "parameters": {
    "min": 20,
    "max": 30
  }
}
```

This json will call faker method as

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

Installation
============

[](#installation)

```
composer require kent013/faker-json

```

Usage
=====

[](#usage)

Call faker method
-----------------

[](#call-faker-method)

```
// 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
```

Call faker method with locale
-----------------------------

[](#call-faker-method-with-locale)

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

get formatter definitions
-------------------------

[](#get-formatter-definitions)

as array

```
FakerFormatterDefinition::listDefinitions()
```

as json

```
FakerJson::formatterDefinitionsAsJson();
```

get formatter locales
---------------------

[](#get-formatter-locales)

as array

```
FakerFormatterDefinition::listLocales()
```

as json

```
FakerJson::formatterLocalesAsJson();
```

Load third party providers
--------------------------

[](#load-third-party-providers)

To load third party providers to generate value, call addProvider prior to use the provider.

```
FakerJson::addProvider(PHPFakerUtil::class);
```

Especially in Laravel, create `FakerJsonServiceProvider` and add it to `app.provider` config

```
