承接 arandu/reducible 相关项目开发

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

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

arandu/reducible

最新稳定版本:v1.0.0

Composer 安装命令:

composer require arandu/reducible

包简介

Reducible pattern as a trait

README 文档

README

Tests

A trait to make your classes customizable via a set of reducers.

Installation

composer require arandu/reducible

Usage

use Arandu\Reducible\Reducible;

class MyClass
{
    use Reducible;

    public function callApi($url, $options)
    {
        // will call all reducers for the method name
        $options = $this->transformCallApiOptions($options);

        // ...
    }
}

// -----

// Register a 'transformCallApiOptions' reducer
MyClass::reducer('transformCallApiOptions', function ($options) {
    return [
        ...$options,
        'headers' => [
            ...($options['headers'] ?? []),
            'Authorization' => 'Bearer ' . $this->token,
        ],
    ];
});

// Multiple reducers can be added
MyClass::reducer('transformCallApiOptions', function ($options) {
    return [
        ...$options,
        'headers' => [
            ...($options['headers'] ?? []),
            'X-Api-Key' => $this->apiKey,
        ],
    ];
});

// -----

$myClass = new MyClass();

$myClass->callApi('https://api.example.com', [
    'headers' => [
        'Content-Type' => 'application/json',
    ],
]);
// The callApi method will be called with the following options:
// [
//     'headers' => [
//         'Content-Type' => 'application/json',
//         'Authorization' => 'Bearer ' . $myClass->token,
//         'X-Api-Key' => $myClass->apiKey,
//     ],
// ]

Advanced Usage

Read the Advanced Usage documentation.

License

This package is open-sourced software licensed under the MIT license.

Testing

composer test

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-03-08