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\Entities; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class ServerResponse |
13
|
|
|
* |
14
|
|
|
* @link https://core.telegram.org/bots/api#making-requests |
15
|
|
|
* |
16
|
|
|
* @method bool getOk() If the request was successful |
17
|
|
|
* @method mixed getResult() The result of the query |
18
|
|
|
* @method int getErrorCode() Error code of the unsuccessful request |
19
|
|
|
* @method string getDescription() Human-readable description of the result / unsuccessful request |
20
|
|
|
* |
21
|
|
|
* @todo method ResponseParameters getParameters() Field which can help to automatically handle the error |
22
|
|
|
*/ |
23
|
|
|
class ServerResponse extends Entity |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* ServerResponse constructor. |
27
|
|
|
* |
28
|
|
|
* @param array $data |
29
|
|
|
* @param string $bot_username |
30
|
|
|
* |
31
|
|
|
* @throws \Longman\TelegramBot\Exception\TelegramException |
32
|
|
|
*/ |
33
|
9 |
|
public function __construct(array $data, $bot_username) |
34
|
|
|
{ |
35
|
|
|
// Make sure we don't double-save the raw_data |
36
|
9 |
|
unset($data['raw_data']); |
37
|
9 |
|
$data['raw_data'] = $data; |
38
|
|
|
|
39
|
9 |
|
$is_ok = isset($data['ok']) ? (bool) $data['ok'] : false; |
40
|
9 |
|
$result = isset($data['result']) ? $data['result'] : null; |
41
|
|
|
|
42
|
9 |
|
if ($is_ok && is_array($result)) { |
43
|
6 |
|
if ($this->isAssoc($result)) { |
44
|
4 |
|
$data['result'] = $this->createResultObject($result, $bot_username); |
45
|
|
|
} else { |
46
|
2 |
|
$data['result'] = $this->createResultObjects($result, $bot_username); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
9 |
|
parent::__construct($data, $bot_username); |
51
|
9 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Check if array is associative |
55
|
|
|
* |
56
|
|
|
* @link https://stackoverflow.com/a/4254008 |
57
|
|
|
* |
58
|
|
|
* @param array $array |
59
|
|
|
* |
60
|
|
|
* @return bool |
61
|
|
|
*/ |
62
|
6 |
|
protected function isAssoc(array $array) |
63
|
|
|
{ |
64
|
6 |
|
return count(array_filter(array_keys($array), 'is_string')) > 0; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* If response is ok |
69
|
|
|
* |
70
|
|
|
* @return bool |
71
|
|
|
*/ |
72
|
5 |
|
public function isOk() |
73
|
|
|
{ |
74
|
5 |
|
return (bool) $this->getOk(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Print error |
79
|
|
|
* |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
|
|
public function printError() |
83
|
|
|
{ |
84
|
|
|
return 'Error N: ' . $this->getErrorCode() . ' Description: ' . $this->getDescription(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Create and return the object of the received result |
89
|
|
|
* |
90
|
|
|
* @param array $result |
91
|
|
|
* @param string $bot_username |
92
|
|
|
* |
93
|
|
|
* @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 |
94
|
|
|
* @throws \Longman\TelegramBot\Exception\TelegramException |
95
|
|
|
*/ |
96
|
4 |
|
private function createResultObject($result, $bot_username) |
97
|
|
|
{ |
98
|
|
|
// We don't need to save the raw_data of the response object! |
99
|
4 |
|
$result['raw_data'] = null; |
100
|
|
|
|
101
|
|
|
$result_object_types = [ |
102
|
4 |
|
'total_count' => 'UserProfilePhotos', //Response from getUserProfilePhotos |
103
|
|
|
'file_id' => 'File', //Response from getFile |
104
|
|
|
'title' => 'Chat', //Response from getChat |
105
|
|
|
'username' => 'User', //Response from getMe |
106
|
|
|
'user' => 'ChatMember', //Response from getChatMember |
107
|
|
|
'url' => 'WebhookInfo', //Response from getWebhookInfo |
108
|
|
|
]; |
109
|
4 |
|
foreach ($result_object_types as $type => $object_class) { |
110
|
4 |
|
if (isset($result[$type])) { |
111
|
2 |
|
$object_class = __NAMESPACE__ . '\\' . $object_class; |
112
|
|
|
|
113
|
2 |
|
return new $object_class($result); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
//Response from sendMessage |
118
|
2 |
|
return new Message($result, $bot_username); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Create and return the objects array of the received result |
123
|
|
|
* |
124
|
|
|
* @param array $result |
125
|
|
|
* @param string $bot_username |
126
|
|
|
* |
127
|
|
|
* @return null|\Longman\TelegramBot\Entities\ChatMember[]|\Longman\TelegramBot\Entities\Update[] |
128
|
|
|
* @throws \Longman\TelegramBot\Exception\TelegramException |
129
|
|
|
*/ |
130
|
2 |
|
private function createResultObjects($result, $bot_username) |
131
|
|
|
{ |
132
|
2 |
|
$results = []; |
133
|
2 |
|
if (isset($result[0]['user'])) { |
134
|
|
|
//Response from getChatAdministrators |
135
|
|
|
foreach ($result as $user) { |
136
|
|
|
// We don't need to save the raw_data of the response object! |
137
|
|
|
$user['raw_data'] = null; |
138
|
|
|
|
139
|
|
|
$results[] = new ChatMember($user); |
140
|
|
|
} |
141
|
|
|
} else { |
142
|
|
|
//Get Update |
143
|
2 |
|
foreach ($result as $update) { |
144
|
|
|
// We don't need to save the raw_data of the response object! |
145
|
1 |
|
$update['raw_data'] = null; |
146
|
|
|
|
147
|
1 |
|
$results[] = new Update($update, $bot_username); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
2 |
|
return $results; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|