arandu / reducible
作为特性的可约模式
v1.0.0
2024-03-08 01:23 UTC
Requires
- php: ^8.1
Requires (Dev)
- phpunit/phpunit: ^10.5
README
一个通过一组还原器使您的类可自定义的特性
安装
composer require arandu/reducible
用法
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, // ], // ]
高级用法
阅读高级用法文档。
许可
本软件包是开源软件,受MIT许可证许可。
测试
composer test