decodelabs/fluidity

Tools for creating fluent interfaces

v0.2.1 2022-03-09 16:38 UTC

This package is auto-updated.

Last update: 2022-08-10 11:40:10 UTC


README

PHP from Packagist Latest Version Total Downloads GitHub Workflow Status PHPStan License

Tools for creating fluent interfaces.

Installation

Install via Composer:

composer require decodelabs/fluidity

PHP version

Please note, the final v1 releases of all Decode Labs libraries will target PHP8 or above.

Current support for earlier versions of PHP will be phased out in the coming months.

Usage

Method chaining

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;
}

Create fluent object interfaces with basic generic logic structure support.

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
    });

Licensing

Fluidity is licensed under the MIT License. See LICENSE for the full license text.