PHP code example of clabonte / faker-config

1. Go to this page and download the library: Download clabonte/faker-config 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/ */

    

clabonte / faker-config example snippets


$generator = \Faker\Factory::create();
$guesser = new \FakerConfig\ConfigGuesser($generator);

\FakerConfig\ConfigGuesserLoader::loadFile($guesser, 'path_to_your_config.json');

$parser = new \FakerConfig\Parser\FormatParser();
$parser->load($guesser->getGenerator());

// You can use any property defined in the Generator's PhpDoc
$format = $parser->parse("firstName");
$guesser->addFormat('Entity', 'property1', $format);

// Or any method defined in the Generator's PhpDoc
$format = $parser->parse("numberBetween(0,10)");
$guesser->addFormat('Entity', 'property2', $format);

// Wildcard define format to use for a given property for any entity
$format = $parser->parse("uuid");
$guesser->addFormat(\Faker\Guesser\ConfigGuesser::WILDCARD, 'id', $format);

// Specific entity/property format will always take precedence over a wildcard format
$format = $parser->parse("isbn10");
$guesser->addFormat('Book', 'id', $format);

/* Assuming the Book class has the following properties:
   - id
   - property1
   - property2

  And the ConfigGuesser has been configured with the following JSON:
  { 
    "Book": {
        "id": "isbn10",
        "property1": "words(5, true)"
    }
  }
 */

// The following would populate the Book object as follow:
// - 'id' = random ISBN 
// - 'property1' = random string of 5 words
// - 'property2' = no update

$populator = new \FakerConfig\Populator\ObjectPopulator($generator, $guesser);
$book = new Book();
$populator->populate($book); 

$array = array(
    'id' => null,
    'property1' => null,
    'property2' => null,
    'property3' => null);

/*
  Assuming the ConfigGuesser has been configured with the following JSON:
  { 
    "*": {
      "id": "uuid",
    },
    "Entity": {
        "property1": "name",
        "property2": "numberBetween(0,10)"
     }
  }
 */
 
// The following would populate the array as an 'Entity' entity as follow:
// - 'id' = random UUID 
// - 'property1' = random name
// - 'property2' = random number between 0 and 10
// - 'property3' = no update

$populator = new \FakerConfig\Populator\ArrayPopulator($generator, $guesser);
$populator->populate($array, 'Entity');