biig/optimus

0.2.3 2019-03-06 11:05 UTC

This package is auto-updated.

Last update: 2024-08-28 21:51:37 UTC


README

该库的目标是使用 YAML 配置文件将两个数组(输入和输出)映射。

安装

您可以使用以下命令使用 composer 安装此库

composer require biig/optimus

用法

要使用此库,您必须创建一个扩展 AbstractMappingTransformer 类的转换器。

此转换器将读取映射并将其应用于输入数据。

基本示例

转换器

首先,您必须创建您的转换器并实现 transform 方法。
这里我们使用 symfony/yaml ^3.4 来解析 YAML 文件。

<?php
 
namespace Transformer;
 
use Biig\Optimus\AbstractMappingTransformer;
use Symfony\Component\Yaml\Yaml;
 
class MyTransformer extends AbstractMappingTransformer
{
    /**
     * {@inheritdoc}
     */
    protected function transform(array $inputArray)
    {
        $config = Yaml::parseFile('/mapping.yaml');
        $result = $this->transformFromMapping($config['transformer']['mapping'], $inputArray);
        // ...
        return $result;
    }
}

现在,$result 变量包含新的数组。

配置

假设您得到了 PHP 数组 $inputArray(输入数据)

$inputArray = [
    'user' => [
        'civility' => 'M',
        'firstname' => 'John',
        'lastname' => 'Doe',
    ],
    'title' => 'A title',
];

并且您想将其转换为以下数组(预期输出)

$outputArray = [
    'title' => 'A title',
    'participants' => [
        0 => [
            'civility' => 'M',
            'name' => 'Doe',
        ],
    ],
];

您将需要实现以下 YAML

# mapping.yaml
transformer:
    mapping:
        title:
            from: 'title'
            to: 'title'
        participants_1_civility:
            from: 'user.civility'
            to: 'participants.0.civility'
        participants_1_name:
            from: 'user.lastname'
            to: 'participants.0.name'

可用的映射选项

您可以声明您的节点

  • from : 字符串,输入数组中键的路径
  • to : 字符串,输出数组中键的路径
  • function : 数组,用于转换数据的 PHP 函数
    • name : 字符串,函数的名称
    • params : [可选] 数组,函数的参数(输入数组中键的路径)
  • required : 布尔值,如果键是 required
  • condition : 数组,指定键是否必须的条件

示例

将键 a 转换为键 b

from: a
to: b

使用函数获取键 b

to: b
function:
    name: getB
    params: [a]

注意:函数 getB($arg1) 必须在您的相关转换器中声明 您可以使用函数将输入值转换为预期值。例如,将人的礼貌(先生,夫人)转换为数字(1,2)

public function getCivility($civility)
{
    $array = [
        'Mr' => 1,
        'Mrs' => 2,
    ];
    
    return $array[$civility];
}

使字段为必填项

实际上,如果 from 值在您的输入数组中不存在,则 to 字段不会出现在输出数组中。
如果您想使此字段为必填项,可以添加 required 键。

from: a
to: b
required: true # Default to false

使用默认值

实际上,如果 from 值在您的输入数组中不存在,则 to 字段不会出现在输出数组中。
如果您想使此字段获取默认值,可以添加 default 键。

from: a
to: b
default: 1 # If key "a" don't exist, "b" value will be "1"
from: a
to: b
default:
    function:
        name: getB # You can also use a function to define default value

添加条件

如果您只想在字段 B 存在时转换字段 A。

from: a
to: foo
condition:
    exists: b

您可以在条件中放置多个字段

from: a
to: foo
condition:
    exists:
        - b
        - c

一次获取所有错误映射

如果您想一次返回所有错误,可以在配置中设置以下参数

# mapping.yaml
transformer:
    parameters:
      show_all_errors: true
    mapping:
        title:
            from: 'title'
            to: 'title'
        participants_1_civility:
            from: 'user.civility'
            to: 'participants.0.civility'
        participants_1_name:
            from: 'user.lastname'
            to: 'participants.0.name'