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 Response |
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
|
|
|
/** @var bool */ |
30
|
|
|
protected $ok = false; |
31
|
|
|
|
32
|
|
|
/** @var string */ |
33
|
|
|
protected $description; |
34
|
|
|
|
35
|
|
|
/** @var array */ |
36
|
|
|
protected $result; |
37
|
|
|
|
38
|
|
|
/** @var int */ |
39
|
|
|
protected $error_code; |
40
|
|
|
|
41
|
|
|
/** @var array */ |
42
|
|
|
protected $parameters; |
43
|
|
|
|
44
|
|
|
/** @var array */ |
45
|
|
|
protected $raw_data; |
46
|
|
|
|
47
|
|
|
/** @var string */ |
48
|
|
|
protected $bot_username; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Response constructor. |
52
|
|
|
* |
53
|
|
|
* @param array $data |
54
|
|
|
* @param string $bot_username |
55
|
|
|
* |
56
|
|
|
* @throws \Longman\TelegramBot\Exception\TelegramException |
57
|
|
|
*/ |
58
|
12 |
|
public function __construct(array $data, $bot_username = '') |
59
|
|
|
{ |
60
|
12 |
|
$this->raw_data = $data; |
61
|
|
|
|
62
|
12 |
|
$this->ok = $data['ok']; |
63
|
|
|
|
64
|
12 |
|
if (isset($data['description'])) { |
65
|
3 |
|
$this->description = $data['description']; |
66
|
|
|
} |
67
|
|
|
|
68
|
12 |
|
if (isset($data['error_code'])) { |
69
|
2 |
|
$this->error_code = $data['error_code']; |
70
|
|
|
} |
71
|
|
|
|
72
|
12 |
|
if (isset($data['parameters'])) { |
73
|
|
|
$this->parameters = $data['parameters']; |
74
|
|
|
} |
75
|
|
|
|
76
|
12 |
|
$this->bot_username = $bot_username; |
77
|
|
|
|
78
|
12 |
|
$result = isset($data['result']) ? $data['result'] : null; |
79
|
12 |
|
if ($this->ok && is_array($result)) { |
80
|
6 |
|
if ($this->isAssoc($result)) { |
81
|
4 |
|
$this->result = $this->createResultObject($result, $bot_username); |
|
|
|
|
82
|
|
|
} else { |
83
|
2 |
|
$this->result = $this->createResultObjects($result, $bot_username); |
84
|
|
|
} |
85
|
|
|
} |
86
|
12 |
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Check if array is associative |
90
|
|
|
* |
91
|
|
|
* @link https://stackoverflow.com/a/4254008 |
92
|
|
|
* |
93
|
|
|
* @param array $array |
94
|
|
|
* |
95
|
|
|
* @return bool |
96
|
|
|
*/ |
97
|
6 |
|
protected function isAssoc(array $array) |
98
|
|
|
{ |
99
|
6 |
|
return count(array_filter(array_keys($array), 'is_string')) > 0; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* If response is ok |
104
|
|
|
* |
105
|
|
|
* @return bool |
106
|
|
|
*/ |
107
|
6 |
|
public function isOk() |
108
|
|
|
{ |
109
|
6 |
|
return $this->ok; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Return result |
114
|
|
|
* |
115
|
|
|
* @return array |
116
|
|
|
*/ |
117
|
10 |
|
public function getResult() |
118
|
|
|
{ |
119
|
10 |
|
return ! empty($this->result) ? $this->result : []; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Return error code |
124
|
|
|
* |
125
|
|
|
* @return int |
126
|
|
|
*/ |
127
|
5 |
|
public function getErrorCode() |
128
|
|
|
{ |
129
|
5 |
|
return ! empty($this->error_code) ? $this->error_code : null; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Return human-readable description of the result / unsuccessful request |
134
|
|
|
* |
135
|
|
|
* @return string |
136
|
|
|
*/ |
137
|
5 |
|
public function getDescription() |
138
|
|
|
{ |
139
|
5 |
|
return ! empty($this->description) ? $this->description : null; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Get error |
144
|
|
|
* |
145
|
|
|
* @see https://secure.php.net/manual/en/function.print-r.php |
146
|
|
|
* |
147
|
|
|
* @return string |
148
|
|
|
*/ |
149
|
|
|
public function getError() |
150
|
|
|
{ |
151
|
|
|
$error = sprintf('Error N: %s, Description: %s', $this->getErrorCode(), $this->getDescription()); |
152
|
|
|
|
153
|
|
|
return $error; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Create and return the object of the received result |
158
|
|
|
* |
159
|
|
|
* @param array $result |
160
|
|
|
* @param string $bot_username |
161
|
|
|
* |
162
|
|
|
* @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 |
163
|
|
|
* @throws \Longman\TelegramBot\Exception\TelegramException |
164
|
|
|
*/ |
165
|
4 |
|
protected function createResultObject($result, $bot_username) |
166
|
|
|
{ |
167
|
|
|
$result_object_types = [ |
168
|
4 |
|
'total_count' => UserProfilePhotos::class, // Response from getUserProfilePhotos |
169
|
|
|
'file_id' => File::class, // Response from getFile |
170
|
|
|
'title' => Chat::class, // Response from getChat |
171
|
|
|
'username' => User::class, // Response from getMe |
172
|
|
|
'user' => ChatMember::class, // Response from getChatMember |
173
|
|
|
'url' => WebhookInfo::class, // Response from getWebhookInfo |
174
|
|
|
]; |
175
|
4 |
|
foreach ($result_object_types as $type => $object_class) { |
176
|
4 |
|
if (isset($result[$type])) { |
177
|
4 |
|
return new $object_class($result, $bot_username); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
// Response from sendMessage |
182
|
2 |
|
return new Message($result, $bot_username); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Create and return the objects array of the received result |
187
|
|
|
* |
188
|
|
|
* @param array $result |
189
|
|
|
* @param string $bot_username |
190
|
|
|
* |
191
|
|
|
* @return null|\Longman\TelegramBot\Entities\ChatMember[]|\Longman\TelegramBot\Entities\Update[] |
192
|
|
|
* @throws \Longman\TelegramBot\Exception\TelegramException |
193
|
|
|
*/ |
194
|
2 |
|
protected function createResultObjects($result, $bot_username) |
195
|
|
|
{ |
196
|
2 |
|
$results = []; |
197
|
2 |
|
if (isset($result[0]['user'])) { |
198
|
|
|
// Response from getChatAdministrators |
199
|
|
|
foreach ($result as $user) { |
200
|
|
|
// We don't need to save the raw_data of the response object! |
201
|
|
|
$user['raw_data'] = null; |
202
|
|
|
|
203
|
|
|
$results[] = new ChatMember($user); |
204
|
|
|
} |
205
|
|
|
} else { |
206
|
|
|
// Get Update |
207
|
2 |
|
foreach ($result as $update) { |
208
|
|
|
// We don't need to save the raw_data of the response object! |
209
|
1 |
|
$update['raw_data'] = null; |
210
|
|
|
|
211
|
1 |
|
$results[] = new Update($update, $bot_username); |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
|
215
|
2 |
|
return $results; |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
|
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..