|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: hborras |
|
5
|
|
|
* Date: 2/04/16 |
|
6
|
|
|
* Time: 23:14. |
|
7
|
|
|
*/ |
|
8
|
|
|
namespace Hborras\TwitterAdsSDK\TwitterAds; |
|
9
|
|
|
|
|
10
|
|
|
class Cursor implements \IteratorAggregate |
|
11
|
|
|
{ |
|
12
|
|
|
private $account; |
|
13
|
|
|
/** @var Resource */ |
|
14
|
|
|
private $resource; |
|
15
|
|
|
private $params; |
|
16
|
|
|
private $next_cursor = null; |
|
17
|
|
|
private $current_index = 0; |
|
18
|
|
|
private $total_count = 0; |
|
19
|
|
|
/** @var array */ |
|
20
|
|
|
private $collection; |
|
21
|
|
|
|
|
22
|
|
|
public function __construct(/* @noinspection PhpUnnecessaryFullyQualifiedNameInspection */ |
|
23
|
|
|
\Hborras\TwitterAdsSDK\TwitterAds\Resource $resource, Account $account, $request, $params) |
|
24
|
|
|
{ |
|
25
|
|
|
$this->resource = $resource; |
|
|
|
|
|
|
26
|
|
|
$this->account = $account; |
|
27
|
|
|
$this->params = $params; |
|
28
|
|
|
$this->fromResponse($request); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @return bool |
|
33
|
|
|
*/ |
|
34
|
|
|
public function isExhausted() |
|
35
|
|
|
{ |
|
36
|
|
|
return is_null($this->next_cursor) ? true : false; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @return integer |
|
41
|
|
|
*/ |
|
42
|
|
|
public function count() |
|
43
|
|
|
{ |
|
44
|
|
|
return max($this->total_count, count($this->collection)); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @return Resource |
|
49
|
|
|
*/ |
|
50
|
|
|
public function first() |
|
51
|
|
|
{ |
|
52
|
|
|
return $this->next(); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @return int |
|
57
|
|
|
*/ |
|
58
|
|
|
public function fetched() |
|
59
|
|
|
{ |
|
60
|
|
|
return count($this->collection); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @return Resource | 0 |
|
65
|
|
|
*/ |
|
66
|
|
|
public function next() |
|
67
|
|
|
{ |
|
68
|
|
|
if ($this->current_index < count($this->collection)) { |
|
69
|
|
|
$value = $this->collection[$this->current_index]; |
|
70
|
|
|
++$this->current_index; |
|
71
|
|
|
|
|
72
|
|
|
return $value; |
|
73
|
|
|
} elseif (!is_null($this->next_cursor)) { |
|
74
|
|
|
$this->fetchNext(); |
|
75
|
|
|
|
|
76
|
|
|
return $this->next(); |
|
77
|
|
|
} else { |
|
78
|
|
|
$this->current_index = 0; |
|
79
|
|
|
|
|
80
|
|
|
return 0; |
|
|
|
|
|
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param array $params |
|
86
|
|
|
* @return Cursor |
|
87
|
|
|
*/ |
|
88
|
|
|
public function fetchNext($params = []) |
|
89
|
|
|
{ |
|
90
|
|
|
$params['cursor'] = $this->next_cursor; |
|
91
|
|
|
switch ($this->account->getTwitterAds()->getMethod()) { |
|
92
|
|
|
case 'GET': |
|
93
|
|
|
$response = $this->account->getTwitterAds()->get($this->account->getTwitterAds()->getResource(), $params); |
|
94
|
|
|
break; |
|
95
|
|
|
case 'POST': |
|
96
|
|
|
$response = $this->account->getTwitterAds()->post($this->account->getTwitterAds()->getResource(), $params); |
|
97
|
|
|
break; |
|
98
|
|
|
case 'PUT': |
|
99
|
|
|
$response = $this->account->getTwitterAds()->put($this->account->getTwitterAds()->getResource(), $params); |
|
100
|
|
|
break; |
|
101
|
|
|
case 'DELETE': |
|
102
|
|
|
$response = $this->account->getTwitterAds()->delete($this->account->getTwitterAds()->getResource()); |
|
103
|
|
|
break; |
|
104
|
|
|
default: |
|
105
|
|
|
$response = $this->account->getTwitterAds()->get($this->account->getTwitterAds()->getResource(), $params); |
|
106
|
|
|
break; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
return $this->fromResponse($response->getBody()); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @param $request |
|
114
|
|
|
* @return $this |
|
115
|
|
|
*/ |
|
116
|
|
|
public function fromResponse($request) |
|
117
|
|
|
{ |
|
118
|
|
|
$this->next_cursor = isset($request->next_cursor) ? $request->next_cursor : null; |
|
119
|
|
|
if (isset($request->total_count)) { |
|
120
|
|
|
$this->total_count = intval($request->total_count); |
|
121
|
|
|
} |
|
122
|
|
|
foreach ($request->data as $item) { |
|
123
|
|
|
if (method_exists($this->resource, 'fromResponse')) { |
|
124
|
|
|
if ($this->resource instanceof Account) { |
|
125
|
|
|
$obj = new $this->resource($this->account->getTwitterAds()); |
|
126
|
|
|
} else { |
|
127
|
|
|
$obj = new $this->resource(); |
|
128
|
|
|
} |
|
129
|
|
|
$obj->setAccount($this->account); |
|
130
|
|
|
$this->collection[] = $obj->fromResponse($item); |
|
131
|
|
|
} else { |
|
132
|
|
|
$this->collection[] = $item; |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
return $this; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* {@inheritdoc} |
|
141
|
|
|
*/ |
|
142
|
|
|
public function getIterator() |
|
143
|
|
|
{ |
|
144
|
|
|
return new \ArrayIterator($this->collection); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @return array |
|
149
|
|
|
*/ |
|
150
|
|
|
public function getCollection() |
|
151
|
|
|
{ |
|
152
|
|
|
return $this->collection; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @param array $collection |
|
157
|
|
|
*/ |
|
158
|
|
|
public function setCollection($collection) |
|
159
|
|
|
{ |
|
160
|
|
|
$this->collection = $collection; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* @return Account |
|
165
|
|
|
*/ |
|
166
|
|
|
public function getAccount() |
|
167
|
|
|
{ |
|
168
|
|
|
return $this->account; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* @param Account $account |
|
173
|
|
|
*/ |
|
174
|
|
|
public function setAccount($account) |
|
175
|
|
|
{ |
|
176
|
|
|
$this->account = $account; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* @return mixed |
|
181
|
|
|
*/ |
|
182
|
|
|
public function getParams() |
|
183
|
|
|
{ |
|
184
|
|
|
return $this->params; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* @param mixed $params |
|
189
|
|
|
*/ |
|
190
|
|
|
public function setParams($params) |
|
191
|
|
|
{ |
|
192
|
|
|
$this->params = $params; |
|
193
|
|
|
} |
|
194
|
|
|
} |
|
195
|
|
|
|
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..