phlak/collection

This package is abandoned and no longer maintained. The author suggests using the tightenco/collect package instead.

Lightweight collection library

0.1.0 2017-09-04 07:33 UTC

This package is auto-updated.

Last update: 2019-11-07 05:27:22 UTC


README

Latest Stable Version Total Downloads Author License Build Status StyleCI

Lightweight collection library -- by, Chris Kankiewicz

Requirements

Install with Composer

composer require phlak/collection

Initializing the Client

First, import Collection:

use PHLAK\Collection;

Then pass an array of items to the Collection class or the static make method:

$collection = new Collection\Collection(['foo', 'bar', 'baz']);

// or

$collection = Collection\Collection::make(['foo', 'bar', 'baz']);

Usage

Iterate over each item in a collection and perform an action via a Closure:

$collection->each(Closure $function);

Map each item of the collection to a new value via a Closure:

$collection->map(Closure $function);

Filter the items in a collection by returning only the items where the Closure returns true:

$collection->filter(Closure $function);

Filter the items in a collection by returning only the items where the Closure returns false (opposite of the filter method):

$collection->reject(Closure $function);

Reduce a collection down to a single item by iterating over the Closure until a single item remains:

$collection->reduce(Closure $function, $initial);

Sum all the items in a collection and return the value:

$collection->sum(Closure $function, $initial);

Changelog

A list of changes can be found on the GitHub Releases page.

Troubleshooting

Please report bugs to the GitHub Issue Tracker.

Copyright

This project is liscensed under the MIT License.