salamanderbe/array-mapper

基于配置将对象映射到数组的简单映射器

0.1.3 2020-10-22 10:07 UTC

This package is auto-updated.

Last update: 2024-09-22 18:33:57 UTC


README

安装

composer require salamanderbe/array-mapper

示例

基本示例

给定一个简单的对象,其具有以下值(JSON)

{
    "identifier": "1",
    "title": "my basic object"
}

当我们把这个对象(例如 json_decode)传递给以下映射配置的映射函数时

$mapping = [
    'id' => 'identifier',
    'name' => 'title',
];

该函数将源对象的 identifier 字段映射到输出对象的 id 字段,对 titlename 也是如此。结果是以下数组

[
    'id' => '1',
    'name' => 'my basic object'
]

固定值

从以下源对象开始

{
    "identifier": "1",
    "title": "my basic object"
}

使用 # 符号,我们可以轻松添加源对象上不存在的固定值

$mapping = [
    'id' => 'identifier',
    'name' => 'title',
    'type' => '#my type',
];

现在我们有一个包含我们的固定值的数组

[
    'id' => '1',
    'name' => 'my basic object',
    'type' => 'my type',
]

注意:如果您的源字段包含一个 # 字符,您可以通过添加另一个 # 字符来转义它,例如 ##type

嵌套

使用上述相同的对象

{
    "identifier": "1",
    "title": "my basic object"
}

我们可以使用如下配置轻松创建嵌套映射

$mapping = [
    'id' => 'identifier',
    'nested' => [
        'name' => 'title',
    ]
];

这将产生以下数组

[
    'id' => '1',
    'nested' => [
        'name' => 'my basic object'
    ]
]

源对象上的嵌套对象字段

当您的源对象有一个子对象时

{
    "identifier": "1",
    "title": "my basic object",
    "child": {
        "child_name": "my child"
    }
}

您可以使用 . 符号来访问子字段,例如以下配置示例

$mapping = [
    'id' => 'identifier',
    'name' => 'title',
    'child_name' => 'child.child_title',
];

子对象的 child_title 字段现在被映射到输出对象的 child_name 字段,结果是以下输出

[
    'id' => '1',
    'name' => 'my basic object',
    'child_name' => 'my child'
]

源对象上的数组

给定以下对象

{
    "identifier": "1",
    "title": "my basic object",
    "children": [
        {
            "child_title": "my first child"
        },
        {
            "child_title": "my second child"
        }
    ]
}

当我们想要映射包含在子对象中的字段时,我们可以使用 * 符号来指示我们想要从对象数组中获取一个字段,例如

$mapping = [
    'id' => 'identifier',
    'name' => 'title',
    'child_names' => 'children.*.child_title',
];

现在输出数组上的 child_names 字段将包含所有子对象的 child_title 字段

[
    'id' => '1',
    'name' => 'my basic object',
    'child_names' => [
        'my first child',
        'my second child'
    ]
]

结果数组中的数组

假设您想将子对象映射到嵌套数组中

{
    "identifier": "1",
    "title": "my basic object",
    "children": [
        {
            "child_title": "my first child"
        },
        {
            "child_title": "my second child"
        }
    ]
}

我们可以在输出字段上使用 .* 符号来指示我们想要映射多个子对象的字段

$mapping = [
    'id' => 'identifier',
    'name' => 'title',
    'children.*' => [
        'name' => 'children.*.child_title',
    ],
];

现在所有源对象的子对象的 child_title 字段都将映射到输出数组上的 children 数组,children 中的每个项目都是一个数组,包含子对象的标题字段被映射到输出对象的名称字段

[
    'id' => '1',
    'name' => 'my basic object',
    'children' => [
        [
            'name' => 'my first child'
        ],
        [
            'name' => 'my second child'
        ],

    ]
]

合并结果数组中的数组

假设我们有一个包含2个数组的源对象,我们希望将它们合并成一个结果

{
    "identifier": "1",
    "title": "my basic object",
    "children": [
        {
            "child_title": "my first child"
        },
        {
            "child_title": "my second child"
        }
    ],
    "grandchildren": [
        {
            "grandchild_title": "my first grandchild"
        },
        {
            "grandchild_title": "my second grandchild"
        }
    ]
}

我们可以使用与数组相同的语法来合并这些数组,但不是嵌套多个源

$mapping = [
    'id' => 'identifier',
    'name' => 'title',
    'descendants.*' => [
        ['name' => 'children.*.child_title'],
        ['name' => 'grandchildren.*.grandchild_title'],
    ],
];

这将产生一个结果数组,其中包含一个 descendants 字段,包含源对象的 childrengrandchildren

[
    'id' => '1',
    'name' => 'my basic object',
    'descendants' => [
        [
            'name' => 'my first child'
        ],
        [
            'name' => 'my second child'
        ],
        [
            'name' => 'my first grandchild'
        ],
        [
            'name' => 'my second grandchild'
        ],
    ]
]