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
|
6 |
|
public function __construct(Collection $collection, array $params) |
40
|
|
|
{ |
41
|
6 |
|
$this->page = $collection; |
42
|
6 |
|
$this->params = $params; |
43
|
6 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Make a AutoPagingIterator object. |
47
|
|
|
* |
48
|
|
|
* @param \Arcanedev\Stripe\Collection $collection |
49
|
|
|
* @param array $params |
50
|
|
|
* |
51
|
|
|
* @return self |
52
|
|
|
*/ |
53
|
6 |
|
public static function make(Collection $collection, array $params) |
54
|
|
|
{ |
55
|
6 |
|
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
|
6 |
|
public function current() |
69
|
|
|
{ |
70
|
6 |
|
$item = current($this->page->data); |
71
|
6 |
|
$this->lastId = $item !== false ? $item['id'] : null; |
72
|
|
|
|
73
|
6 |
|
return $item; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Move forward to next element. |
78
|
|
|
* @link http://php.net/manual/en/iterator.next.php |
79
|
|
|
*/ |
80
|
6 |
|
public function next() |
81
|
|
|
{ |
82
|
6 |
|
$item = next($this->page->data); |
83
|
|
|
|
84
|
6 |
|
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
|
6 |
|
$this->pageOffset += count($this->page->data); |
88
|
|
|
|
89
|
6 |
|
if ($this->page['has_more']) { |
90
|
4 |
|
$this->params = array_merge( |
91
|
4 |
|
$this->params ? $this->params : [], |
92
|
4 |
|
['starting_after' => $this->lastId] |
93
|
|
|
); |
94
|
|
|
|
95
|
4 |
|
$this->page = $this->page->all($this->params); |
|
|
|
|
96
|
|
|
} |
97
|
6 |
|
else return; |
98
|
|
|
} |
99
|
6 |
|
} |
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
|
2 |
|
public function key() |
108
|
|
|
{ |
109
|
2 |
|
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
|
6 |
|
public function valid() |
119
|
|
|
{ |
120
|
6 |
|
$key = key($this->page->data); |
121
|
|
|
|
122
|
6 |
|
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
|
6 |
|
public function rewind() |
130
|
|
|
{ |
131
|
|
|
// Actually rewinding would require making a copy of the original page. |
132
|
6 |
|
} |
133
|
|
|
} |
134
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.