kozz/collection

Powerful Data Storage based on SplDoublyLinkedList

1.3.2 2014-08-08 22:00 UTC

This package is not auto-updated.

Last update: 2021-10-25 12:17:45 UTC


README

Build Status Coverage Status Scrutinizer Code Quality Latest Stable Version Latest Unstable Version License

Data Structure based on SplDoublyLinkedList.

Numerical keys, consequentially increasing, no gaps possible. Quick sequential iterating.

Advantages:

  • Using Lamda Modifiers (see addModifier method)
  • Regular array compatiable (ArrayAccess interface implemented)

Installation

Add the package to your composer.json and run composer update.

{
    "require": {
        "kozz/collection": "*"
    }
}

Basic Usage

Initializing

    use Kozz\Components\Collection;
    $collection = new Collection();

Initializing from any Traversable or Iterator

  1. You can initiate collection as SplDoublyLinkedList-based structure with Collection::from($traversable)

        $traversable = new \ArrayIterator(range(1,1000));
        $collection = Collection::from($traversable);
  2. You also able to use your Iterator as Collection's data container with new Collection($iterator). Your iterator will converts to SplDoublyLinkedList once you try use any method from ArrayAccess or Countable interfaces implemented in Collection. This is good solution if your iterator is cursor in big DB Data Set and you need just add some modifiers with addModifier

        $mongo = new \MongoClient();
        $cursor = $mongo->selectDB('testDB')->selectCollection('testCollection')->find();
        $collection = new Collection($cursor);

Modifiers

Sometimes you should modify your data in collection

With Collection

Modifiers are quite helpful to process DB Data Sets. And with this Collection you are able simply add modifier in just one line:

    use Kozz\Components\Collection;
    
    $mongo = new \MongoClient();
    $cursor = $mongo->selectDB('testDB')->selectCollection('testCollection')->find();
    //[0=>['_id'=>MongoId(...), 'value'=>123], ...]
    
    
    $collection = new Collection($cursor);
    $collection->addModifier(function(&$item){
        $item['id'] = (string)$item['_id'];
    });
    $collection->addModifier(function(&$item){
        unset($item['_id']);
    });

So now Modifiers are stored in Collection and you have two ways to apply it:

  1. use getFilterIterator() method to get an Iterator with all applied modifiers:

        foreach($collection->getFilterIterator() as $item)
        {
            // $item = ['id'=>'4af9f23d8ead0e1d32000000', 'value'=>123]
        }
  2. Call ->toArray() that calls getFilterIterator() :

        $array = $collection->toArray();
        //$item = [ 0=> ['id'=>'4af9f23d8ead0e1d32000000', 'value'=>123], ...]
        foreach($array as $item)
        {
            //do stuff
        }

Without Collection

You actually can modify your data with plain SPL:

    $mongo = new \MongoClient();
    $cursor = $mongo->selectDB('testDB')->selectCollection('testCollection')->find();
    
    $it = new CallbackFilterIterator($cursor, function(&$item){
        $item['id'] = (string)$item['_id'];
        return true;
    });
    $it = new CallbackFilterIterator($it, function(&$item){
        unset($item['_id']);
        return true;
    });
    
    foreach($array as $item)
    {
        // $item = ['id'=>'4af9f23d8ead0e1d32000000', 'value'=>123]
    }

ArrayAccess

Adding element

    $element = 'string';
    $collection->push($element);
    //or
    $collection[] = $element;

Replacing element

    $element2 = new stdClass();
    $collection->set(0, $element2);
    //or
    $collection[0] = $element2;
    // This throws Exception (offset 100 not exists)
    $collection->set(100, $element2);

Check offset

    $collection->exists(0); 
    //or
    isset($collection[0]);

Retrieve element

    $element = $collection->get(0); 
    //or
    $element = $collection[0];

Remove element

    $element = $collection->remove(0);
    //or
    $element = unset($collection[0]);