robertasproniu/php-sortable-collections

sortable collections of items

dev-master 2017-05-24 06:38 UTC

This package is not auto-updated.

Last update: 2024-04-13 23:46:33 UTC


README

An example package that allows to create, merge and sort collections of items.

Collections are sorted using custom algorithm wich can be found in SortableArray.php file

To install use:

composer require robertasproniu/php-sortable-collections dev-master

To run the tests:

./vendor/bin/phpunit

Creating a collection

use SortableCollection\Collection\Collection;
$collection = new Collection($items); // where $items = []

or using a factory class

use SortableCollection\CollectionFactory;
use SortableCollection\Collection\Collection;

class CustomCollectionFactory extends CollectionFactory
{
    public static function create($items) : CollectionInterface
    {
        // do whatever you need 
        
        return new Collection($items);
    }
}

$collection = CustomCollectionFactory::create($data); 

Get items from collection

$collection->get(); // return array

Remove duplicate values from a collection

$collection->unique();

Sort collection values

$collection->sort(function($value1, $value2){
    //conditions
});