浏览代码

Implemented MultiBulkResponseKVIterator to wrap iterators that should be seen as $k => array($MBIterator, $MBIterator++) by external code (think of ZRANGE .. WITHSCORES as a practical example).

Daniele Alessandri 15 年之前
父节点
当前提交
47db5075ec
共有 1 个文件被更改,包括 52 次插入0 次删除
  1. 52 0
      lib/Predis.php

+ 52 - 0
lib/Predis.php

@@ -1223,6 +1223,58 @@ class MultiBulkResponseIterator implements \Iterator, \Countable {
     }
 }
 
+class MultiBulkResponseKVIterator implements \Iterator, \Countable {
+    private $_iterator, $_position, $_current, $_replySize;
+
+    public function __construct(MultiBulkResponseIterator $iterator) {
+        $virtualSize = count($iterator) / 2;
+
+        $this->_iterator   = $iterator;
+        $this->_position   = 0;
+        $this->_current    = $virtualSize > 0 ? $this->getValue() : null;
+        $this->_replySize  = $virtualSize;
+    }
+
+    public function __destruct() {
+        $this->_iterator->sync();
+    }
+
+    public function rewind() {
+        // NOOP
+    }
+
+    public function current() {
+        return $this->_current;
+    }
+
+    public function key() {
+        return $this->_position;
+    }
+
+    public function next() {
+        if (++$this->_position < $this->_replySize) {
+            $this->_current = $this->getValue();
+        }
+        return $this->_position;
+    }
+
+    public function valid() {
+        return $this->_position < $this->_replySize;
+    }
+
+    public function count() {
+        return $this->_replySize;
+    }
+
+    private function getValue() {
+        $k = $this->_iterator->current();
+        $this->_iterator->next();
+        $v = $this->_iterator->current();
+        $this->_iterator->next();
+        return array($k, $v);
+    }
+}
+
 /* ------------------------------------------------------------------------- */
 
 namespace Predis\Commands;