MultiBulkResponse.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. * {@inheritdoc}
  75. */
  76. protected abstract function getValue();
  77. }