gathercontent/config-value-object

此软件包已被弃用且不再维护。没有建议的替代软件包。
此软件包最新版本(0.2.4)没有可用的许可信息。

验证 GatherContent 的项目配置

0.2.4 2018-11-20 11:44 UTC

This package is not auto-updated.

Last update: 2023-05-27 11:13:50 UTC


README

Build Status

确保配置遵守 规则

要求

  • PHP 5.3.0 或更高版本(推荐至少 PHP 7.1)

安装

在您的仓库内运行以下命令

$ composer require gathercontent/config-value-object:0.2.*

用法

$json = 'your config in JSON format goes here!';

$configObject = Config::fromJson($json);

如果配置不遵守 规则,上述代码将抛出 ConfigValueException

规则

配置是一个标签数组。它必须至少有一个标签。

有效配置的示例

[
  {
    "label": "Content",
    "name": "tab1",
    "hidden": false,
    "elements": [
      {
        "type": "text",
        "name": "el1",
        "required": false,
        "label": "Blog post",
        "value": "<p>Hello world</p>",
        "microcopy": "",
        "limit_type": "words",
        "limit": "1000",
        "plain_text": false
      }
    ]
  }
]

标签结构

必须是对象。所有属性都是必需的。不允许有额外的属性。

{
  "label": "Content",                // string, not empty
  "name": "tab1",                    // string, not empty, unique
  "hidden": false,                   // boolean
  "elements": [ /* tab elements */ ] // array
}

元素结构

允许的元素类型

  • 文本
  • 文件
  • 部分
  • 单选选择
  • 复选选择

所有元素都必须是对象。所有属性都是必需的。不允许有额外的属性。

类型 text
{
  "type": "text",                // string, must be "text"
  "name": "el1",                 // string, not empty, unique
  "required": false,             // boolean
  "label": "Blog post",          // string, not empty
  "value": "<p>Hello world</p>", // string
  "microcopy": "",               // string
  "limit_type": "words",         // string, either "words" or "chars"
  "limit": 1000,                 // integer, non-negative
  "plain_text": false            // boolean
}
类型 files
{
  "type": "files",   // string, must be "files"
  "name": "el2",     // string, not empty, unique
  "required": false, // boolean
  "label": "Photos", // string, not empty
  "microcopy": ""    // string
}
类型 section
{
  "type": "section",                 // string, must be "section"
  "name": "el3",                     // string, not empty, unique
  "title": "Title",                  // string, not empty
  "subtitle": "<p>How goes it?</p>"  // string
}
类型 choice_radio
{
  "type": "choice_radio",              // string, must be "choice_radio"
  "name": "el4",                       // string, not empty, unique
  "required": false,                   // boolean
  "label": "Label",                    // string, not empty
  "microcopy": "",                     // string
  "other_option": false,               // boolean
  "options": [ /* element options */ ] // array, must have at least one option
}

如果 other_optiontrue,则至少需要两个选项。

类型 choice_checkbox
{
  "type": "choice_checkbox",           // string, must be "choice_checkbox"
  "name": "el4",                       // string, not empty, unique
  "required": false,                   // boolean
  "label": "Label",                    // string, not empty
  "microcopy": "",                     // string
  "options": [ /* element options */ ] // array, must have at least one option
}

选项结构

所有选项都必须是对象。所有属性都是必需的。不允许有额外的属性。

大多数选项将如下所示

{
  "name": "op1",       // string, not empty, unique
  "label": "Option 1", // string, not empty
  "selected": false    // boolean
}

唯一的例外是当 other_option 属性为 true 时,choice_radio 元素的最后一个选项

{
  "name": "op1",       // string, not empty, unique
  "label": "Other",    // string, not empty
  "selected": true,    // boolean
  "value": "Something" // string
}

如果选项未选中,则 "other" 选项的 value 属性必须为空。

choice_radio 不能有超过一个选中的选项。

测试

运行单元测试

$ ./vendor/bin/phpunit

测试是否符合 PSR2 编码风格指南

$ ./vendor/bin/phpcs --standard=PSR2 ./src