BraintreeResourceCollection

Braintree ResourceCollection ResourceCollection is a container object for result data.

Defined (1)

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

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