alcaeus / fluent-facade
Library to create fluent interfaces for classes that don't use them
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-10-06 10:41:25 UTC
README
🏡 Library to create fluent interfaces for classes that don't support them
Installation
To install using composer, run
$ composer require alcaeus/fluent-facade
Usage
To wrap an object that does not provide a fluent interface, use the static
ClassWrapper::wrap
helper, then use the class as if it provided a fluent
interface:
use Alcaeus\Fluent\ClassWrapper; ClassWrapper::wrap($object) ->doSomething() ->doSomethingElse();
Getters and other return values
Any non-object return value from a method is ignored and discarded. If a method
returns an object, this object is automatically wrapped and returned, allowing
nested calls. You can end this nesting by calling the end()
method:
use Alcaeus\Fluent\ClassWrapper; ClassWrapper::wrap($object) ->doSomething() ->getNestedObject() ->doSomethingElse() ->end() ->getOtherNestedObject() ->doYetAnotherThing();
Note that it's not possible to call end()
on the root object that was wrapped.
Public properties
If your class contains public properties that contain objects, you can access them and get a wrapped instance of the original object. Properties that contain non-object values will cause an exception to be thrown.
use Alcaeus\Fluent\ClassWrapper; ClassWrapper::wrap($object) ->doSomething() ->nestedObject ->doSomethingElse() ->end() ->otherNestedObject ->doYetAnotherThing();