MultiBulkResponse.php 1.9 KB

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