承接 kozz/collection 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

kozz/collection

最新稳定版本:1.3.2

Composer 安装命令:

composer require kozz/collection

包简介

Powerful Data Storage based on SplDoublyLinkedList

README 文档

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

统计信息

  • 总下载量: 19.97k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 0
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 1
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-07-29