MultiBulkResponseTuple.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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
  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 = 0;
  28. $this->current = $virtualSize > 0 ? $this->getValue() : null;
  29. $this->replySize = $virtualSize;
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function __destruct()
  35. {
  36. $this->iterator->sync();
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. protected function getValue()
  42. {
  43. $k = $this->iterator->current();
  44. $this->iterator->next();
  45. $v = $this->iterator->current();
  46. $this->iterator->next();
  47. return array($k, $v);
  48. }
  49. }