decodelabs / fluidity
创建流畅接口的工具
v0.3.4
2023-09-26 09:13 UTC
Requires
- php: ^8.0
Requires (Dev)
README
创建流畅接口的工具。
Fluidity提供了一系列中间件接口,有助于开发旨在提供流畅接口的库。
关注DecodeLabs博客获取新闻和更新 DecodeLabs blog。
安装
通过Composer安装
composer require decodelabs/fluidity
用法
方法链
namespace DecodeLabs\Fluidity; interface Then { public function then(callable $callback): Then; public function thenEach(iterable $values, callable $callback): Then; public function thenIf(?bool $truth, callable $yes, callable $no=null): Then; public function thenUnless(?bool $truth, callable $no, callable $yes=null): Then; }
使用基本的泛型逻辑结构支持创建流畅的对象接口。
use DecodeLabs\Fluidity\Then; use DecodeLabs\Fluidity\ThenTrait; $test = new class() implements Then { use ThenTrait; public function doThing(int $value=null) {} }; $truth = true; $test ->then(function($test) { $test->doThing(); }) ->thenEach([1, 2, 3], function($test, $value) { // Called three times $test->doThing($value); }) ->thenIf($truth, function($test) { // This gets called if($truth) }, function($test) { // This get called otherwise }) ->thenUnless($truth, function($test) { // This gets called if(!$truth) }, function($test) { // This get called otherwise });
许可
Fluidity遵循MIT许可协议。完整的许可协议文本请参阅 LICENSE。