1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Twitter\API\REST\Gateway; |
4
|
|
|
|
5
|
|
|
use Twitter\API\Exception\TwitterException; |
6
|
|
|
use Twitter\API\REST\Response\ApiRate; |
7
|
|
|
use Twitter\API\REST\Response\ApiResponse; |
8
|
|
|
use Twitter\API\REST\Response\HttpStatus; |
9
|
|
|
use Twitter\API\REST\Response\LimitedApiRate; |
10
|
|
|
use Twitter\API\REST\Response\UnlimitedApiRate; |
11
|
|
|
|
12
|
|
|
class CodebirdResponseParser |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @param object $result |
16
|
|
|
* |
17
|
|
|
* @return ApiResponse |
18
|
|
|
* |
19
|
|
|
* @throws TwitterException |
20
|
|
|
*/ |
21
|
27 |
|
public function parseObject($result) |
22
|
|
|
{ |
23
|
27 |
|
return $this->getResponse($result, false); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param object $result |
28
|
|
|
* |
29
|
|
|
* @return ApiResponse |
30
|
|
|
* |
31
|
|
|
* @throws TwitterException |
32
|
|
|
*/ |
33
|
6 |
|
public function parseList($result) |
34
|
|
|
{ |
35
|
6 |
|
return $this->getResponse($result, true); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Handles a twitter API response object |
40
|
|
|
* |
41
|
|
|
* @param object $result |
42
|
|
|
* @param bool $isList |
43
|
|
|
* |
44
|
|
|
* @return ApiResponse |
45
|
|
|
* |
46
|
|
|
* @throws TwitterException |
47
|
|
|
*/ |
48
|
33 |
|
private function getResponse($result, $isList) |
49
|
|
|
{ |
50
|
33 |
|
$this->handleErrors($result); |
51
|
|
|
|
52
|
30 |
|
$httpStatus = $this->getHttpStatus($result); |
53
|
27 |
|
$rate = $this->getRate($result); |
54
|
27 |
|
$content = $this->getContent($result, $isList); |
55
|
|
|
|
56
|
27 |
|
return new ApiResponse($httpStatus, $content, $rate); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param object $result |
61
|
|
|
* |
62
|
|
|
* @throws TwitterException |
63
|
|
|
*/ |
64
|
33 |
|
private function handleErrors($result) |
65
|
|
|
{ |
66
|
33 |
|
if (isset($result->errors)) { |
67
|
3 |
|
$error = reset($result->errors); |
68
|
3 |
|
throw new TwitterException($error->message, $error->code); |
69
|
|
|
} |
70
|
30 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param object $result |
74
|
|
|
* |
75
|
|
|
* @return HttpStatus |
76
|
|
|
* |
77
|
|
|
* @throws TwitterException |
78
|
|
|
*/ |
79
|
30 |
|
private function getHttpStatus($result) |
80
|
|
|
{ |
81
|
30 |
|
$httpStatus = new HttpStatus($result->httpstatus); |
82
|
30 |
|
if ($httpStatus->isError()) { |
83
|
3 |
|
throw new TwitterException($result->message); |
84
|
|
|
} |
85
|
|
|
|
86
|
27 |
|
return $httpStatus; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param object $result |
91
|
|
|
* |
92
|
|
|
* @return ApiRate |
93
|
|
|
*/ |
94
|
27 |
|
private function getRate($result) |
95
|
|
|
{ |
96
|
27 |
|
if (isset($result->rate)) { |
97
|
|
|
return new LimitedApiRate( |
98
|
|
|
$result->rate['limit'], |
99
|
|
|
$result->rate['remaining'], |
100
|
|
|
$result->rate['reset'] |
101
|
|
|
); |
102
|
|
|
} |
103
|
|
|
|
104
|
27 |
|
return new UnlimitedApiRate(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param object $result |
109
|
|
|
* @param bool $isList |
110
|
|
|
* |
111
|
|
|
* @return object|array|null |
112
|
|
|
*/ |
113
|
27 |
|
private function getContent($result, $isList) |
114
|
|
|
{ |
115
|
27 |
|
$content = $result; |
116
|
|
|
|
117
|
27 |
|
unset($content->httpstatus, $content->rate); |
118
|
|
|
|
119
|
27 |
|
if ($isList) { |
120
|
6 |
|
$content = []; |
121
|
6 |
|
foreach ($result as $index => $obj) { |
122
|
|
|
if (is_numeric($index)) { |
123
|
2 |
|
$content[(int) $index] = $obj; |
124
|
|
|
} |
125
|
4 |
|
} |
126
|
18 |
|
} elseif (empty($content)) { |
127
|
|
|
$content = null; |
128
|
|
|
} |
129
|
|
|
|
130
|
27 |
|
return $content; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|