|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Maztech\GraphNodes; |
|
4
|
|
|
|
|
5
|
|
|
use Maztech\InstagramRequest; |
|
6
|
|
|
use Maztech\Url\InstagramUrlManipulator; |
|
7
|
|
|
use Maztech\Exceptions\InstagramSDKException; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class GraphEdge |
|
11
|
|
|
* |
|
12
|
|
|
* @package Instagram |
|
13
|
|
|
*/ |
|
14
|
|
|
class GraphEdge extends Collection |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var InstagramRequest The original request that generated this data. |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $request; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var array An array of Graph meta data like pagination, etc. |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $metaData = []; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var string|null The parent Graph edge endpoint that generated the list. |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $parentEdgeEndpoint; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var string|null The subclass of the child GraphNode's. |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $subclassName; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Init this collection of GraphNode's. |
|
38
|
|
|
* |
|
39
|
|
|
* @param InstagramRequest $request The original request that generated this data. |
|
40
|
|
|
* @param array $data An array of GraphNode's. |
|
41
|
|
|
* @param array $metaData An array of Graph meta data like pagination, etc. |
|
42
|
|
|
* @param string|null $parentEdgeEndpoint The parent Graph edge endpoint that generated the list. |
|
43
|
|
|
* @param string|null $subclassName The subclass of the child GraphNode's. |
|
44
|
|
|
*/ |
|
45
|
|
|
public function __construct(InstagramRequest $request, array $data = [], array $metaData = [], $parentEdgeEndpoint = null, $subclassName = null) |
|
46
|
|
|
{ |
|
47
|
|
|
$this->request = $request; |
|
48
|
|
|
$this->metaData = $metaData; |
|
49
|
|
|
$this->parentEdgeEndpoint = $parentEdgeEndpoint; |
|
50
|
|
|
$this->subclassName = $subclassName; |
|
51
|
|
|
|
|
52
|
|
|
parent::__construct($data); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Gets the parent Graph edge endpoint that generated the list. |
|
57
|
|
|
* |
|
58
|
|
|
* @return string|null |
|
59
|
|
|
*/ |
|
60
|
|
|
public function getParentGraphEdge() |
|
61
|
|
|
{ |
|
62
|
|
|
return $this->parentEdgeEndpoint; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Gets the subclass name that the child GraphNode's are cast as. |
|
67
|
|
|
* |
|
68
|
|
|
* @return string|null |
|
69
|
|
|
*/ |
|
70
|
|
|
public function getSubClassName() |
|
71
|
|
|
{ |
|
72
|
|
|
return $this->subclassName; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Returns the raw meta data associated with this GraphEdge. |
|
77
|
|
|
* |
|
78
|
|
|
* @return array |
|
79
|
|
|
*/ |
|
80
|
|
|
public function getMetaData() |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->metaData; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Returns the next cursor if it exists. |
|
87
|
|
|
* |
|
88
|
|
|
* @return string|null |
|
89
|
|
|
*/ |
|
90
|
|
|
public function getNextCursor() |
|
91
|
|
|
{ |
|
92
|
|
|
return $this->getCursor('after'); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Returns the previous cursor if it exists. |
|
97
|
|
|
* |
|
98
|
|
|
* @return string|null |
|
99
|
|
|
*/ |
|
100
|
|
|
public function getPreviousCursor() |
|
101
|
|
|
{ |
|
102
|
|
|
return $this->getCursor('before'); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Returns the cursor for a specific direction if it exists. |
|
107
|
|
|
* |
|
108
|
|
|
* @param string $direction The direction of the page: after|before |
|
109
|
|
|
* |
|
110
|
|
|
* @return string|null |
|
111
|
|
|
*/ |
|
112
|
|
|
public function getCursor($direction) |
|
113
|
|
|
{ |
|
114
|
|
|
if (isset($this->metaData['paging']['cursors'][$direction])) { |
|
115
|
|
|
return $this->metaData['paging']['cursors'][$direction]; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
return null; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Generates a pagination URL based on a cursor. |
|
123
|
|
|
* |
|
124
|
|
|
* @param string $direction The direction of the page: next|previous |
|
125
|
|
|
* |
|
126
|
|
|
* @return string|null |
|
127
|
|
|
* |
|
128
|
|
|
* @throws InstagramSDKException |
|
129
|
|
|
*/ |
|
130
|
|
|
public function getPaginationUrl($direction) |
|
131
|
|
|
{ |
|
132
|
|
|
$this->validateForPagination(); |
|
133
|
|
|
|
|
134
|
|
|
// Do we have a paging URL? |
|
135
|
|
|
if (!isset($this->metaData['paging'][$direction])) { |
|
136
|
|
|
return null; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
$pageUrl = $this->metaData['paging'][$direction]; |
|
140
|
|
|
|
|
141
|
|
|
return InstagramUrlManipulator::baseGraphUrlEndpoint($pageUrl); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Validates whether or not we can paginate on this request. |
|
146
|
|
|
* |
|
147
|
|
|
* @throws InstagramSDKException |
|
148
|
|
|
*/ |
|
149
|
|
|
public function validateForPagination() |
|
150
|
|
|
{ |
|
151
|
|
|
if ($this->request->getMethod() !== 'GET') { |
|
152
|
|
|
throw new InstagramSDKException('You can only paginate on a GET request.', 720); |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Gets the request object needed to make a next|previous page request. |
|
158
|
|
|
* |
|
159
|
|
|
* @param string $direction The direction of the page: next|previous |
|
160
|
|
|
* |
|
161
|
|
|
* @return InstagramRequest|null |
|
162
|
|
|
* |
|
163
|
|
|
* @throws InstagramSDKException |
|
164
|
|
|
*/ |
|
165
|
|
|
public function getPaginationRequest($direction) |
|
166
|
|
|
{ |
|
167
|
|
|
$pageUrl = $this->getPaginationUrl($direction); |
|
168
|
|
|
if (!$pageUrl) { |
|
169
|
|
|
return null; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
$newRequest = clone $this->request; |
|
173
|
|
|
$newRequest->setEndpoint($pageUrl); |
|
174
|
|
|
|
|
175
|
|
|
return $newRequest; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* Gets the request object needed to make a "next" page request. |
|
180
|
|
|
* |
|
181
|
|
|
* @return InstagramRequest|null |
|
182
|
|
|
* |
|
183
|
|
|
* @throws InstagramSDKException |
|
184
|
|
|
*/ |
|
185
|
|
|
public function getNextPageRequest() |
|
186
|
|
|
{ |
|
187
|
|
|
return $this->getPaginationRequest('next'); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* Gets the request object needed to make a "previous" page request. |
|
192
|
|
|
* |
|
193
|
|
|
* @return InstagramRequest|null |
|
194
|
|
|
* |
|
195
|
|
|
* @throws InstagramSDKException |
|
196
|
|
|
*/ |
|
197
|
|
|
public function getPreviousPageRequest() |
|
198
|
|
|
{ |
|
199
|
|
|
return $this->getPaginationRequest('previous'); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* The total number of results according to Graph if it exists. |
|
204
|
|
|
* |
|
205
|
|
|
* This will be returned if the summary=true modifier is present in the request. |
|
206
|
|
|
* |
|
207
|
|
|
* @return int|null |
|
208
|
|
|
*/ |
|
209
|
|
|
public function getTotalCount() |
|
210
|
|
|
{ |
|
211
|
|
|
if (isset($this->metaData['summary']['total_count'])) { |
|
212
|
|
|
return $this->metaData['summary']['total_count']; |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
return null; |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* @inheritDoc |
|
220
|
|
|
*/ |
|
221
|
|
|
public function map(\Closure $callback) |
|
222
|
|
|
{ |
|
223
|
|
|
return new static( |
|
224
|
|
|
$this->request, |
|
225
|
|
|
array_map($callback, $this->items, array_keys($this->items)), |
|
226
|
|
|
$this->metaData, |
|
227
|
|
|
$this->parentEdgeEndpoint, |
|
228
|
|
|
$this->subclassName |
|
229
|
|
|
); |
|
230
|
|
|
} |
|
231
|
|
|
} |
|
232
|
|
|
|