The Nacosvel Components
Table of Contents
You can install the package via Composer:
composer require nacosvel/transformer-mapper// 测试示例
$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'],
];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',
];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
Thanks goes to these wonderful people:
Contributions of any kind are welcome!
Distributed under the MIT License (MIT). Please see License File for more information.