voilab/mapping

将一组数据映射到恒定输出的映射系统

1.2.5 2016-12-13 10:50 UTC

This package is auto-updated.

Last update: 2024-09-20 20:56:56 UTC


README

安装

通过 Composer

在项目根目录创建 composer.json 文件

{
    "require": {
        "voilab/mapping": "1.*"
    }
}
$ composer require voilab/mapping

示例数据集

// have a mapping instance
$mapping = new \voilab\mapping\Mapping();

// have a set of datas. This could also be an object with
// methods like getName(), getInterests(), etc.
$data = [
    'id' => 1,
    'name' => 'John',
    'email' => 'john@doe.ch',
    'interests' => [
        [
            'type' => 'intellect',
            'description' => 'Some book',
            'contact' => [
                'name' => 'Some author'
            ]
        ],
        [
            'type' => 'sport',
            'description' => 'Football'
        ]
    ],
    'bestFriend' => [
        'id' => 2,
        'name' => 'Fred'
        'age' => 30
    ],
    'work' => [
        'description' => 'Free worker',
        'section' => [
            'type' => 'Social'
        ]
    ]
];

示例

此脚本可以帮助您获得一个恒定的输出结构,无论输入数据如何填充(数组或对象)。

简单映射

$mapped = $mapping->map($data, [
    'id',
    'name'
]);

// results in
[
    'id' => 1,
    'name' => 'John'
]

函数调用映射

$mapped = $mapping->map($data, [
    'id',
    'email' => function ($data) {
        return $data['email'] == 'john@doe.ch'
            ? 'some.default@email.ch'
            : $data['email'];
    }
]);

// results in
[
    'id' => 1,
    'email' => 'some.default@email.ch'
]

一对一或一对多关系映射

$mapped = $mapping->map($data, [
    'work' => [
        'section' => [
            'type'
        ]
    ]
]);

// results in
[
    'work' => [
        'section' => [
            'type' => 'Social'
        ]
    ]
]

一对多或多对多关系映射

$mapped = $mapping->map($data, [
    'interests' => [[
        'description'
    ]]
]);

// results in
[
    'interests' => [
        ['description' => 'Some book'],
        ['description' => 'Football']
    ]
]

复杂关系映射

$mapped = $mapping->map($data, [
    'interests' => [[
        'contact' => [
            'name'
        ]
    ]]
]);

// results in
[
    'interests' => [
        ['contact' => [
            'name' => 'Some author'
        ]],
        ['contact' => null]
    ]
]

更改映射键

$mapped = $mapping->map($data, [
    'id',
    'personName' => 'name'
]);

// results in
[
    'id' => 1,
    'personName' => 'John'
]

更改关系映射键

$mapped = $mapping->map($data, [
    \voilab\mapping\Mapping::rel('work', 'nextWork') => [
        'description'
    ]
]);

// results in
[
    'nextWork' => [
        'description' => 'Free worker'
    ]
]

作为替代,您可以使用以下表示法。代码不久将发生变化。

$mapped = $mapping->map($data, [
    'work as nextWork' => [
        'description'
    ]
]);

通配符映射

在对象中是实验性的。

$mapped = $mapping->map($data, [
    'name',
    'bestFriend' => [
        '*'
    ]
]);

// results in
[
    'name' => 'John',
    'bestFriend' => [
        'id' => 2,
        'name' => 'Fred'
        'age' => 30
    ]
]

高级函数映射使用

如果您在集合内部,您可能想访问当前索引。这很简单:

$special_for_interest = [
    ['data' => 1],
    ['data' => 2]
];

$mapped = $mapping->map($data, [
    'interests' => [[
        'description',
        'special' => function ($interest, $index) use ($special_for_interest) {
            return $special_for_interest[$index]['data'];
        },
        'contact' => [
            'name' => function ($contact, $index, $indexes, $parents) {
                // if you want to access the parent interest. The first parent
                // is the top parent: 0 => main data, 1 => interest
                return $contact['name'] . ' for ' . $parents[1]['description'];
            }
        ]
    ]]
]);

// results in
[
    'interests' => [
        [
            'description' => 'Some book',
            'special' => 1,
            'contact' => [
                'name' => 'Some author for Some book'
            ]
        ],
        [
            'description' => 'Football',
            'special' => 2,
            'contact' => null
        ]
    ]
]

填充器

填充器是从初始源提取数据的对象。这个库包装了两个默认值,用于数组管理和简单的对象(具有驼峰式命名的方法)。

更改填充器

如果您的数据需要不同的处理,您需要创建自己的填充器(必须扩展 \voilab\mapping\Hydrator),然后在构造时设置它们

$mapping = new \voilab\mapping\Mapping(
    new \my\object\Hydrator(),
    new \my\array\Hydrator()
);

请检查包装的填充器源代码以了解其工作原理。它相当简单。

插件

简介

配置

“深度一对一或多对一关系映射”插件始终处于活动状态。如果您想添加其他插件,只需在初始化映射对象时这样做即可

$mapping->addPlugin(new \voilab\mapping\plugin\FirstInCollection());

禁用插件管理

如果您不想调用任何插件(即使是默认插件),只需将插件键分隔符设置为 null。

$mapping->setPluginKeySeparator(null);

深度一对一或多对一关系映射

遍历一对一或多对一关系树,直到达到键(此处:section 的类型和 interests 的名称)。

$mapped = $mapping->map($data, [
    'sectionType' => 'work.section.type',
    'interests' => [[
        'contactName' => 'contact.name'
    ]]
]);

// results in
[
    'sectionType' => 'Social',
    'interests' => [
        ['contactName' => 'Some author']
        ['contactName' => null]
    ]
]

深度优先一对多或多对多关系映射

在遇到一对多或多对多关系时,它获取集合中的第一个元素,然后尝试获取其他关系,在访问键之前(此处:name)。

$mapping->addPlugin(new \voilab\mapping\plugin\FirstInCollection());

$mapped = $mapping->map($data, [
    'firstInterestContactName' => 'interests[].contact.name'
]);

// results in
[
    'firstInterestContactName' => 'Some author'
]

测试

$ phpunit

安全性

如果您发现任何与安全性相关的问题,请使用问题跟踪器。

致谢

许可证

MIT 许可证(MIT)。有关更多信息,请参阅 许可证文件