PHP-Collection is an easy to use library for handling sets of data with support for filtering, sorting and reducing the data sets.
The class Collection extends PHP's ArrayObject and so brings a little extra to the table.
You can just clone the repository and use it as you see fit or use Composer and handle the dependency management very efficiently.
Append the following line to the require-key of your composer.json file.
"fschaechter/php-collection": "~2"
You can report bugs, problems and other issues here. Every useful bit is very much appreciated.
First you need a some data and create a collection.
<?php
$data = array(
"Apple",
"Apple,"
"Pear",
"Been",
"Pear",
"Strawberry",
);
$collection = new \Collection\Collection($data);
Append some more data.
<?php
$collection->append("Been");
$collection[] = "Pear";
Now you can just iterate over the data.
<?php
echo sprinft(
"We have %s items." . PHP_EOL,
count($collection)
);
foreach ($collection as $item) {
echo $item . PHP_EOL;
}
It would be nice if the collection is sorted.
<?php
$comparator = new \Collection\Comparator\String();
$sorter = new \Collection\Sorter\Uasort($comparator);
$collection->sort($sorter);
foreach ($collection as $item) {
echo $item . PHP_EOL;
}
We don't like apples.
<?php
$filter = new \Collection\Filter\Unequal("Apple");
$collection = $collection->filter($filter);
foreach ($collection as $item) {
echo $item . PHP_EOL;
}
Let's distill the items.
<?php
$reducer = new \Collection\Reducer\Distinct();
$collection = $collection->reduce($reducer);
foreach ($collection as $item) {
echo $item . PHP_EOL;
}
Take a look at the examples directory, there you will find three example of how to play with a collection.
- Look at example_filter.php to see the filter system in action.
- Look at example_reduce.php to see how a reducer melts away unwanted data.
- Look at example_sort.php to see how you can sort a collection of objects.
This library is developed under the principles of TDD. You can find all the tests within the tests directory. Check for yourself using PHPUnit.
$ bin/phpunit
Apache Ant is used for the complete build process. See build.xml, the file is based on the one you can find in PHPUnit's github.com repo.
$ ant
And Travis-CI makes sure its tested over and over again.
All commands are installed to the bin directory by Composer. Add this directory to the environment path and there are easy accessible.
Here's an example for BASH, add the line to the ~/.bashrc.
PATH="./bin:$PATH"
Now it is very simple to run the unit tests.
$ phpunit
Instead of
$ bin/phpunit
So much more productivity gained.
I come across a lot of data in form of list almost daily and i need an easy, testable and pragmatic way to extract information of these lists.
This library is licensed under the terms of the GNU General Public License v3.
Added Travis CI status image. Updated documentation.
Added exception classes. Added a new filter for simple filtering of arrays.
Added isEmpty() to Collection, so you can check if the collection is empty or has some elements.
Added method getFirst() to Collection. This makes it easier to access the first element of the collection.
There was a bug in examples/example_sort.php.
The comparators moved from the namespace \Collection\Sorter\Comparator to \Collection\Comparator. This changed the API incompatibly with the prior version 1.0.0.
This is the initial project.