aerni/factory

快速生成虚假内容

v1.5.1 2024-08-16 13:45 UTC

This package is auto-updated.

Last update: 2024-09-11 21:28:58 UTC


README

Statamic Packagist version Packagist Total Downloads

工厂

此插件提供了一个简单的方法,通过使用 Faker,快速为您的 Collection EntriesTaxonomy TermsGlobals 生成虚假数据。

安装

使用 Composer 安装插件

composer require aerni/factory

发布包的配置

php please vendor:publish --tag=factory-config

以下配置将发布到 config/factory.php

return [

    /*
    |--------------------------------------------------------------------------
    | Publish Status
    |--------------------------------------------------------------------------
    |
    | The publish status of collection entries and taxonomy terms
    | created by the factory.
    |
    | Tip: Randomize the status by setting this to '(bool) random_int(0, 1)'.
    |
    */

    'published' => true,

    /*
    |--------------------------------------------------------------------------
    | Title Fallback Settings
    |--------------------------------------------------------------------------
    |
    | These title settings will function as a fallback to create titles for
    | your collection entries and taxonomy terms, if you didn't explicitly set
    | a 'title' field in the respective blueprint.
    |
    | 'chars': The character count of the title will be in this range.
    | 'real_text': Use real english words instead of Lorem Ipsum.
    |
    */

    'title' => [
        'chars' => [$min = 20, $max = 30],
        'real_text' => true,
    ],

];

基本用法

在开始疯狂生成虚假数据之前,您需要让工厂知道您希望它创建哪些字段。您可以通过在蓝图中的每个字段上定义一个 factory 键来实现这一点,以便对要生成虚假数据的字段进行模拟。键的值是 Faker 格式化器,用于简单字段类型,以及用于像网格这样的高级字段类型的选项数组。请参阅 Faker 文档 了解可用的格式化器。

这是一个关于人员集合的示例蓝图

title: Person
sections:
  main:
    display: Main
    fields:
      -
        handle: first_name
        field:
          type: text
          factory: firstName
      -
        handle: last_name
        field:
          type: text
          factory: lastName
      -
        handle: age
        field:
          type: number
          factory: numberBetween(20, 50)
      -
        handle: bio
        field:
          type: textarea
          factory: paragraph(3, true)

在终端中运行工厂并遵循说明

php please factory

特殊字段类型

上述示例对基本字段类型效果很好。但对于 Bard、Replicator、网格和表格呢?我很高兴你问了。为了生成这些“特殊字段类型”的内容,您需要根据下面的示例更改蓝图。

Bard & Replicator

min_sets 定义要创建的最小集合数。max_sets 定义要创建的最大集合数。

title: 'Bard & Replicator'
sections:
  main:
    display: Main
    fields:
      -
        handle: replicator
        field:
          type: replicator
          sets:
            text:
              display: Text
              factory:
                min_sets: 1
                max_sets: 3
              fields:
                -
                  handle: text
                  field:
                    type: text
                    factory: word
                -
                  handle: textarea
                  field:
                    type: textarea
                    factory: 'paragraph(3, true)'

网格

min_rows 定义要创建的最小行数。max_rows 定义要创建的最大行数。

title: Table
sections:
  main:
    display: Main
    fields:
      -
        handle: grid
        field:
          type: grid
          factory:
            min_rows: 1
            max_rows: 4
          fields:
            -
              handle: first_name
              field:
                type: text
                factory: firstName
            -
              handle: last_name
              field:
                type: text
                factory: lastName

表格

min_rows 定义您想要创建的最小行数。max_rows 定义您想要创建的最大行数。min_cells 定义您想要创建的最小单元格数。max_cells 定义您想要创建的最大单元格数。formatter 定义要使用的 faker 格式化器。

title: Table
sections:
  main:
    display: Main
    fields:
      -
        handle: table
        field:
          type: table
          factory:
            min_rows: 1
            max_rows: 3
            min_cells: 3
            max_cells: 5
            formatter: word