1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- <?php
- namespace Predis\Iterator;
- class MultiBulkResponseTuple extends MultiBulkResponse implements \OuterIterator
- {
- private $iterator;
-
- public function __construct(MultiBulkResponseSimple $iterator)
- {
- $this->checkPreconditions($iterator);
- $virtualSize = count($iterator) / 2;
- $this->iterator = $iterator;
- $this->position = $iterator->getPosition();
- $this->current = $virtualSize > 0 ? $this->getValue() : null;
- $this->replySize = $virtualSize;
- }
-
- protected function checkPreconditions(MultiBulkResponseSimple $iterator)
- {
- if ($iterator->getPosition() !== 0) {
- throw new \RuntimeException('Cannot initialize a tuple iterator with an already initiated iterator');
- }
- if (($size = count($iterator)) % 2 !== 0) {
- throw new \UnexpectedValueException("Invalid reply size for a tuple iterator [$size]");
- }
- }
-
- public function getInnerIterator()
- {
- return $this->iterator;
- }
-
- public function __destruct()
- {
- $this->iterator->sync(true);
- }
-
- protected function getValue()
- {
- $k = $this->iterator->current();
- $this->iterator->next();
- $v = $this->iterator->current();
- $this->iterator->next();
- return array($k, $v);
- }
- }
|