承接 nacosvel/transformer-mapper 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

nacosvel/transformer-mapper

最新稳定版本:v1.0.0

Composer 安装命令:

composer require nacosvel/transformer-mapper

包简介

The Nacosvel Components

README 文档

README

Transformer Mapper

The Nacosvel Components

GitHub Tag Total Downloads Packagist Version Packagist PHP Version Support Packagist License

Table of Contents
  1. Installation
  2. Usage
  3. Contributing
  4. Contributors
  5. License

Installation

You can install the package via Composer:

composer require nacosvel/transformer-mapper

[back to top]

Usage

// 测试示例
$input = [
    'user'     => [
        'id'      => 1,
        'name'    => 'Alice',
        'address' => [
            'city' => 'Beijing',
            'zip'  => '100000',
        ],
    ],
    'client'   => [
        'id'      => 2,
        'name'    => 'Bob',
        'address' => [
            'city' => 'Shanghai',
            'zip'  => '200000',
        ],
    ],
    'products' => [
        [
            'id'    => 101,
            'name'  => 'Product A',
            'price' => 100,
        ], [
            'id'    => 102,
            'name'  => 'Product B',
            'price' => 200,
        ],
    ],
    'return.url' => 'https://example.com/pay/return',
    'cancel.url' => 'https://example.com/pay/notify/paypal',
];
// 定义映射规则
$rules = [
    'member.id'            => 'user.id',
    'member.name'          => 'user.name',
    // 'member.address'       => 'user.address',
    'address.address_city' => 'user.address.city',
    'address.address_zip'  => 'user.address.zip',
    'user.id'              => 'client.id',
    'user.name'            => 'client.name',
    'user.address_city'    => 'client.address.city',
    'products.*.uuid'      => 'products.*.id',
    'products.*.user_name' => 'products.*.name',
    'prices'               => [
        'input'     => 'products.*.price',
        'transform' => function ($prices) {
            return array_map(fn($price) => $price * 10, $prices);
        },
    ],
    'return\.url'          => 'return\.url',
    'cancel.url'           => 'cancel\.url',
    'default'              => [
        'input'     => null,
        'default'   => null,
        'transform' => function ($default) {
            return $default ?? 'default';
        },
    ],
    'function'             => fn($data) => $data['client']['name'],
];

WildcardMapper::class

use Nacosvel\TransformerMapper\WildcardMapper;

$wildcard = new WildcardMapper($input);
$mapper   = $wildcard->mapper($rules);

// $mapper
$mapper = [
    'member'     => [
        'id'   => 1,
        'name' => 'Alice',
    ],
    'address'    => [
        'address_city' => 'Beijing',
        'address_zip'  => '100000',
    ],
    'user'       => [
        'id'           => 2,
        'name'         => 'Bob',
        'address_city' => 'Shanghai',
    ],
    'products'   => [
        [
            'uuid'      => 101,
            'user_name' => 'Product A',
        ],
        [
            'uuid'      => 102,
            'user_name' => 'Product B',
        ],
    ],
    'return.url' => 'https://example.com/pay/return',
    'cancel'     => [
        'url' => 'https://example.com/pay/notify/paypal',
    ],
    'prices'     => [
        0 => 1000,
        1 => 2000,
    ],
    'default'    => 'default',
    'function'   => 'Bob',
];

Contributing

Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.

If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

[back to top]

Contributors

Thanks goes to these wonderful people:

contrib.rocks image

Contributions of any kind are welcome!

[back to top]

License

Distributed under the MIT License (MIT). Please see License File for more information.

[back to top]

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-12-22