-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathResult.php
319 lines (262 loc) · 6.96 KB
/
Result.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
<?php
namespace romanzipp\Twitch;
use Psr\Http\Client\RequestExceptionInterface;
use Psr\Http\Message\ResponseInterface;
use romanzipp\Twitch\Objects\Paginator;
class Result
{
/**
* Guzzle exception, if present.
*
* @var RequestExceptionInterface|null
*/
private ?RequestExceptionInterface $exception = null;
/**
* Query result data.
*
* @var array<string, mixed>|\stdClass
*/
private \stdClass|array $data = [];
/**
* Message field, if present.
*
* @var string|null
*/
private ?string $message = null;
/**
* Total amount of result data.
*
* @var int
*/
private int $total = 0;
/**
* Status Code.
*
* @var int
*/
private int $status;
/**
* Twitch response pagination cursor.
*
* @var \stdClass|null
*/
private ?\stdClass $pagination = null;
/**
* Internal paginator.
*
* @var Paginator|null
*/
private ?Paginator $paginator = null;
/**
* Original Guzzle HTTP Response.
*
* @var ResponseInterface
*/
public ResponseInterface $response;
public function __construct(ResponseInterface $response, ?RequestExceptionInterface $exception = null)
{
$this->response = $response;
$this->status = $response->getStatusCode();
$this->exception = $exception;
$this->processPayload($response);
$this->paginator = Paginator::from($this);
}
public function success(): bool
{
return null === $this->exception;
}
/**
* @return array<string, mixed>|\stdClass
*/
public function data()
{
return $this->data;
}
public function getTotal(): int
{
return $this->total;
}
public function getPagination(): ?\stdClass
{
return $this->pagination;
}
public function getPaginator(): ?Paginator
{
return $this->paginator;
}
public function getStatus(): int
{
return $this->status;
}
public function getException(): ?RequestExceptionInterface
{
return $this->exception;
}
public function getResponse(): ?ResponseInterface
{
return $this->response;
}
/**
* Return the current count of items in dataset.
*
* @return int Count
*/
public function count(): int
{
if ( ! is_array($this->data)) {
return 0;
}
return count((array) $this->data);
}
/**
* Returns the last HTTP or API error.
*
* @return string Error message
*/
public function getErrorMessage(): string
{
/** @phpstan-ignore-next-line */
if (null === $this->exception || null === $this->exception->getResponse()) {
return 'Twitch API Unavailable';
}
if ( ! is_string($this->message)) {
return $this->exception->getMessage();
}
return $this->message;
}
/**
* Shifts the current result (Use for single user/video etc. query).
*
* @return mixed Shifted data
*/
public function shift()
{
if ( ! is_array($this->data)) {
return null;
}
if ( ! empty($data = $this->data)) {
return array_shift($data);
}
return null;
}
/**
* Set the Paginator to fetch the next set of results.
*
* @return Paginator|null
*/
public function next(): ?Paginator
{
if (null === $this->paginator) {
return null;
}
return $this->paginator->next();
}
/**
* Set the Paginator to fetch the last set of results.
*
* @return Paginator|null
*/
public function back(): ?Paginator
{
if (null === $this->paginator) {
return null;
}
return $this->paginator->back();
}
/**
* Check if the response contains a cursor to the next set of results.
*
* @return bool
*/
public function hasMoreResults(): bool
{
if (0 === $this->count()) {
return false;
}
if (null === $this->paginator) {
return false;
}
return null !== $this->paginator->cursor();
}
/**
* Get rate limit information.
*
* @param string|null $key Get an index value. Available: limit, remaining, reset
*
* @return int|array<string, int>|null
*/
public function getRateLimit(?string $key = null)
{
if ( ! $this->response->hasHeader('Ratelimit-Remaining')) {
return null;
}
$rateLimit = [
'limit' => (int) $this->response->getHeaderLine('Ratelimit-Limit'),
'remaining' => (int) $this->response->getHeaderLine('Ratelimit-Remaining'),
'reset' => (int) $this->response->getHeaderLine('Ratelimit-Reset'),
];
if (null === $key) {
return $rateLimit;
}
return $rateLimit[$key] ?? null;
}
public function isOAuthError(): bool
{
if (null === $this->exception) {
return false;
}
return 401 === $this->getStatus() && in_array($this->getErrorMessage(), [
'Invalid OAuth token',
'OAuth token is missing',
]);
}
/**
* Insert users in data response.
*
* @param Twitch $twitch
* @param string $identifierAttribute Attribute to identify the users
* @param string $insertTo Data index to insert user data
*
* @return self
*/
public function insertUsers(Twitch $twitch, string $identifierAttribute = 'user_id', string $insertTo = 'user'): self
{
$data = $this->data;
$userIds = collect($data)->map(function ($item) use ($identifierAttribute) {
return $item->{$identifierAttribute};
})->toArray();
if (0 === count($userIds)) {
return $this;
}
$users = collect($twitch->getUsers(['id' => $userIds])->data);
$dataWithUsers = collect($data)
->map(static function ($item) use ($users, $identifierAttribute, $insertTo) {
$item->$insertTo = $users->where('id', $item->{$identifierAttribute})->first();
return $item;
});
$this->data = $dataWithUsers->toArray();
return $this;
}
/**
* Map response payload to instance attributes.
*
* @param ResponseInterface $response
*/
protected function processPayload(ResponseInterface $response): void
{
$payload = @json_decode(
$response->getBody()
);
if (null === $payload) {
return;
}
$this->message = $payload->message ?? null;
$this->total = $payload->total ?? 0;
$this->pagination = $payload->pagination ?? null;
if ( ! property_exists($payload, 'data')) {
$this->data = $payload;
return;
}
$this->data = $payload->data ?? [];
}
}