phlak / collection
Lightweight collection library
Requires
- php: >=5.6
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.3
- phpunit/phpunit: ^5.7
This package is auto-updated.
Last update: 2019-11-07 05:27:22 UTC
README
Lightweight collection library -- by, Chris Kankiewicz
Requirements
- PHP >= 5.6
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.