clabonte/faker-config

Faker扩展,通过简单的JSON配置文件填充实体

v1.0 2017-09-07 00:47 UTC

This package is auto-updated.

Last update: 2024-09-29 04:37:08 UTC


README

FakerConfig是Faker的扩展,通过简单的JSON配置文件填充实体。

FakerConfig提供了一个简单的方式来配置在为给定实体/属性组合生成数据时使用的格式。

Build Status

使用此扩展,可以创建一个简单的JSON配置文件来描述如何格式化各种实体及其属性。FakerConfig将解析Faker的Generator PHPDoc,用于识别配置文件可以使用的有效格式列表,并将验证配置文件以拒绝任何Faker无法理解的格式。

目录

配置文件

配置通过一个非常简单的JSON文件完成,该文件列出了要填充的实体作为JSON对象('*' =通配符)。对于每个实体,您只需列出您想要填充的属性以及要使用的格式。

以下是一个示例配置文件

{
  "*": {
    "id": "uuid"
  },

  "Book": {
    "id": "isbn10"
  },

  "Entity": {
    "property1": "url",
    "property2": "numberBetween(0,10)"
  },

  "Package\\Entity": {
    "property1": "city",
    "property2": "words(5, true)"
  }
}

示例配置文件可在项目中找到

步骤1:创建ConfigGuesser

第一步是创建一个使用生成器的ConfigGuesser对象

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

步骤2:加载配置文件

然后,您需要告诉Guesser在填充数据时需要格式化的实体/属性列表。

这样做最简单的方法是加载您的JSON配置文件

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

替代方案:通过编程配置Guesser

或者,您也可以使用FormatParser通过编程配置Guesser

$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);

步骤3:填充您的实体

一旦ConfigGuesser被正确配置,您就可以使用它和一个填充器来填充您的实体。FakeConfig提供了两个填充器来完成此操作

  • ObjectPopulator:用于填充对象实体
  • ArrayPopulator:用于填充关联数组实体

填充对象实体

ObjectPopulator可以用来自动填充任何对象,基于其类层次结构。填充器将扫描对象类及其所有祖先,以识别必须填充的属性列表,并应用配置中定义的格式。

/* 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');