1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Telegram\Bot; |
4
|
|
|
|
5
|
|
|
use Telegram\Bot\Exceptions\TelegramSDKException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class TelegramRequest. |
9
|
|
|
* |
10
|
|
|
* Builds Telegram Bot API Request Entity. |
11
|
|
|
*/ |
12
|
|
|
class TelegramRequest |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var string|null The bot access token to use for this request. |
16
|
|
|
*/ |
17
|
|
|
protected $accessToken; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var string The HTTP method for this request. |
21
|
|
|
*/ |
22
|
|
|
protected $method; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string The API endpoint for this request. |
26
|
|
|
*/ |
27
|
|
|
protected $endpoint; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var array The headers to send with this request. |
31
|
|
|
*/ |
32
|
|
|
protected $headers = []; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var array The parameters to send with this request. |
36
|
|
|
*/ |
37
|
|
|
protected $params = []; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var array Special parameters for HttpClient. |
41
|
|
|
*/ |
42
|
|
|
protected $httpClientParams = []; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var array The files to send with this request. |
46
|
|
|
*/ |
47
|
|
|
protected $files = []; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Indicates if the request to Telegram will be asynchronous (non-blocking). |
51
|
|
|
* |
52
|
|
|
* @var bool |
53
|
|
|
*/ |
54
|
|
|
protected $isAsyncRequest = false; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Creates a new Request entity. |
58
|
|
|
* |
59
|
|
|
* @param string|null $accessToken |
60
|
40 |
|
* @param string|null $method |
61
|
|
|
* @param string|null $endpoint |
62
|
|
|
* @param array|null $params |
63
|
|
|
* @param bool $isAsyncRequest |
64
|
|
|
*/ |
65
|
|
|
public function __construct( |
66
|
|
|
$accessToken = null, |
67
|
40 |
|
$method = null, |
68
|
40 |
|
$endpoint = null, |
69
|
40 |
|
array $params = [], |
70
|
40 |
|
$isAsyncRequest = false |
71
|
40 |
|
) { |
72
|
40 |
|
$this->setAccessToken($accessToken); |
73
|
|
|
$this->setMethod($method); |
74
|
|
|
$this->setEndpoint($endpoint); |
75
|
|
|
$this->setParams($params); |
76
|
|
|
$this->setHttpClientParams($params); |
77
|
|
|
$this->setAsyncRequest($isAsyncRequest); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
40 |
|
* Set the bot access token for this request. |
82
|
|
|
* |
83
|
40 |
|
* @param string |
84
|
|
|
* |
85
|
40 |
|
* @return TelegramRequest |
86
|
|
|
*/ |
87
|
|
|
public function setAccessToken($accessToken) |
88
|
|
|
{ |
89
|
|
|
$this->accessToken = $accessToken; |
90
|
|
|
|
91
|
|
|
return $this; |
92
|
|
|
} |
93
|
40 |
|
|
94
|
|
|
/** |
95
|
40 |
|
* Return the bot access token for this request. |
96
|
|
|
* |
97
|
|
|
* @return string|null |
98
|
|
|
*/ |
99
|
|
|
public function getAccessToken() |
100
|
|
|
{ |
101
|
|
|
return $this->accessToken; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Validate that bot access token exists for this request. |
106
|
|
|
* |
107
|
|
|
* @throws TelegramSDKException |
108
|
|
|
*/ |
109
|
|
|
public function validateAccessToken() |
110
|
|
|
{ |
111
|
|
|
$accessToken = $this->getAccessToken(); |
112
|
|
|
if ($accessToken === null) { |
113
|
|
|
throw new TelegramSDKException('You must provide your bot access token to make any API requests.'); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
40 |
|
* Set the HTTP method for this request. |
119
|
|
|
* |
120
|
40 |
|
* @param string |
121
|
|
|
* |
122
|
40 |
|
* @return TelegramRequest |
123
|
|
|
*/ |
124
|
|
|
public function setMethod($method) |
125
|
|
|
{ |
126
|
|
|
$this->method = strtoupper($method); |
127
|
|
|
|
128
|
|
|
return $this; |
129
|
|
|
} |
130
|
40 |
|
|
131
|
|
|
/** |
132
|
40 |
|
* Return the HTTP method for this request. |
133
|
|
|
* |
134
|
|
|
* @return string |
135
|
|
|
*/ |
136
|
|
|
public function getMethod() |
137
|
|
|
{ |
138
|
|
|
return $this->method; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Validate that the HTTP method is set. |
143
|
|
|
* |
144
|
|
|
* @throws TelegramSDKException |
145
|
|
|
*/ |
146
|
|
|
public function validateMethod() |
147
|
|
|
{ |
148
|
|
|
if (!$this->method) { |
149
|
|
|
throw new TelegramSDKException('HTTP method not specified.'); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
if (!in_array($this->method, ['GET', 'POST'])) { |
153
|
|
|
throw new TelegramSDKException('Invalid HTTP method specified.'); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
40 |
|
* Set the endpoint for this request. |
159
|
|
|
* |
160
|
40 |
|
* @param string $endpoint |
161
|
|
|
* |
162
|
40 |
|
* @return TelegramRequest |
163
|
|
|
*/ |
164
|
|
|
public function setEndpoint($endpoint) |
165
|
|
|
{ |
166
|
|
|
$this->endpoint = $endpoint; |
167
|
|
|
|
168
|
|
|
return $this; |
169
|
|
|
} |
170
|
40 |
|
|
171
|
|
|
/** |
172
|
40 |
|
* Return the API Endpoint for this request. |
173
|
|
|
* |
174
|
|
|
* @return string |
175
|
|
|
*/ |
176
|
|
|
public function getEndpoint() |
177
|
|
|
{ |
178
|
|
|
return $this->endpoint; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
40 |
|
* Set the params for this request. |
183
|
|
|
* |
184
|
40 |
|
* @param array $params |
185
|
|
|
* |
186
|
40 |
|
* @return TelegramRequest |
187
|
|
|
*/ |
188
|
|
View Code Duplication |
public function setParams(array $params = []) |
|
|
|
|
189
|
|
|
{ |
190
|
|
|
$params = self::filterParams($params, 'http_client_', false); |
191
|
|
|
$this->params = array_merge($this->params, $params); |
192
|
|
|
|
193
|
|
|
return $this; |
194
|
40 |
|
} |
195
|
|
|
|
196
|
40 |
|
/** |
197
|
|
|
* Return the params for this request. |
198
|
|
|
* |
199
|
|
|
* @return array |
200
|
|
|
*/ |
201
|
|
|
public function getParams() |
202
|
|
|
{ |
203
|
|
|
return $this->params; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Filter parameters for HttpClient. |
208
|
|
|
* |
209
|
|
|
* @param array $params |
210
|
|
|
* |
211
|
|
|
* @return $this |
212
|
|
|
*/ |
213
|
|
View Code Duplication |
public function setHttpClientParams(array $params) |
|
|
|
|
214
|
|
|
{ |
215
|
|
|
$params = self::filterParams($params, 'http_client_', true); |
216
|
|
|
$this->httpClientParams = array_merge($this->httpClientParams, $params); |
217
|
|
|
|
218
|
40 |
|
return $this; |
219
|
|
|
} |
220
|
40 |
|
|
221
|
|
|
/** |
222
|
40 |
|
* Return special HttpClient params for this request. |
223
|
|
|
* |
224
|
|
|
* @return array |
225
|
|
|
*/ |
226
|
|
|
public function getHttpClientParams() |
227
|
|
|
{ |
228
|
|
|
return $this->httpClientParams; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
40 |
|
* Set the headers for this request. |
233
|
|
|
* |
234
|
40 |
|
* @param array $headers |
235
|
|
|
* |
236
|
40 |
|
* @return TelegramRequest |
237
|
|
|
*/ |
238
|
|
|
public function setHeaders(array $headers) |
239
|
|
|
{ |
240
|
|
|
$this->headers = array_merge($this->headers, $headers); |
241
|
|
|
|
242
|
|
|
return $this; |
243
|
|
|
} |
244
|
40 |
|
|
245
|
|
|
/** |
246
|
40 |
|
* Return the headers for this request. |
247
|
|
|
* |
248
|
|
|
* @return array |
249
|
|
|
*/ |
250
|
|
|
public function getHeaders() |
251
|
|
|
{ |
252
|
|
|
$headers = $this->getDefaultHeaders(); |
253
|
|
|
|
254
|
40 |
|
return array_merge($this->headers, $headers); |
255
|
|
|
} |
256
|
40 |
|
|
257
|
40 |
|
/** |
258
|
|
|
* Make this request asynchronous (non-blocking). |
259
|
|
|
* |
260
|
|
|
* @param $isAsyncRequest |
261
|
|
|
* |
262
|
|
|
* @return TelegramRequest |
263
|
|
|
*/ |
264
|
|
|
public function setAsyncRequest($isAsyncRequest) |
265
|
|
|
{ |
266
|
|
|
$this->isAsyncRequest = $isAsyncRequest; |
267
|
|
|
|
268
|
40 |
|
return $this; |
269
|
|
|
} |
270
|
|
|
|
271
|
40 |
|
/** |
272
|
40 |
|
* Check if this is an asynchronous request (non-blocking). |
273
|
|
|
* |
274
|
|
|
* @return bool |
275
|
|
|
*/ |
276
|
|
|
public function isAsyncRequest() |
277
|
|
|
{ |
278
|
|
|
return $this->isAsyncRequest; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* Only return params on POST requests. |
283
|
|
|
* |
284
|
|
|
* @return array |
285
|
|
|
*/ |
286
|
|
|
public function getPostParams() |
287
|
|
|
{ |
288
|
|
|
if ($this->getMethod() === 'POST') { |
289
|
|
|
return $this->getParams(); |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
return []; |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* The default headers used with every request. |
297
|
|
|
* |
298
|
|
|
* @return array |
299
|
|
|
*/ |
300
|
|
|
public function getDefaultHeaders() |
301
|
|
|
{ |
302
|
|
|
return [ |
303
|
|
|
'User-Agent' => 'Telegram Bot PHP SDK v'.Api::VERSION.' - (https://github.com/irazasyed/telegram-bot-sdk)', |
304
|
|
|
]; |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* Filter parameters with prefix (keep or remove them from array). |
309
|
|
|
* |
310
|
|
|
* @param array $params |
311
|
|
|
* @param string $prefix |
312
|
|
|
* @param bool|true $keep |
313
|
|
|
* |
314
|
|
|
* @return array |
315
|
|
|
*/ |
316
|
|
|
private static function filterParams(array $params, $prefix, $keep = true) |
317
|
|
|
{ |
318
|
|
|
$mainKey = array_keys($params)[0]; |
319
|
|
|
$result = []; |
320
|
|
|
foreach ($params[$mainKey] as $key => $value) { |
321
|
|
|
$pos = strpos($key, $prefix); |
322
|
|
|
if ($keep && $pos === 0) { |
323
|
|
|
$key = substr($key, strlen($prefix)); |
324
|
|
|
$result[$key] = $value; |
325
|
|
|
} elseif (!$keep && $pos !== 0) { |
326
|
|
|
$result[$key] = $value; |
327
|
|
|
} |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
return $keep ? $result : [$mainKey => $result]; |
331
|
|
|
} |
332
|
|
|
} |
333
|
|
|
|
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.