|
1
|
|
|
<?php namespace Arcanedev\Stripe\Utilities; |
|
2
|
|
|
|
|
3
|
|
|
use Arcanedev\Stripe\Collection; |
|
4
|
|
|
use Iterator; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Class AutoPagingIterator |
|
8
|
|
|
* |
|
9
|
|
|
* @package Arcanedev\Stripe\Utilities |
|
10
|
|
|
* @author ARCANEDEV <[email protected]> |
|
11
|
|
|
*/ |
|
12
|
|
|
class AutoPagingIterator implements Iterator |
|
13
|
|
|
{ |
|
14
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
15
|
|
|
| Properties |
|
16
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
17
|
|
|
*/ |
|
18
|
|
|
private $lastId = null; |
|
19
|
|
|
|
|
20
|
|
|
/** @var \Arcanedev\Stripe\Collection */ |
|
21
|
|
|
private $page = null; |
|
22
|
|
|
|
|
23
|
|
|
/** @var int */ |
|
24
|
|
|
private $pageOffset = 0; |
|
25
|
|
|
|
|
26
|
|
|
/** @var array */ |
|
27
|
|
|
private $params = []; |
|
28
|
|
|
|
|
29
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
30
|
|
|
| Constructor |
|
31
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
32
|
|
|
*/ |
|
33
|
|
|
/** |
|
34
|
|
|
* AutoPagingIterator constructor. |
|
35
|
|
|
* |
|
36
|
|
|
* @param \Arcanedev\Stripe\Collection $collection |
|
37
|
|
|
* @param array $params |
|
38
|
|
|
*/ |
|
39
|
15 |
|
public function __construct(Collection $collection, array $params) |
|
40
|
|
|
{ |
|
41
|
15 |
|
$this->page = $collection; |
|
42
|
15 |
|
$this->params = $params; |
|
43
|
15 |
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Make a AutoPagingIterator object. |
|
47
|
|
|
* |
|
48
|
|
|
* @param \Arcanedev\Stripe\Collection $collection |
|
49
|
|
|
* @param array $params |
|
50
|
|
|
* |
|
51
|
|
|
* @return self |
|
52
|
|
|
*/ |
|
53
|
15 |
|
public static function make(Collection $collection, array $params) |
|
54
|
|
|
{ |
|
55
|
15 |
|
return new self($collection, $params); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
59
|
|
|
| Main Functions |
|
60
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
61
|
|
|
*/ |
|
62
|
|
|
/** |
|
63
|
|
|
* Return the current element. |
|
64
|
|
|
* @link http://php.net/manual/en/iterator.current.php |
|
65
|
|
|
* |
|
66
|
|
|
* @return mixed |
|
67
|
|
|
*/ |
|
68
|
15 |
|
public function current() |
|
69
|
|
|
{ |
|
70
|
15 |
|
$item = current($this->page->data); |
|
71
|
15 |
|
$this->lastId = $item !== false ? $item['id'] : null; |
|
72
|
|
|
|
|
73
|
15 |
|
return $item; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Move forward to next element. |
|
78
|
|
|
* @link http://php.net/manual/en/iterator.next.php |
|
79
|
|
|
*/ |
|
80
|
15 |
|
public function next() |
|
81
|
|
|
{ |
|
82
|
15 |
|
$item = next($this->page->data); |
|
83
|
|
|
|
|
84
|
15 |
|
if ($item === false) { |
|
85
|
|
|
// If we've run out of data on the current page, try to fetch another one |
|
86
|
|
|
// and increase the offset the new page would start at |
|
87
|
15 |
|
$this->pageOffset += count($this->page->data); |
|
88
|
|
|
|
|
89
|
15 |
|
if ($this->page['has_more']) { |
|
90
|
10 |
|
$this->params = array_merge( |
|
91
|
10 |
|
$this->params ? $this->params : [], |
|
92
|
10 |
|
['starting_after' => $this->lastId] |
|
93
|
8 |
|
); |
|
94
|
|
|
|
|
95
|
10 |
|
$this->page = $this->page->all($this->params); |
|
|
|
|
|
|
96
|
8 |
|
} |
|
97
|
15 |
|
else return; |
|
98
|
8 |
|
} |
|
99
|
15 |
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Return the key of the current element. |
|
103
|
|
|
* @link http://php.net/manual/en/iterator.key.php |
|
104
|
|
|
* |
|
105
|
|
|
* @return mixed |
|
106
|
|
|
*/ |
|
107
|
5 |
|
public function key() |
|
108
|
|
|
{ |
|
109
|
5 |
|
return key($this->page->data) + $this->pageOffset; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Checks if current position is valid. |
|
114
|
|
|
* @link http://php.net/manual/en/iterator.valid.php |
|
115
|
|
|
* |
|
116
|
|
|
* @return bool |
|
117
|
|
|
*/ |
|
118
|
15 |
|
public function valid() |
|
119
|
|
|
{ |
|
120
|
15 |
|
$key = key($this->page->data); |
|
121
|
|
|
|
|
122
|
15 |
|
return ($key !== null && $key !== false); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Rewind the Iterator to the first element. |
|
127
|
|
|
* @link http://php.net/manual/en/iterator.rewind.php |
|
128
|
|
|
*/ |
|
129
|
15 |
|
public function rewind() |
|
130
|
|
|
{ |
|
131
|
|
|
// Actually rewinding would require making a copy of the original page. |
|
132
|
15 |
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..