ResourceCollection.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. namespace Braintree;
  3. use Iterator;
  4. /**
  5. * Braintree ResourceCollection
  6. * ResourceCollection is a container object for result data
  7. *
  8. * stores and retrieves search results and aggregate data
  9. *
  10. * example:
  11. * <code>
  12. * $result = Customer::all();
  13. *
  14. * foreach($result as $transaction) {
  15. * print_r($transaction->id);
  16. * }
  17. * </code>
  18. *
  19. * @package Braintree
  20. * @subpackage Utility
  21. */
  22. class ResourceCollection implements Iterator
  23. {
  24. private $_batchIndex;
  25. private $_ids;
  26. private $_index;
  27. private $_items;
  28. private $_pageSize;
  29. private $_pager;
  30. /**
  31. * set up the resource collection
  32. *
  33. * expects an array of attributes with literal keys
  34. *
  35. * @param array $response
  36. * @param array $pager
  37. */
  38. public function __construct($response, $pager)
  39. {
  40. $this->_pageSize = $response["searchResults"]["pageSize"];
  41. $this->_ids = $response["searchResults"]["ids"];
  42. $this->_pager = $pager;
  43. }
  44. /**
  45. * returns the current item when iterating with foreach
  46. */
  47. public function current()
  48. {
  49. return $this->_items[$this->_index];
  50. }
  51. /**
  52. * returns the first item in the collection
  53. *
  54. * @return mixed
  55. */
  56. public function firstItem()
  57. {
  58. $ids = $this->_ids;
  59. $page = $this->_getPage([$ids[0]]);
  60. return $page[0];
  61. }
  62. public function key()
  63. {
  64. return null;
  65. }
  66. /**
  67. * advances to the next item in the collection when iterating with foreach
  68. */
  69. public function next()
  70. {
  71. ++$this->_index;
  72. }
  73. /**
  74. * rewinds the testIterateOverResults collection to the first item when iterating with foreach
  75. */
  76. public function rewind()
  77. {
  78. $this->_batchIndex = 0;
  79. $this->_getNextPage();
  80. }
  81. /**
  82. * returns whether the current item is valid when iterating with foreach
  83. */
  84. public function valid()
  85. {
  86. if ($this->_index == count($this->_items) && $this->_batchIndex < count($this->_ids)) {
  87. $this->_getNextPage();
  88. }
  89. if ($this->_index < count($this->_items)) {
  90. return true;
  91. } else {
  92. return false;
  93. }
  94. }
  95. public function maximumCount()
  96. {
  97. return count($this->_ids);
  98. }
  99. private function _getNextPage()
  100. {
  101. if (empty($this->_ids))
  102. {
  103. $this->_items = [];
  104. }
  105. else
  106. {
  107. $this->_items = $this->_getPage(array_slice($this->_ids, $this->_batchIndex, $this->_pageSize));
  108. $this->_batchIndex += $this->_pageSize;
  109. $this->_index = 0;
  110. }
  111. }
  112. /**
  113. * requests the next page of results for the collection
  114. *
  115. * @return void
  116. */
  117. private function _getPage($ids)
  118. {
  119. $object = $this->_pager['object'];
  120. $method = $this->_pager['method'];
  121. $methodArgs = [];
  122. foreach ($this->_pager['methodArgs'] as $arg) {
  123. array_push($methodArgs, $arg);
  124. }
  125. array_push($methodArgs, $ids);
  126. return call_user_func_array(
  127. [$object, $method],
  128. $methodArgs
  129. );
  130. }
  131. /**
  132. * returns all IDs in the collection
  133. *
  134. * @return array
  135. */
  136. public function getIds()
  137. {
  138. return $this->_ids;
  139. }
  140. }
  141. class_alias('Braintree\ResourceCollection', 'Braintree_ResourceCollection');