MultiBulkResponseTuple.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. /*
  3. * This file is part of the Predis package.
  4. *
  5. * (c) Daniele Alessandri <suppakilla@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Predis\Iterators;
  11. /**
  12. * Abstracts the access to a streamable list of tuples represented
  13. * as a multibulk reply that alternates keys and values.
  14. *
  15. * @author Daniele Alessandri <suppakilla@gmail.com>
  16. */
  17. class MultiBulkResponseTuple extends MultiBulkResponse implements \OuterIterator
  18. {
  19. private $iterator;
  20. /**
  21. * @param MultiBulkResponseSimple $iterator Multibulk reply iterator.
  22. */
  23. public function __construct(MultiBulkResponseSimple $iterator)
  24. {
  25. $virtualSize = count($iterator) / 2;
  26. $this->iterator = $iterator;
  27. $this->position = $iterator->getPosition();
  28. $this->current = $virtualSize > 0 ? $this->getValue() : null;
  29. $this->replySize = $virtualSize;
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function getInnerIterator()
  35. {
  36. return $this->iterator;
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function __destruct()
  42. {
  43. $this->iterator->sync(true);
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. protected function getValue()
  49. {
  50. $k = $this->iterator->current();
  51. $this->iterator->next();
  52. $v = $this->iterator->current();
  53. $this->iterator->next();
  54. return array($k, $v);
  55. }
  56. }