nacosvel/transformer-mapper
最新稳定版本:v1.0.0
Composer 安装命令:
composer require nacosvel/transformer-mapper
包简介
The Nacosvel Components
README 文档
README
Transformer Mapper
The Nacosvel Components
Table of Contents
Installation
You can install the package via Composer:
composer require nacosvel/transformer-mapper
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!
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature) - Commit your Changes (
git commit -m 'Add some AmazingFeature') - Push to the Branch (
git push origin feature/AmazingFeature) - Open a Pull Request
Contributors
Thanks goes to these wonderful people:
Contributions of any kind are welcome!
License
Distributed under the MIT License (MIT). Please see License File for more information.
统计信息
- 总下载量: 7
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-12-22