, (*1)
Usage
Collection solve few things by implementing following interfaces: \Countable, \ArrayAccess, \Iterator, and \SortableInterface.
This gives you an ability to count() collection, use a foreach on it, access it like an array $a[1] and sort its contents $a->sort($sorter).
Apart from regular collections there are also LazyCollection's that allow you to specify a closure that will initialize collection
contents only if it's needed., (*2)
Create your first collection:, (*3)
$collection = new Collection();
$collection->add('item1');
$collection->add('item2');
$collection->add('item3');
Use it:, (*4)
echo count($collection); // returns 3
echo $collection[0]; // returns "item1"
echo $collection->slice(1, 2); // returns Collection with a length of 2 containing item2 and item3.
echo $collection->filter(function($element, $offset){ return $offset % 2 == 0; }); // returns sub-collection with all elements with even offset number
$collection->sort(SorterInterface $sorter); // sorts collection
Lazy collection example:, (*5)
$lazy = new LazyCollection(function(){
return new Collection(array(1, 2, 3));
});
echo $lazy[2]; // initializes the closure and returns "3"
Installation (Composer)
{
"require": {
"phpextra/collection":"~1.0"
}
}
Changelog
No releases yet
Contributing
All code contributions must go through a pull request.
Fork the project, create a feature branch, and send me a pull request.
To ensure a consistent code base, you should make sure the code follows
the coding standards.
If you would like to help take a look at the list of issues., (*6)
Requirements
See composer.json for a full list of dependencies.
Authors
Jacek Kobus - <kobus.jacek@gmail.com>
See the file LICENSE.txt for copying permission.on.