Download the PHP package clabonte/faker-config without Composer
On this page you can find all versions of the php package clabonte/faker-config. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package faker-config
faker-config
FakerConfig is a Faker extension to populate entities via a simple JSON configuration file.
FakerConfig provides an easy way to configure the format to use when generating data for a given entity/property combination.
With this extension, one can create a simple JSON configuration file to describe how to format various Entities and their properties. FakerConfig will parse Faker's Generator PHPDoc used to identify the list of valid formats that can be used by the configuration file and will validate the configuration file against to to reject any format that won't be understood by Faker
Table of Contents
- Configuration File
- Step 1. Create the ConfigGuesser
- Step 2. Load the Configuration File
- Alternate Solution: Configure the Guesser Programmatically
- Step 3. Populate Your Entity
- Populate an Object Entity
- Populate an Array Entity
Configuration File
The configuration is done via a very simply JSON file that lists out entities to populate as JSON objects ('*' = wildcard). For each entity, you simply list out the properties you want to populate along with the format to use.
Here is a sample configuration file:
Sample configuration files are available in the project:
- Simple file: example-config.json
- Configuration with class hierarchy: guesser-object-config.json
Step 1. Create the ConfigGuesser
The first step consists in creating a ConfigGuesser object with the generator to use:
Step 2. Load the Configuration File
Then, you need to tell the guesser the list of entities/properties that need to be formatted when populating data.
The easiest way to do so is by loading your JSON configuration file:
Alternate Solution: Configure the Guesser Programmatically
Alternatively, you can also configure the guesser programmatically using the FormatParser:
Step 3. Populate Your Entity
Once the ConfigGuesser has been properly configured, you can use it with a populator to fill your entity. FakeConfig provides 2 populators to do so:
- ObjectPopulator: To populate an object entity
- ArrayPopulator: To populate an associative array entity
Populate an Object Entity
The ObjectPopulator can be used to populate any object automatically based on its class hierarchy. The populator will scan the object class and all of its ancestor to identify the list of properties that must be populated and apply the format defined in your configuration.
Populate an Array Entity
You can also populate any associative array using a similar approach: