Stream.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace Twilio;
  3. class Stream implements \Iterator {
  4. public $page;
  5. public $firstPage;
  6. public $limit;
  7. public $currentRecord;
  8. public $pageLimit;
  9. public $currentPage;
  10. function __construct(Page $page, $limit, $pageLimit) {
  11. $this->page = $page;
  12. $this->firstPage = $page;
  13. $this->limit = $limit;
  14. $this->currentRecord = 1;
  15. $this->pageLimit = $pageLimit;
  16. $this->currentPage = 1;
  17. }
  18. /**
  19. * (PHP 5 &gt;= 5.0.0)<br/>
  20. * Return the current element
  21. * @link http://php.net/manual/en/iterator.current.php
  22. * @return mixed Can return any type.
  23. */
  24. public function current() {
  25. return $this->page->current();
  26. }
  27. /**
  28. * (PHP 5 &gt;= 5.0.0)<br/>
  29. * Move forward to next element
  30. * @link http://php.net/manual/en/iterator.next.php
  31. * @return void Any returned value is ignored.
  32. */
  33. public function next() {
  34. $this->page->next();
  35. $this->currentRecord++;
  36. if ($this->overLimit()) {
  37. return;
  38. }
  39. if (!$this->page->valid()) {
  40. if ($this->overPageLimit()) {
  41. return;
  42. }
  43. $this->page = $this->page->nextPage();
  44. $this->currentPage++;
  45. }
  46. }
  47. /**
  48. * (PHP 5 &gt;= 5.0.0)<br/>
  49. * Return the key of the current element
  50. * @link http://php.net/manual/en/iterator.key.php
  51. * @return mixed scalar on success, or null on failure.
  52. */
  53. public function key() {
  54. return $this->currentRecord;
  55. }
  56. /**
  57. * (PHP 5 &gt;= 5.0.0)<br/>
  58. * Checks if current position is valid
  59. * @link http://php.net/manual/en/iterator.valid.php
  60. * @return boolean The return value will be casted to boolean and then evaluated.
  61. * Returns true on success or false on failure.
  62. */
  63. public function valid() {
  64. return $this->page && $this->page->valid() && !$this->overLimit() && !$this->overPageLimit();
  65. }
  66. /**
  67. * (PHP 5 &gt;= 5.0.0)<br/>
  68. * Rewind the Iterator to the first element
  69. * @link http://php.net/manual/en/iterator.rewind.php
  70. * @return void Any returned value is ignored.
  71. */
  72. public function rewind() {
  73. $this->page = $this->firstPage;
  74. $this->page->rewind();
  75. $this->currentPage = 1;
  76. $this->currentRecord = 1;
  77. }
  78. protected function overLimit() {
  79. return ($this->limit !== null
  80. && $this->limit !== Values::NONE
  81. && $this->limit < $this->currentRecord);
  82. }
  83. protected function overPageLimit() {
  84. return ($this->pageLimit !== null
  85. && $this->pageLimit !== Values::NONE
  86. && $this->pageLimit < $this->currentPage);
  87. }
  88. }