|
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
|
|
|
use Hborras\TwitterAdsSDK\TwitterAds; |
|
11
|
|
|
|
|
12
|
|
|
class Cursor implements \Iterator, \Countable, \arrayaccess |
|
13
|
|
|
{ |
|
14
|
|
|
/** @var Resource */ |
|
15
|
|
|
private $resource; |
|
16
|
|
|
private $params; |
|
17
|
|
|
private $next_cursor = null; |
|
18
|
|
|
private $current_index = 0; |
|
19
|
|
|
private $total_count = 0; |
|
20
|
|
|
/** @var array */ |
|
21
|
|
|
private $collection; |
|
22
|
|
|
/** @var TwitterAds */ |
|
23
|
|
|
private $twitterAds; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var int|null |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $indexLeft; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var int|null |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $indexRight; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var bool |
|
37
|
|
|
*/ |
|
38
|
|
|
protected static $defaultUseImplicitFetch = false; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var bool |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $useImplicitFetch; |
|
44
|
|
|
|
|
45
|
|
|
public function __construct(/* @noinspection PhpUnnecessaryFullyQualifiedNameInspection */ |
|
46
|
|
|
\Hborras\TwitterAdsSDK\TwitterAds\Resource $resource, TwitterAds $twitterAds, $request, $params) |
|
47
|
|
|
{ |
|
48
|
|
|
$this->resource = $resource; |
|
|
|
|
|
|
49
|
|
|
$this->params = $params; |
|
50
|
|
|
$this->twitterAds = $twitterAds; |
|
51
|
|
|
$this->fromResponse($request); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @return bool |
|
56
|
|
|
*/ |
|
57
|
|
|
public function isExhausted() |
|
58
|
|
|
{ |
|
59
|
|
|
return is_null($this->next_cursor) ? true : false; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @return integer |
|
64
|
|
|
*/ |
|
65
|
|
|
public function count() |
|
66
|
|
|
{ |
|
67
|
|
|
return max($this->total_count, count($this->collection)); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @return int |
|
72
|
|
|
*/ |
|
73
|
|
|
public function fetched() |
|
74
|
|
|
{ |
|
75
|
|
|
return count($this->collection); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @return Resource | 0 |
|
80
|
|
|
*/ |
|
81
|
|
|
public function next() |
|
82
|
|
|
{ |
|
83
|
|
|
if ($this->current_index == $this->getIndexRight() && !is_null($this->next_cursor)) { |
|
84
|
|
|
if ($this->getUseImplicitFetch()) { |
|
85
|
|
|
$this->fetchNext(); |
|
86
|
|
|
if ($this->current_index == $this->getIndexRight()) { |
|
87
|
|
|
$this->current_index = null; |
|
88
|
|
|
} else { |
|
89
|
|
|
++$this->current_index; |
|
90
|
|
|
} |
|
91
|
|
|
} else { |
|
92
|
|
|
$this->current_index = null; |
|
93
|
|
|
} |
|
94
|
|
|
} else { |
|
95
|
|
|
++$this->current_index; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @param array $params |
|
101
|
|
|
* @return Cursor |
|
102
|
|
|
*/ |
|
103
|
|
|
public function fetchNext($params = []) |
|
104
|
|
|
{ |
|
105
|
|
|
$requestParams = $this->params; |
|
106
|
|
|
if (count($params) > 0) { |
|
107
|
|
|
foreach ($params as $key => $value) { |
|
108
|
|
|
$requestParams[$key] = $value; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
$requestParams['cursor'] = $this->next_cursor; |
|
112
|
|
|
switch ($this->getTwitterAds()->getMethod()) { |
|
113
|
|
|
case 'GET': |
|
114
|
|
|
$response = $this->getTwitterAds()->get($this->getTwitterAds()->getResource(), $requestParams); |
|
115
|
|
|
break; |
|
116
|
|
|
case 'POST': |
|
117
|
|
|
$response = $this->getTwitterAds()->post($this->getTwitterAds()->getResource(), $params); |
|
118
|
|
|
break; |
|
119
|
|
|
case 'PUT': |
|
120
|
|
|
$response = $this->getTwitterAds()->put($this->getTwitterAds()->getResource(), $params); |
|
121
|
|
|
break; |
|
122
|
|
|
case 'DELETE': |
|
123
|
|
|
$response = $this->getTwitterAds()->delete($this->getTwitterAds()->getResource()); |
|
124
|
|
|
break; |
|
125
|
|
|
default: |
|
126
|
|
|
$response = $this->getTwitterAds()->get($this->getTwitterAds()->getResource(), $params); |
|
127
|
|
|
break; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
return $this->fromResponse($response->getBody()); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @param $request |
|
135
|
|
|
* @return $this |
|
136
|
|
|
*/ |
|
137
|
|
|
public function fromResponse($request) |
|
138
|
|
|
{ |
|
139
|
|
|
$this->next_cursor = isset($request->next_cursor) ? $request->next_cursor : null; |
|
140
|
|
|
if (isset($request->total_count)) { |
|
141
|
|
|
$this->total_count = intval($request->total_count); |
|
142
|
|
|
} |
|
143
|
|
|
foreach ($request->data as $item) { |
|
144
|
|
|
if (method_exists($this->resource, 'fromResponse')) { |
|
145
|
|
|
/** @var \Hborras\TwitterAdsSDK\TwitterAds\Resource $obj */ |
|
146
|
|
|
$obj = new $this->resource(); |
|
147
|
|
|
$this->collection[] = $obj->fromResponse($item); |
|
148
|
|
|
} else { |
|
149
|
|
|
$this->collection[] = $item; |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
if ($this->indexRight === null) { |
|
154
|
|
|
$this->indexLeft = 0; |
|
155
|
|
|
$this->indexRight = -1; |
|
156
|
|
|
$this->current_index = 0; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
$this->indexRight += count($request->data); |
|
160
|
|
|
|
|
161
|
|
|
return $this; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* {@inheritdoc} |
|
166
|
|
|
*/ |
|
167
|
|
|
public function getIterator() |
|
168
|
|
|
{ |
|
169
|
|
|
return new \ArrayIterator($this->collection); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* @return array |
|
174
|
|
|
*/ |
|
175
|
|
|
public function getCollection() |
|
176
|
|
|
{ |
|
177
|
|
|
return $this->collection; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* @param array $collection |
|
182
|
|
|
*/ |
|
183
|
|
|
public function setCollection($collection) |
|
184
|
|
|
{ |
|
185
|
|
|
$this->collection = $collection; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* @return TwitterAds |
|
190
|
|
|
*/ |
|
191
|
|
|
public function getTwitterAds() |
|
192
|
|
|
{ |
|
193
|
|
|
return $this->twitterAds; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* @param TwitterAds $twitterAds |
|
198
|
|
|
*/ |
|
199
|
|
|
public function setTwitterAds($twitterAds) |
|
200
|
|
|
{ |
|
201
|
|
|
$this->twitterAds = $twitterAds; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* Return the current element |
|
206
|
|
|
* @link http://php.net/manual/en/iterator.current.php |
|
207
|
|
|
* @return mixed Can return any type. |
|
208
|
|
|
* @since 5.0.0 |
|
209
|
|
|
*/ |
|
210
|
|
|
public function current() |
|
211
|
|
|
{ |
|
212
|
|
|
return isset($this->collection[$this->current_index]) |
|
213
|
|
|
? $this->collection[$this->current_index] |
|
214
|
|
|
: false; |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* Return the key of the current element |
|
219
|
|
|
* @link http://php.net/manual/en/iterator.key.php |
|
220
|
|
|
* @return mixed scalar on success, or null on failure. |
|
221
|
|
|
* @since 5.0.0 |
|
222
|
|
|
*/ |
|
223
|
|
|
public function key() |
|
224
|
|
|
{ |
|
225
|
|
|
return $this->current_index; |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* Checks if current position is valid |
|
230
|
|
|
* @link http://php.net/manual/en/iterator.valid.php |
|
231
|
|
|
* @return boolean The return value will be casted to boolean and then evaluated. |
|
232
|
|
|
* Returns true on success or false on failure. |
|
233
|
|
|
* @since 5.0.0 |
|
234
|
|
|
*/ |
|
235
|
|
|
public function valid() |
|
236
|
|
|
{ |
|
237
|
|
|
return isset($this->collection[$this->current_index]); |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
/** |
|
241
|
|
|
* Rewind the Iterator to the first element |
|
242
|
|
|
* @link http://php.net/manual/en/iterator.rewind.php |
|
243
|
|
|
* @return void Any returned value is ignored. |
|
244
|
|
|
* @since 5.0.0 |
|
245
|
|
|
*/ |
|
246
|
|
|
public function rewind() |
|
247
|
|
|
{ |
|
248
|
|
|
$this->current_index = $this->current_index--; |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
/** |
|
252
|
|
|
* Whether a offset exists |
|
253
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetexists.php |
|
254
|
|
|
* @param mixed $offset <p> |
|
255
|
|
|
* An offset to check for. |
|
256
|
|
|
* </p> |
|
257
|
|
|
* @return boolean true on success or false on failure. |
|
258
|
|
|
* </p> |
|
259
|
|
|
* <p> |
|
260
|
|
|
* The return value will be casted to boolean if non-boolean was returned. |
|
261
|
|
|
* @since 5.0.0 |
|
262
|
|
|
*/ |
|
263
|
|
|
public function offsetExists($offset) |
|
264
|
|
|
{ |
|
265
|
|
|
return isset($this->collection[$offset]); |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
/** |
|
269
|
|
|
* Offset to retrieve |
|
270
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetget.php |
|
271
|
|
|
* @param mixed $offset <p> |
|
272
|
|
|
* The offset to retrieve. |
|
273
|
|
|
* </p> |
|
274
|
|
|
* @return mixed Can return all value types. |
|
275
|
|
|
* @since 5.0.0 |
|
276
|
|
|
*/ |
|
277
|
|
|
public function offsetGet($offset) |
|
278
|
|
|
{ |
|
279
|
|
|
return isset($this->collection[$offset]) ? $this->collection[$offset] : null; |
|
280
|
|
|
} |
|
281
|
|
|
|
|
282
|
|
|
/** |
|
283
|
|
|
* Offset to set |
|
284
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetset.php |
|
285
|
|
|
* @param mixed $offset <p> |
|
286
|
|
|
* The offset to assign the value to. |
|
287
|
|
|
* </p> |
|
288
|
|
|
* @param mixed $value <p> |
|
289
|
|
|
* The value to set. |
|
290
|
|
|
* </p> |
|
291
|
|
|
* @return void |
|
292
|
|
|
* @since 5.0.0 |
|
293
|
|
|
*/ |
|
294
|
|
|
public function offsetSet($offset, $value) |
|
295
|
|
|
{ |
|
296
|
|
|
if ($offset === null) { |
|
297
|
|
|
$this->collection[] = $value; |
|
298
|
|
|
} else { |
|
299
|
|
|
$this->collection[$offset] = $value; |
|
300
|
|
|
} |
|
301
|
|
|
} |
|
302
|
|
|
|
|
303
|
|
|
/** |
|
304
|
|
|
* Offset to unset |
|
305
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetunset.php |
|
306
|
|
|
* @param mixed $offset <p> |
|
307
|
|
|
* The offset to unset. |
|
308
|
|
|
* </p> |
|
309
|
|
|
* @return void |
|
310
|
|
|
* @since 5.0.0 |
|
311
|
|
|
*/ |
|
312
|
|
|
public function offsetUnset($offset) |
|
313
|
|
|
{ |
|
314
|
|
|
unset($this->collection[$offset]); |
|
315
|
|
|
} |
|
316
|
|
|
|
|
317
|
|
|
/** |
|
318
|
|
|
* @return bool |
|
319
|
|
|
*/ |
|
320
|
|
|
public static function getDefaultUseImplicitFetch() |
|
321
|
|
|
{ |
|
322
|
|
|
return static::$defaultUseImplicitFetch; |
|
323
|
|
|
} |
|
324
|
|
|
|
|
325
|
|
|
/** |
|
326
|
|
|
* @param bool $use_implicit_fetch |
|
327
|
|
|
*/ |
|
328
|
|
|
public static function setDefaultUseImplicitFetch($use_implicit_fetch) |
|
329
|
|
|
{ |
|
330
|
|
|
static::$defaultUseImplicitFetch = $use_implicit_fetch; |
|
331
|
|
|
} |
|
332
|
|
|
|
|
333
|
|
|
/** |
|
334
|
|
|
* @return bool |
|
335
|
|
|
*/ |
|
336
|
|
|
public function getUseImplicitFetch() |
|
337
|
|
|
{ |
|
338
|
|
|
return $this->useImplicitFetch !== null |
|
339
|
|
|
? $this->useImplicitFetch |
|
340
|
|
|
: static::$defaultUseImplicitFetch; |
|
341
|
|
|
} |
|
342
|
|
|
|
|
343
|
|
|
/** |
|
344
|
|
|
* @param bool $useImplicitFetch |
|
345
|
|
|
*/ |
|
346
|
|
|
public function setUseImplicitFetch($useImplicitFetch) |
|
347
|
|
|
{ |
|
348
|
|
|
$this->useImplicitFetch = $useImplicitFetch; |
|
349
|
|
|
} |
|
350
|
|
|
|
|
351
|
|
|
/** |
|
352
|
|
|
* @return int|null |
|
353
|
|
|
*/ |
|
354
|
|
|
public function getIndexLeft() |
|
355
|
|
|
{ |
|
356
|
|
|
return $this->indexLeft; |
|
357
|
|
|
} |
|
358
|
|
|
|
|
359
|
|
|
/** |
|
360
|
|
|
* @return int|null |
|
361
|
|
|
*/ |
|
362
|
|
|
public function getIndexRight() |
|
363
|
|
|
{ |
|
364
|
|
|
return $this->indexRight; |
|
365
|
|
|
} |
|
366
|
|
|
|
|
367
|
|
|
/** |
|
368
|
|
|
* @return mixed |
|
369
|
|
|
*/ |
|
370
|
|
|
public function getParams() |
|
371
|
|
|
{ |
|
372
|
|
|
return $this->params; |
|
373
|
|
|
} |
|
374
|
|
|
/** |
|
375
|
|
|
* @param mixed $params |
|
376
|
|
|
*/ |
|
377
|
|
|
public function setParams($params) |
|
378
|
|
|
{ |
|
379
|
|
|
$this->params = $params; |
|
380
|
|
|
} |
|
381
|
|
|
} |
|
382
|
|
|
|
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..