crocodile2u/chainy

There is no license information available for the latest version (v1.0) of this package.

v1.0 2018-12-09 07:51 UTC

This package is auto-updated.

Last update: 2024-04-10 19:41:11 UTC


README

Have you ever written code like this?

$array = array_filter($array);
$array = array_map(
    $array,
    function($element) {
        // ... manipulations ...
        return $modifiedElement;
    }
);
sort($array);

Know what? You could do the same in a more readable and nice way:

$chain = (new \chainy\Chain)

    ->filter()

    ->map(
        function($element) {
            // ... manipulations ...
            return $modifiedElement;
        }
    )

    ->sort();

$array = $chain->apply($array);

Chainy is a pipeline of functions, where every next one gets input from the previous one's output. In the example above, when $chain->apply() is called on $array, it goes sequentially through filter(), map() and sort(). As you can expect, those methods are just wrappers for the PHP's built-in functions array_filter, array_map and sort.

This is how things go with chainy:

  1. Create new Chain instance.
  2. Setup the pipeline, adding elements to the chain (in this casem filter to get rid of empty element, map to apply some modifications to every element that survived filter, and then sort the resulting array).
  3. Call Chain->apply() on the input array. The result is the filtered, modified and sorted array.
$chain = (new \chainy\Chain)

+------> filter() --+
|                   |
|    +--------------+
|    |
|    +-> map(function()...) --+
|                             |
|    +------------------------+
|    |
|    +-> sort();
|
| $array = $chain->apply($array);
|                          |
|--------------------------+