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