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 \IteratorAggregate |
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
|
|
|
public function __construct(/* @noinspection PhpUnnecessaryFullyQualifiedNameInspection */ |
26
|
|
|
\Hborras\TwitterAdsSDK\TwitterAds\Resource $resource, TwitterAds $twitterAds, $request, $params) |
27
|
|
|
{ |
28
|
|
|
$this->resource = $resource; |
|
|
|
|
29
|
|
|
$this->params = $params; |
30
|
|
|
$this->twitterAds = $twitterAds; |
31
|
|
|
$this->fromResponse($request); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @return bool |
36
|
|
|
*/ |
37
|
|
|
public function isExhausted() |
38
|
|
|
{ |
39
|
|
|
return is_null($this->next_cursor) ? true : false; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return integer |
44
|
|
|
*/ |
45
|
|
|
public function count() |
46
|
|
|
{ |
47
|
|
|
return max($this->total_count, count($this->collection)); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return Resource |
52
|
|
|
*/ |
53
|
|
|
public function first() |
54
|
|
|
{ |
55
|
|
|
return $this->next(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return int |
60
|
|
|
*/ |
61
|
|
|
public function fetched() |
62
|
|
|
{ |
63
|
|
|
return count($this->collection); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return Resource | 0 |
68
|
|
|
*/ |
69
|
|
|
public function next() |
70
|
|
|
{ |
71
|
|
|
if ($this->current_index < count($this->collection)) { |
72
|
|
|
$value = $this->collection[$this->current_index]; |
73
|
|
|
++$this->current_index; |
74
|
|
|
|
75
|
|
|
return $value; |
76
|
|
|
} elseif (!is_null($this->next_cursor)) { |
77
|
|
|
$this->fetchNext(); |
78
|
|
|
|
79
|
|
|
return $this->next(); |
80
|
|
|
} else { |
81
|
|
|
$this->current_index = 0; |
82
|
|
|
|
83
|
|
|
return 0; |
|
|
|
|
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param array $params |
89
|
|
|
* @return Cursor |
90
|
|
|
*/ |
91
|
|
|
public function fetchNext($params = []) |
92
|
|
|
{ |
93
|
|
|
$params['cursor'] = $this->next_cursor; |
94
|
|
|
switch ($this->getTwitterAds()->getMethod()) { |
95
|
|
|
case 'GET': |
96
|
|
|
$response = $this->getTwitterAds()->get($this->getTwitterAds()->getResource(), $params); |
97
|
|
|
break; |
98
|
|
|
case 'POST': |
99
|
|
|
$response = $this->getTwitterAds()->post($this->getTwitterAds()->getResource(), $params); |
100
|
|
|
break; |
101
|
|
|
case 'PUT': |
102
|
|
|
$response = $this->getTwitterAds()->put($this->getTwitterAds()->getResource(), $params); |
103
|
|
|
break; |
104
|
|
|
case 'DELETE': |
105
|
|
|
$response = $this->getTwitterAds()->delete($this->getTwitterAds()->getResource()); |
106
|
|
|
break; |
107
|
|
|
default: |
108
|
|
|
$response = $this->getTwitterAds()->get($this->getTwitterAds()->getResource(), $params); |
109
|
|
|
break; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return $this->fromResponse($response->getBody()); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param $request |
117
|
|
|
* @return $this |
118
|
|
|
*/ |
119
|
|
|
public function fromResponse($request) |
120
|
|
|
{ |
121
|
|
|
$this->next_cursor = isset($request->next_cursor) ? $request->next_cursor : null; |
122
|
|
|
if (isset($request->total_count)) { |
123
|
|
|
$this->total_count = intval($request->total_count); |
124
|
|
|
} |
125
|
|
|
foreach ($request->data as $item) { |
126
|
|
|
if (method_exists($this->resource, 'fromResponse')) { |
127
|
|
|
$obj = new $this->resource(); |
128
|
|
|
$this->collection[] = $obj->fromResponse($item); |
129
|
|
|
} else { |
130
|
|
|
$this->collection[] = $item; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return $this; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* {@inheritdoc} |
139
|
|
|
*/ |
140
|
|
|
public function getIterator() |
141
|
|
|
{ |
142
|
|
|
return new \ArrayIterator($this->collection); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @return array |
147
|
|
|
*/ |
148
|
|
|
public function getCollection() |
149
|
|
|
{ |
150
|
|
|
return $this->collection; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @param array $collection |
155
|
|
|
*/ |
156
|
|
|
public function setCollection($collection) |
157
|
|
|
{ |
158
|
|
|
$this->collection = $collection; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @return TwitterAds |
163
|
|
|
*/ |
164
|
|
|
public function getTwitterAds() |
165
|
|
|
{ |
166
|
|
|
return $this->twitterAds; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @param TwitterAds $twitterAds |
171
|
|
|
*/ |
172
|
|
|
public function setTwitterAds($twitterAds) |
173
|
|
|
{ |
174
|
|
|
$this->twitterAds = $twitterAds; |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
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..