Browse Source

MultiBulkResponseIterator and MultiBulkResponseKVIterator inherit from a common MultiBulkResponseIteratorBase class.

Daniele Alessandri 15 years ago
parent
commit
6e5c495b26
1 changed files with 32 additions and 53 deletions
  1. 32 53
      lib/Predis.php

+ 32 - 53
lib/Predis.php

@@ -1164,29 +1164,8 @@ class HashRing {
     }
 }
 
-class MultiBulkResponseIterator implements \Iterator, \Countable {
-    private $_connection, $_position, $_current, $_replySize;
-
-    public function __construct($socket, $size) {
-        $this->_connection = $socket;
-        $this->_position   = 0;
-        $this->_current    = $size > 0 ? $this->getValue() : null;
-        $this->_replySize  = $size;
-    }
-
-    public function __destruct() {
-        // when the iterator is garbage-collected (e.g. it goes out of the
-        // scope of a foreach) but it has not reached its end, we must sync
-        // the client with the queued elements that have not been read from
-        // the connection with the server.
-        $this->sync();
-    }
-
-    public function sync() {
-        while ($this->valid()) {
-            $this->next();
-        }
-    }
+abstract class MultiBulkResponseIteratorBase implements \Iterator, \Countable {
+    protected $_position, $_current, $_replySize;
 
     public function rewind() {
         // NOOP
@@ -1218,55 +1197,55 @@ class MultiBulkResponseIterator implements \Iterator, \Countable {
         return $this->_replySize;
     }
 
-    private function getValue() {
-        return \Predis\Response::read($this->_connection);
-    }
+    protected abstract function getValue();
 }
 
-class MultiBulkResponseKVIterator implements \Iterator, \Countable {
-    private $_iterator, $_position, $_current, $_replySize;
+class MultiBulkResponseIterator extends MultiBulkResponseIteratorBase {
+    private $_connection;
 
-    public function __construct(MultiBulkResponseIterator $iterator) {
-        $virtualSize = count($iterator) / 2;
-
-        $this->_iterator   = $iterator;
+    public function __construct($socket, $size) {
+        $this->_connection = $socket;
         $this->_position   = 0;
-        $this->_current    = $virtualSize > 0 ? $this->getValue() : null;
-        $this->_replySize  = $virtualSize;
+        $this->_current    = $size > 0 ? $this->getValue() : null;
+        $this->_replySize  = $size;
     }
 
     public function __destruct() {
-        $this->_iterator->sync();
+        // when the iterator is garbage-collected (e.g. it goes out of the
+        // scope of a foreach) but it has not reached its end, we must sync
+        // the client with the queued elements that have not been read from
+        // the connection with the server.
+        $this->sync();
     }
 
-    public function rewind() {
-        // NOOP
+    public function sync() {
+        while ($this->valid()) {
+            $this->next();
+        }
     }
 
-    public function current() {
-        return $this->_current;
+    protected function getValue() {
+        return \Predis\Response::read($this->_connection);
     }
+}
 
-    public function key() {
-        return $this->_position;
-    }
+class MultiBulkResponseKVIterator extends MultiBulkResponseIteratorBase {
+    private $_iterator;
 
-    public function next() {
-        if (++$this->_position < $this->_replySize) {
-            $this->_current = $this->getValue();
-        }
-        return $this->_position;
-    }
+    public function __construct(MultiBulkResponseIterator $iterator) {
+        $virtualSize = count($iterator) / 2;
 
-    public function valid() {
-        return $this->_position < $this->_replySize;
+        $this->_iterator   = $iterator;
+        $this->_position   = 0;
+        $this->_current    = $virtualSize > 0 ? $this->getValue() : null;
+        $this->_replySize  = $virtualSize;
     }
 
-    public function count() {
-        return $this->_replySize;
+    public function __destruct() {
+        $this->_iterator->sync();
     }
 
-    private function getValue() {
+    protected function getValue() {
         $k = $this->_iterator->current();
         $this->_iterator->next();
         $v = $this->_iterator->current();