2017 © Pedro Peláez
 

library collection

Powerful Data Storage based on SplDoublyLinkedList

image

kozz/collection

Powerful Data Storage based on SplDoublyLinkedList

  • Thursday, August 28, 2014
  • by urakozz
  • Repository
  • 1 Watchers
  • 4 Stars
  • 13,632 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 4 Forks
  • 0 Open issues
  • 10 Versions
  • 0 % Grown

The README.md

PHP Collection

Build Status Coverage Status Scrutinizer Code Quality Latest Stable Version Latest Unstable Version License, (*1)

Data Structure based on SplDoublyLinkedList., (*2)

Numerical keys, consequentially increasing, no gaps possible. Quick sequential iterating., (*3)

Advantages: - Using Lamda Modifiers (see addModifier method) - Regular array compatiable (ArrayAccess interface implemented), (*4)

Installation

Add the package to your composer.json and run composer update., (*5)

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

Basic Usage

Initializing, (*6)

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

Initializing from any Traversable or Iterator, (*7)

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

        $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, (*9)

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

Modifiers

Sometimes you should modify your data in collection, (*10)

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:, (*11)

    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:, (*12)

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

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

        $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:, (*15)

    $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, (*16)

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

Replacing element, (*17)

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

Check offset, (*18)

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

Retrieve element, (*19)

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

Remove element, (*20)

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

The Versions

28/08 2014

dev-master

9999999-dev http://github.com/urakozz/php-collection

Powerful Data Storage based on SplDoublyLinkedList

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

php collection iterator storage spl cursor linkedlist

09/08 2014

1.3.2

1.3.2.0 http://github.com/urakozz/php-collection

Powerful Data Storage based on SplDoublyLinkedList

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

php collection iterator storage spl cursor linkedlist

08/08 2014

1.3.1

1.3.1.0 http://github.com/urakozz/php-collection

Powerful Data Storage based on SplDoublyLinkedList

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

php collection iterator storage spl cursor linkedlist

06/08 2014

1.3.0

1.3.0.0 http://github.com/urakozz/php-collection

Powerful Data Storage based on SplDoublyLinkedList

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

php collection iterator storage spl cursor linkedlist

04/08 2014

v1.1.2

1.1.2.0 http://github.com/urakozz/php-collection

Powerful Data Storage based on SplDoublyLinkedList

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

php collection iterator storage spl cursor linkedlist

04/08 2014

1.1.1

1.1.1.0 http://github.com/urakozz/php-collection

Powerful Data Storage based on SplDoublyLinkedList

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

collection iterator storage spl linkedlist

04/08 2014

1.1

1.1.0.0 http://github.com/urakozz/php-collection

Powerful Data Storage based on SplDoublyLinkedList

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

collection iterator storage spl linkedlist

02/08 2014

1.0.1

1.0.1.0 http://github.com/urakozz/php-collection

Powerful Data Storage based on SplDoublyLinkedList

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

collection iterator storage spl linkedlist

30/07 2014

1.0.0.x-dev

1.0.0.9999999-dev http://github.com/urakozz/php-collection

Powerful Data Storage based on SplDoublyLinkedList

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

collection iterator storage spl linkedlist

30/07 2014

1.0.0

1.0.0.0 http://github.com/urakozz/php-collection

Powerful Data Storage based on SplDoublyLinkedList

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

collection iterator storage spl linkedlist