kozz/collection

基于 SplDoublyLinkedList 的强大数据存储

1.3.2 2014-08-08 22:00 UTC

This package is not auto-updated.

Last update: 2024-09-24 02:11:37 UTC


README

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

基于 SplDoublyLinkedList 的数据结构。

数字键,顺序增加,不可能有间隙。快速顺序迭代。

优点

  • 使用 Lambda 修饰符(参见 addModifier 方法)
  • 与常规数组兼容(实现了 ArrayAccess 接口)

安装

将包添加到您的 composer.json 中,并运行 composer update

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

基本用法

初始化

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

从任何可遍历或迭代器初始化

  1. 您可以使用 Collection::from($traversable) 将集合初始化为基于 SplDoublyLinkedList 的结构

        $traversable = new \ArrayIterator(range(1,1000));
        $collection = Collection::from($traversable);
  2. 您还可以使用您的 Iterator 作为 Collection 的数据容器,使用 new Collection($iterator)。当您尝试使用 Collection 中实现的 ArrayAccessCountable 接口中的任何方法时,您的迭代器将转换为 SplDoublyLinkedList。这是一个好的解决方案,如果您的迭代器是大数据集的游标,您只需要使用 addModifier 添加一些修饰符。

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

修饰符

有时您需要修改集合中的数据

使用 Collection

修饰符对处理数据集非常有帮助。并且使用此 Collection,您只需一行即可轻松添加修饰符

    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']);
    });

因此,修饰符存储在 Collection 中,您有两种方式来应用它

  1. 使用 getFilterIterator() 方法获取应用了所有修饰符的迭代器

        foreach($collection->getFilterIterator() as $item)
        {
            // $item = ['id'=>'4af9f23d8ead0e1d32000000', 'value'=>123]
        }
  2. 调用 ->toArray(),这将调用 getFilterIterator()

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

不使用 Collection

实际上,您可以使用纯 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

添加元素

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

替换元素

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

检查偏移量

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

检索元素

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

删除元素

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