PaginatedCollection.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace Braintree;
  3. use Iterator;
  4. /**
  5. * Braintree PaginatedCollection
  6. * PaginatedCollection is a container object for paginated data
  7. *
  8. * retrieves and pages through large collections of results
  9. *
  10. * example:
  11. * <code>
  12. * $result = MerchantAccount::all();
  13. *
  14. * foreach($result as $merchantAccount) {
  15. * print_r($merchantAccount->status);
  16. * }
  17. * </code>
  18. *
  19. * @package Braintree
  20. * @subpackage Utility
  21. */
  22. class PaginatedCollection implements Iterator
  23. {
  24. private $_pager;
  25. private $_pageSize;
  26. private $_currentPage;
  27. private $_index;
  28. private $_totalItems;
  29. private $_items;
  30. /**
  31. * set up the paginated collection
  32. *
  33. * expects an array of an object and method to call on it
  34. *
  35. * @param array $pager
  36. */
  37. public function __construct($pager)
  38. {
  39. $this->_pager = $pager;
  40. $this->_pageSize = 0;
  41. $this->_currentPage = 0;
  42. $this->_totalItems = 0;
  43. $this->_index = 0;
  44. }
  45. /**
  46. * returns the current item when iterating with foreach
  47. */
  48. public function current()
  49. {
  50. return $this->_items[($this->_index % $this->_pageSize)];
  51. }
  52. public function key()
  53. {
  54. return null;
  55. }
  56. /**
  57. * advances to the next item in the collection when iterating with foreach
  58. */
  59. public function next()
  60. {
  61. ++$this->_index;
  62. }
  63. /**
  64. * rewinds the collection to the first item when iterating with foreach
  65. */
  66. public function rewind()
  67. {
  68. $this->_index = 0;
  69. $this->_currentPage = 0;
  70. $this->_pageSize = 0;
  71. $this->_totalItems = 0;
  72. $this->_items = [];
  73. }
  74. /**
  75. * returns whether the current item is valid when iterating with foreach
  76. */
  77. public function valid()
  78. {
  79. if ($this->_currentPage == 0 || $this->_index % $this->_pageSize == 0 && $this->_index < $this->_totalItems)
  80. {
  81. $this->_getNextPage();
  82. }
  83. return $this->_index < $this->_totalItems;
  84. }
  85. private function _getNextPage()
  86. {
  87. $this->_currentPage++;
  88. $object = $this->_pager['object'];
  89. $method = $this->_pager['method'];
  90. if (isset($this->_pager['query'])) {
  91. $query = $this->_pager['query'];
  92. $result = call_user_func(
  93. [$object, $method],
  94. $query,
  95. $this->_currentPage
  96. );
  97. } else {
  98. $result = call_user_func(
  99. [$object, $method],
  100. $this->_currentPage
  101. );
  102. }
  103. $this->_totalItems= $result->getTotalItems();
  104. $this->_pageSize = $result->getPageSize();
  105. $this->_items = $result->getCurrentPage();
  106. }
  107. }
  108. class_alias('Braintree\PaginatedCollection', 'Braintree_PaginatedCollection');