Braintree_ResourceCollection

ResourceCollection is a container object for result data.

Defined (1)

The class is defined in the following location(s).

/classes/lib/Braintree/Braintree/ResourceCollection.php  
  1. class ResourceCollection implements Iterator 
  2. private $_index; 
  3. private $_batchIndex; 
  4. private $_items; 
  5. private $_pageSize; 
  6. private $_pager; 
  7.  
  8. /** 
  9. * set up the resource collection 
  10. * expects an array of attributes with literal keys 
  11. * @param array $response 
  12. * @param array $pager 
  13. */ 
  14. public function __construct($response, $pager) 
  15. $this->_pageSize = $response["searchResults"]["pageSize"]; 
  16. $this->_ids = $response["searchResults"]["ids"]; 
  17. $this->_pager = $pager; 
  18.  
  19. /** 
  20. * returns the current item when iterating with foreach 
  21. */ 
  22. public function current() 
  23. return $this->_items[$this->_index]; 
  24.  
  25. /** 
  26. * returns the first item in the collection 
  27. * @return mixed 
  28. */ 
  29. public function firstItem() 
  30. $ids = $this->_ids; 
  31. $page = $this->_getPage([$ids[0]]); 
  32. return $page[0]; 
  33.  
  34. public function key() 
  35. return null; 
  36.  
  37. /** 
  38. * advances to the next item in the collection when iterating with foreach 
  39. */ 
  40. public function next() 
  41. ++$this->_index; 
  42.  
  43. /** 
  44. * rewinds the testIterateOverResults collection to the first item when iterating with foreach 
  45. */ 
  46. public function rewind() 
  47. $this->_batchIndex = 0; 
  48. $this->_getNextPage(); 
  49.  
  50. /** 
  51. * returns whether the current item is valid when iterating with foreach 
  52. */ 
  53. public function valid() 
  54. if ($this->_index == count($this->_items) && $this->_batchIndex < count($this->_ids)) { 
  55. $this->_getNextPage(); 
  56.  
  57. if ($this->_index < count($this->_items)) { 
  58. return true; 
  59. } else { 
  60. return false; 
  61.  
  62. public function maximumCount() 
  63. return count($this->_ids); 
  64.  
  65. private function _getNextPage() 
  66. if (empty($this->_ids)) 
  67. $this->_items = []; 
  68. else 
  69. $this->_items = $this->_getPage(array_slice($this->_ids, $this->_batchIndex, $this->_pageSize)); 
  70. $this->_batchIndex += $this->_pageSize; 
  71. $this->_index = 0; 
  72.  
  73. /** 
  74. * requests the next page of results for the collection 
  75. * @return void 
  76. */ 
  77. private function _getPage($ids) 
  78. $object = $this->_pager['object']; 
  79. $method = $this->_pager['method']; 
  80. $methodArgs = []; 
  81. foreach ($this->_pager['methodArgs'] as $arg) { 
  82. array_push($methodArgs, $arg); 
  83. array_push($methodArgs, $ids); 
  84.  
  85. return call_user_func_array( 
  86. [$object, $method],  
  87. $methodArgs 
  88. );