Skip to content

Latest commit

 

History

History
76 lines (58 loc) · 1.55 KB

COLLECTIONS.md

File metadata and controls

76 lines (58 loc) · 1.55 KB

Dsheiko\Extras\Collections

Following types iterable, ArrayObject, Iterator belong to collections. Array extras like ::map, ::reduce do not make sense on a live collection, one can rather convert the collection to an array (::toArray) and then apply array extras.

Methods

chain

Returns a wrapped object. Calling methods on this object will continue to return wrapped objects until value is called.

Parameters
  • {iterable|ArrayObject|Iterator} $collection - source collection
Syntax
 chain({iterable|ArrayObject|Iterator} $collection)
Example
<?php
$res = $res = Collections::chain(new \ArrayObject([1,2,3]))
            ->toArray()
            ->value();
echo $res; // "534"

each

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

Parameters
  • {iterable|ArrayObject|Iterator} $collection - source collection
  • {callable} $callable - iteratee callback
Syntax
each($collection, callable $callable)
Example
<?php
$sum = 0;
$obj = new \ArrayObject([1,2,3]);
Collections::each($obj->getIterator(), function ($i) use (&$sum){
    $sum += $i;
});

toArray

Convert collectionb to an array

Parameters
  • {iterable|ArrayObject|Iterator} $collection - source collection
Syntax
toArray($collection)
Example
<?php
$sum = 0;
$obj = new \ArrayObject([1,2,3]);
$res = Collections::toArray();