1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the TelegramBot package. |
4
|
|
|
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]> |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Longman\TelegramBot\Http; |
10
|
|
|
|
11
|
|
|
use Longman\TelegramBot\Entities\Chat; |
12
|
|
|
use Longman\TelegramBot\Entities\ChatMember; |
13
|
|
|
use Longman\TelegramBot\Entities\File; |
14
|
|
|
use Longman\TelegramBot\Entities\Message; |
15
|
|
|
use Longman\TelegramBot\Entities\Update; |
16
|
|
|
use Longman\TelegramBot\Entities\User; |
17
|
|
|
use Longman\TelegramBot\Entities\UserProfilePhotos; |
18
|
|
|
use Longman\TelegramBot\Entities\WebhookInfo; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class ServerResponse |
22
|
|
|
* |
23
|
|
|
* @link https://core.telegram.org/bots/api#making-requests |
24
|
|
|
* |
25
|
|
|
* @todo method ResponseParameters getParameters() Field which can help to automatically handle the error |
26
|
|
|
*/ |
27
|
|
|
class Response |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* ServerResponse constructor. |
31
|
|
|
* |
32
|
|
|
* @param array $data |
33
|
|
|
* @param string $bot_username |
34
|
|
|
* |
35
|
|
|
* @throws \Longman\TelegramBot\Exception\TelegramException |
36
|
|
|
*/ |
37
|
10 |
|
public function __construct(array $data, $bot_username = '') |
38
|
|
|
{ |
39
|
|
|
// Make sure we don't double-save the raw_data |
40
|
10 |
|
unset($data['raw_data']); |
41
|
10 |
|
$data['raw_data'] = $data; |
42
|
|
|
|
43
|
10 |
|
$is_ok = isset($data['ok']) ? (bool) $data['ok'] : false; |
44
|
10 |
|
$result = isset($data['result']) ? $data['result'] : null; |
45
|
|
|
|
46
|
10 |
|
if ($is_ok && is_array($result)) { |
47
|
6 |
|
if ($this->isAssoc($result)) { |
48
|
4 |
|
$data['result'] = $this->createResultObject($result, $bot_username); |
49
|
|
|
} else { |
50
|
2 |
|
$data['result'] = $this->createResultObjects($result, $bot_username); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
//Make sure we're not raw_data inception-ing |
55
|
10 |
View Code Duplication |
if (array_key_exists('raw_data', $data)) { |
|
|
|
|
56
|
10 |
|
if ($data['raw_data'] === null) { |
57
|
10 |
|
unset($data['raw_data']); |
58
|
|
|
} |
59
|
|
|
} else { |
60
|
|
|
$data['raw_data'] = $data; |
61
|
|
|
} |
62
|
|
|
|
63
|
10 |
|
$data['bot_username'] = $bot_username; |
64
|
|
|
|
65
|
10 |
|
$this->assignMemberVariables($data); |
66
|
10 |
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Helper to set member variables |
70
|
|
|
* |
71
|
|
|
* @param array $data |
72
|
|
|
*/ |
73
|
10 |
|
protected function assignMemberVariables(array $data) |
74
|
|
|
{ |
75
|
10 |
|
foreach ($data as $key => $value) { |
76
|
10 |
|
$this->$key = $value; |
77
|
|
|
} |
78
|
10 |
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Check if array is associative |
82
|
|
|
* |
83
|
|
|
* @link https://stackoverflow.com/a/4254008 |
84
|
|
|
* |
85
|
|
|
* @param array $array |
86
|
|
|
* |
87
|
|
|
* @return bool |
88
|
|
|
*/ |
89
|
6 |
|
protected function isAssoc(array $array) |
90
|
|
|
{ |
91
|
6 |
|
return count(array_filter(array_keys($array), 'is_string')) > 0; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* If response is ok |
96
|
|
|
* |
97
|
|
|
* @return bool |
98
|
|
|
*/ |
99
|
6 |
|
public function isOk() |
100
|
|
|
{ |
101
|
6 |
|
return $this->getOk(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* If response is ok |
106
|
|
|
* |
107
|
|
|
* @return bool |
108
|
|
|
*/ |
109
|
6 |
|
public function getOk() |
110
|
|
|
{ |
111
|
6 |
|
return isset($this->ok) ? (bool) $this->ok : false; |
|
|
|
|
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Return result |
116
|
|
|
* |
117
|
|
|
* @return mixed |
118
|
|
|
*/ |
119
|
9 |
|
public function getResult() |
120
|
|
|
{ |
121
|
9 |
|
return isset($this->result) ? $this->result : null; |
|
|
|
|
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Return error code |
126
|
|
|
* |
127
|
|
|
* @return int |
128
|
|
|
*/ |
129
|
5 |
|
public function getErrorCode() |
130
|
|
|
{ |
131
|
5 |
|
return isset($this->error_code) ? $this->error_code : null; |
|
|
|
|
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Return human-readable description of the result / unsuccessful request |
136
|
|
|
* |
137
|
|
|
* @return string |
138
|
|
|
*/ |
139
|
5 |
|
public function getDescription() |
140
|
|
|
{ |
141
|
5 |
|
return isset($this->description) ? $this->description : null; |
|
|
|
|
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Print error |
146
|
|
|
* |
147
|
|
|
* @see https://secure.php.net/manual/en/function.print-r.php |
148
|
|
|
* |
149
|
|
|
* @param bool $return |
150
|
|
|
* |
151
|
|
|
* @return bool|string |
152
|
|
|
*/ |
153
|
|
|
public function printError($return = false) |
154
|
|
|
{ |
155
|
|
|
$error = sprintf('Error N: %s, Description: %s', $this->getErrorCode(), $this->getDescription()); |
156
|
|
|
|
157
|
|
|
if ($return) { |
158
|
|
|
return $error; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
echo $error; |
162
|
|
|
|
163
|
|
|
return true; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Create and return the object of the received result |
168
|
|
|
* |
169
|
|
|
* @param array $result |
170
|
|
|
* @param string $bot_username |
171
|
|
|
* |
172
|
|
|
* @return \Longman\TelegramBot\Entities\Chat|\Longman\TelegramBot\Entities\ChatMember|\Longman\TelegramBot\Entities\File|\Longman\TelegramBot\Entities\Message|\Longman\TelegramBot\Entities\User|\Longman\TelegramBot\Entities\UserProfilePhotos|\Longman\TelegramBot\Entities\WebhookInfo |
173
|
|
|
* @throws \Longman\TelegramBot\Exception\TelegramException |
174
|
|
|
*/ |
175
|
4 |
|
private function createResultObject($result, $bot_username) |
176
|
|
|
{ |
177
|
|
|
// We don't need to save the raw_data of the response object! |
178
|
4 |
|
$result['raw_data'] = null; |
179
|
|
|
|
180
|
|
|
$result_object_types = [ |
181
|
4 |
|
'total_count' => UserProfilePhotos::class, //Response from getUserProfilePhotos |
182
|
|
|
'file_id' => File::class, //Response from getFile |
183
|
|
|
'title' => Chat::class, //Response from getChat |
184
|
|
|
'username' => User::class, //Response from getMe |
185
|
|
|
'user' => ChatMember::class, //Response from getChatMember |
186
|
|
|
'url' => WebhookInfo::class, //Response from getWebhookInfo |
187
|
|
|
]; |
188
|
4 |
|
foreach ($result_object_types as $type => $object_class) { |
189
|
4 |
|
if (isset($result[$type])) { |
190
|
4 |
|
return new $object_class($result); |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
//Response from sendMessage |
195
|
2 |
|
return new Message($result, $bot_username); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Create and return the objects array of the received result |
200
|
|
|
* |
201
|
|
|
* @param array $result |
202
|
|
|
* @param string $bot_username |
203
|
|
|
* |
204
|
|
|
* @return null|\Longman\TelegramBot\Entities\ChatMember[]|\Longman\TelegramBot\Entities\Update[] |
205
|
|
|
* @throws \Longman\TelegramBot\Exception\TelegramException |
206
|
|
|
*/ |
207
|
2 |
|
private function createResultObjects($result, $bot_username) |
208
|
|
|
{ |
209
|
2 |
|
$results = []; |
210
|
2 |
|
if (isset($result[0]['user'])) { |
211
|
|
|
//Response from getChatAdministrators |
212
|
|
|
foreach ($result as $user) { |
213
|
|
|
// We don't need to save the raw_data of the response object! |
214
|
|
|
$user['raw_data'] = null; |
215
|
|
|
|
216
|
|
|
$results[] = new ChatMember($user); |
217
|
|
|
} |
218
|
|
|
} else { |
219
|
|
|
//Get Update |
220
|
2 |
|
foreach ($result as $update) { |
221
|
|
|
// We don't need to save the raw_data of the response object! |
222
|
1 |
|
$update['raw_data'] = null; |
223
|
|
|
|
224
|
1 |
|
$results[] = new Update($update, $bot_username); |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
|
228
|
2 |
|
return $results; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Return an empty Server Response |
233
|
|
|
* |
234
|
|
|
* No request to telegram are sent, this function is used in commands that |
235
|
|
|
* don't need to fire a message after execution |
236
|
|
|
* |
237
|
|
|
* @return \Longman\TelegramBot\Http\Response |
238
|
|
|
* @throws \Longman\TelegramBot\Exception\TelegramException |
239
|
|
|
*/ |
240
|
|
|
public static function createEmpty() |
241
|
|
|
{ |
242
|
|
|
return new static(['ok' => true, 'result' => true]); |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.