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