alexrili/phmap 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

alexrili/phmap

Composer 安装命令:

composer require alexrili/phmap

包简介

Payload maps and transform. From A structure to B structure

README 文档

README

This is just a payload map written in php. This lib helps you to get values from A structure and put it on B struscture

Screenshot 2022-12-04 at 02 09 39

Note: Phmap uses illuminate/support under the hood. If you are using laravel or lumen framework, make sure that you version is >= 9.x, if you're not using any of these frameworks or this dependency(illuminate/support) , than you have nothing to worry about.

Install

Via Composer

$ composer require alexrili/phmap

Basic usage

Under the hood we provide a payload_map() helper function. The payload_map() accpets two arguments: 1º is your input data, 2º is your map config. Both needs to be array type. Let`s see the example bellow.

#can be a response from api request
$inputData = [
    'a' => 'Value from A'
];

$map = [
    'from' => 'a',
    'to' => 'b'
];

$response = payload_map($inputData, $map);
// var_dump($response);
// return will be
array:1 [
    "b" => "Value from A"
]

Now let's imagine that you have a multliple values from an array and needs to change the inside properties. With Phmap you can do that :)

# e.g: Imagine that you have the following payload

$oldPayload = [
    'addresses' => [
        [
            'mainAddress' => true,
            'address' => '54 St',
            'code' => '676329-098'
        ],
        [
            'mainAddress' => false,
            'address' => 'Saint Louis Av',
            'code' => '4432-098'
        ]
    ]
];

$map = [
    [
        'from' => 'addresses.*.address',
        'to' => 'users_addresses.*.street_name'
    ],
    [
        'from' => 'addresses.*.code',
        'to' => 'users_addresses.*.postal_code'
    ]    
];

$newPayload = payload_map($inputData, $map);
// var_dump($newPayload);
// return will be
array:2 [
     "users_addresses" => [
        [
            "street_name" => "54 St",
            "postal_code" => "676329-098"
        ],
        [
            "street_name" => "Saint Louis Av",
            "postal_code" => "4432-098
        ]
    ]
]

Notice: The mainAddress property was not in the new payload, this is happening because we're not tells the payload map to map that

Map configs

Direct values

you can map a value from a direct path in your structure just point where the values are in from key and point the destination path within to key

[
    "from" =>"a",
    "to" => "b" 
]

Concatanated values

you can concatanate one or more values using the .+. symbol

[
    "from" =>"a.a1.+.a.a2",
    "to" => "b" 
]

Fixed values

you can add some fixed values using the__()__ symbol

[
    "from" =>"__(This is a fixed value)__",
    "to" => "b" 
]

Collection(array) of values

you can map a nested property inside an array using the .*. symbol

[
    "from" =>"a.*.a1",
    "to" => "b.*.b1" 
]

One Or Antoher value

you can map two(or more) direferente paths to bring a value, then the phmap will go through all the paths until it finds a value. To make one or another value, you can use || symbol

[
    "from" =>"a||a1",
    "to" => "b" // if finds value in [a], brings [a], if not try to brings [b]
]

Nullable values

you may need to map some properties that has a null value, for that you just need to pass a string flag called nullable with one of this two values ["true"|"false"], if trues, means you want to bring this property to you new structure even if their value is null.

Default value is "false"

[
    "from" =>"a",
    "to" => "b" 
    "nullable" => "true" 
    // if [a] don't have value
    // the results will be ["b"=>null] 
]

Advanced usage

$inputData = [
    'a' => [
        [
            'ac1' => 'I`m a multilevel value'
        ],
        [
            'ac1' => 'I`m a multilevel value',
        ]
    ],
    'aDV' => 'I`m a directed value'
];

$map = [
    [
        'from' => 'a.*.ac1.+.__(I`m a fixed value)__.+.aDV',
        'to' => 'b.*.bc'
    ]
];

$response = payload_map($inputData, $map);
// var_dump($response)
// result wil be
array:1 [
  "b" => array:2 [
    0 => array:1 [
      "bc" => "I`m a multilevel valueI`m a fixed valueI`m a directed value"
    ]
    1 => array:1 [
      "bc" => "I`m a multilevel valueI`m a fixed valueI`m a directed value"
    ]
  ]
]

Change log

Please see CHANGELOG for more information on what has changed recently.

Testing

$ composer test

Contributing

Please see CONTRIBUTING and CODE_OF_CONDUCT for details.

Security

If you discover any security related issues, please email alexrili instead of using the issue tracker.

Credits

  • [Alex Ribeiro][link-author]
  • [All Contributors][link-contributors]

License

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

alexrili/phmap 适用场景与选型建议

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-12-05