alcaeus / fluent-facade
为不使用流畅接口的类创建流畅接口的库
0.1.0
2018-10-27 07:29 UTC
Requires
- php: ^7.2
Requires (Dev)
- doctrine/coding-standard: ^5.0
- phpstan/phpstan: ^0.10.5
- phpstan/phpstan-deprecation-rules: ^0.10.2
- phpstan/phpstan-strict-rules: ^0.10.1
- phpunit/phpunit: ^7.4
This package is auto-updated.
Last update: 2024-09-06 10:32:50 UTC
README
🏡 为不支持流畅接口的类创建流畅接口的库
安装
使用composer安装,请运行
$ composer require alcaeus/fluent-facade
使用方法
要包装一个不提供流畅接口的对象,请使用静态辅助函数ClassWrapper::wrap
,然后像它提供了流畅接口一样使用该类
use Alcaeus\Fluent\ClassWrapper; ClassWrapper::wrap($object) ->doSomething() ->doSomethingElse();
获取器和其它返回值
方法中的任何非对象返回值都将被忽略并丢弃。如果方法返回一个对象,则该对象将被自动包装并返回,允许嵌套调用。您可以通过调用end()
方法结束嵌套
use Alcaeus\Fluent\ClassWrapper; ClassWrapper::wrap($object) ->doSomething() ->getNestedObject() ->doSomethingElse() ->end() ->getOtherNestedObject() ->doYetAnotherThing();
请注意,无法在包装的根对象上调用end()
公共属性
如果您的类包含包含对象的公共属性,您可以访问它们并获得原始对象的包装实例。包含非对象值的属性将抛出异常。
use Alcaeus\Fluent\ClassWrapper; ClassWrapper::wrap($object) ->doSomething() ->nestedObject ->doSomethingElse() ->end() ->otherNestedObject ->doYetAnotherThing();