Skip to content

Latest commit

 

History

History
99 lines (73 loc) · 1.55 KB

CHAINING.md

File metadata and controls

99 lines (73 loc) · 1.55 KB

Chaining

Methods

chain

  • alias: Any::chain

Iterate over a list of elements, yielding each in turn to an $callable function

Parameters
  • {mixed} $value - manipulation target
Syntax
{Set}::chain($target): Chain
Example #1
<?php
use Dsheiko\Extras\Chain;

// Chain of calls
$res = Chain::chain([1, 2, 3]) // same as Arrays::chain([1, 2, 3])
    ->map(function($num){ return $num + 1; })
    ->filter(function($num){ return $num > 1; })
    ->reduce(function($carry, $num){ return $carry + $num; }, 0)
    ->value(); // 9
Example #2
<?php
use Dsheiko\Extras\Chain;

class MapObject
{
    public $foo = "FOO";
    public $bar = "BAR";
}

$res = Chain::chain(new MapObject)
  ->keys()
  ->value(); // ["foo", "bar"]

then

Binds a then (transformer) function to the chain

Parameters
  • {callable} $callable - then function
Syntax
{Set}::then($function): Chain
Example
<?php
use Dsheiko\Extras\Chain;

$res = Chain::chain(new \ArrayObject([1,2,3))
        // same as Collections::chain(new \ArrayObject([1,2,3])
        ->toArray()
        ->then("json_encode")
        ->value(); // "[1,2,3]"

tap

Underscore syntax for then

Parameters
  • {callable} $callable - then function
Syntax
{Set}::tap($function): Chain

value

Extracts the value of a wrapped object.

Syntax
$chain::value()