1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Request class that communicates with Telegram's web servers. Prepares an HTML request and instantiates |
5
|
|
|
* the Response object. |
6
|
|
|
* |
7
|
|
|
* @package Teebot (Telegram bot framework) |
8
|
|
|
* |
9
|
|
|
* @author Stanislav Drozdov <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Teebot\Api; |
15
|
|
|
|
16
|
|
|
use Teebot\Api\{ |
17
|
|
|
Entity\EntityInterface, |
18
|
|
|
Method\AbstractMethod, |
19
|
|
|
Method\MethodInterface |
20
|
|
|
}; |
21
|
|
|
|
22
|
|
|
class Request |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var HttpClient $client |
26
|
|
|
*/ |
27
|
|
|
protected $httpClient; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Request constructor. |
31
|
|
|
* |
32
|
|
|
* @param HttpClient $httpClient |
33
|
|
|
*/ |
34
|
|
|
public function __construct(HttpClient $httpClient) |
35
|
|
|
{ |
36
|
|
|
$this->httpClient = $httpClient; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Executes the Request to Telegram's servers and returns Response object. |
41
|
|
|
* |
42
|
|
|
* @param MethodInterface $method Teebot method's instance to get arguments from |
43
|
|
|
* @param null|EntityInterface $parent Parent entity that initiated the Request |
44
|
|
|
* |
45
|
|
|
* @return Response |
46
|
|
|
*/ |
47
|
|
|
public function exec(MethodInterface $method, EntityInterface $parent = null): Response |
48
|
|
|
{ |
49
|
|
|
$entityClass = $method->getReturnEntity(); |
50
|
|
|
$result = $this->send($method); |
51
|
|
|
|
52
|
|
|
return $this->createResponseFromData($result, $entityClass, $parent); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Sends the request |
57
|
|
|
* |
58
|
|
|
* @param MethodInterface $methodInstance |
59
|
|
|
* |
60
|
|
|
* @return null|string |
61
|
|
|
*/ |
62
|
|
|
protected function send(MethodInterface $methodInstance): ?string |
63
|
|
|
{ |
64
|
|
|
$options = [ |
65
|
|
|
'query' => $methodInstance->getPropertiesArray(), |
|
|
|
|
66
|
|
|
]; |
67
|
|
|
|
68
|
|
|
if ($methodInstance->hasAttachedData()) { |
69
|
|
|
$options = [ |
70
|
|
|
'multipart' => $methodInstance->getPropertiesMultipart(), |
|
|
|
|
71
|
|
|
]; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $this->httpClient->request($methodInstance->getName(), $options); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Creates the Response object from received data. |
79
|
|
|
* |
80
|
|
|
* @param string $receivedData Received data from Telegram's servers |
81
|
|
|
* @param null|string $entityClass Entity class name that should be passed to Response constructor |
82
|
|
|
* @param null|EntityInterface $parent Parent entity |
83
|
|
|
* |
84
|
|
|
* @return Response |
85
|
|
|
*/ |
86
|
|
|
public function createResponseFromData( |
87
|
|
|
string $receivedData, |
88
|
|
|
?string $entityClass, |
89
|
|
|
EntityInterface $parent = null |
90
|
|
|
): Response { |
91
|
|
|
return new Response($receivedData, $entityClass, $parent); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|