alcaeus/fluent-facade

Library to create fluent interfaces for classes that don't use them

0.1.0 2018-10-27 07:29 UTC

This package is auto-updated.

Last update: 2023-01-06 06:46:01 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();