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)
Add the package to your composer.json
and run composer update
.
{
"require": {
"kozz/collection": "*"
}
}
Initializing
use Kozz\Components\Collection;
$collection = new Collection();
Initializing from any Traversable or Iterator
-
You can initiate collection as SplDoublyLinkedList-based structure with
Collection::from($traversable)
$traversable = new \ArrayIterator(range(1,1000)); $collection = Collection::from($traversable);
-
You also able to use your
Iterator
asCollection
's data container withnew Collection($iterator)
. Your iterator will converts to SplDoublyLinkedList once you try use any method fromArrayAccess
orCountable
interfaces implemented inCollection
. This is good solution if your iterator is cursor in big DB Data Set and you need just add some modifiers withaddModifier
$mongo = new \MongoClient(); $cursor = $mongo->selectDB('testDB')->selectCollection('testCollection')->find(); $collection = new Collection($cursor);
Sometimes you should modify your data in 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:
-
use
getFilterIterator()
method to get an Iterator with all applied modifiers:foreach($collection->getFilterIterator() as $item) { // $item = ['id'=>'4af9f23d8ead0e1d32000000', 'value'=>123] }
-
Call
->toArray()
that callsgetFilterIterator()
:$array = $collection->toArray(); //$item = [ 0=> ['id'=>'4af9f23d8ead0e1d32000000', 'value'=>123], ...] foreach($array as $item) { //do stuff }
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]
}
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]);