MultiBulkResponse.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. * Iterator that abstracts the access to multibulk replies and allows
  13. * them to be consumed by user's code in a streaming fashion.
  14. *
  15. * @author Daniele Alessandri <suppakilla@gmail.com>
  16. */
  17. abstract class MultiBulkResponse implements \Iterator, \Countable
  18. {
  19. protected $position;
  20. protected $current;
  21. protected $replySize;
  22. /**
  23. * {@inheritdoc}
  24. */
  25. public function rewind()
  26. {
  27. // NOOP
  28. }
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public function current()
  33. {
  34. return $this->current;
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function key()
  40. {
  41. return $this->position;
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function next()
  47. {
  48. if (++$this->position < $this->replySize) {
  49. $this->current = $this->getValue();
  50. }
  51. return $this->position;
  52. }
  53. /**
  54. * {@inheritdoc}
  55. */
  56. public function valid()
  57. {
  58. return $this->position < $this->replySize;
  59. }
  60. /**
  61. * Returns the number of items of the whole multibulk reply.
  62. *
  63. * This method should be used to get the size of the current multibulk
  64. * reply without using iterator_count, which actually consumes the
  65. * iterator to calculate the size (rewinding is not supported).
  66. *
  67. * @return int
  68. */
  69. public function count()
  70. {
  71. return $this->replySize;
  72. }
  73. /**
  74. * Returns the current position of the iterator.
  75. *
  76. * @return int
  77. */
  78. public function getPosition()
  79. {
  80. return $this->position;
  81. }
  82. /**
  83. * {@inheritdoc}
  84. */
  85. protected abstract function getValue();
  86. }