定制 voilab/mapping 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

voilab/mapping

Composer 安装命令:

composer require voilab/mapping

包简介

Mapping system to promote a constant output from a set of datas

README 文档

README

Install

Via Composer

Create a composer.json file in your project root:

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

Sample dataset

// 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'
        ]
    ]
];

Examples

This piece of script helps you to obtain a constant output structure no matter how the input data is hydrated (array or object).

Simple mapping

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

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

Function call mapping

$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'
]

One-to-one or many-to-one relation mapping

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

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

One-to-many or many-to-many relation mapping

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

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

Complex relation mapping

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

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

Change mapping key

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

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

Change relation mapping key

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

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

As an alternative, you may use this notation below. Code should not change anytime soon.

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

Wildcard mapping

It's experimental with objects.

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

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

Advanced function mapping usage

You may want to access the current index inside your function if you are inside a collection. It's as easy as:

$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
        ]
    ]
]

Hydrators

Hydrators are objects that extract data from the initial source. Two defaults, packaged in this lib, are for array management and simple objects (with getters and setters camelcased).

Change hydrators

If your data needs to be handled differently, you will need to create your own hydrators (which must extend \voilab\mapping\Hydrator) and then set them at construction time:

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

Please check packaged hydrators sources to see how it works. It's quite simple.

Plugins

Introduction

Configuration

The plugin "Deep one-to-one or many-to-one relation mapping" is always active. If you want to add other plugins, just do this when initializing the mapping object:

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

Disable plugin management

If you don't want to call any plugin (even the default one), simply set the plugin key separator to null.

$mapping->setPluginKeySeparator(null);

Deep one-to-one or many-to-one relation mapping

Goes through a tree of one-to-one or many-to-one relations, until it reaches the key (here: type for section and name for interests).

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

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

Deep first one-to-many or many-to-many relation mapping

When encountering a one-to-many or many-to-many relation, it fetch the 1st element in the collection, and then tries to fetch the other relations, before accessing the key (here: name).

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

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

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

Testing

$ phpunit

Security

If you discover any security related issues, please use the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

voilab/mapping 适用场景与选型建议

voilab/mapping 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 122 次下载、GitHub Stars 达 1, 最近一次更新时间为 2015 年 02 月 12 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「mapping」 「voilab」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 voilab/mapping 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 voilab/mapping 我们能提供哪些服务?
定制开发 / 二次开发

基于 voilab/mapping 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 122
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 19
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 3
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-02-12