1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the TelegramBot package. |
5
|
|
|
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]> |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Longman\TelegramBot\Entities; |
11
|
|
|
|
12
|
|
|
use Longman\TelegramBot\Entities\ChatMember\ChatMember; |
13
|
|
|
use Longman\TelegramBot\Entities\ChatMember\Factory as ChatMemberFactory; |
14
|
|
|
use Longman\TelegramBot\Entities\Games\GameHighScore; |
15
|
|
|
use Longman\TelegramBot\Entities\MenuButton\Factory as MenuButtonFactory; |
16
|
|
|
use Longman\TelegramBot\Request; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class ServerResponse |
20
|
|
|
* |
21
|
|
|
* @link https://core.telegram.org/bots/api#making-requests |
22
|
|
|
* |
23
|
|
|
* @method bool getOk() If the request was successful |
24
|
|
|
* @method mixed getResult() The result of the query |
25
|
|
|
* @method int getErrorCode() Error code of the unsuccessful request |
26
|
|
|
* @method string getDescription() Human-readable description of the result / unsuccessful request |
27
|
|
|
* |
28
|
|
|
* @todo method ResponseParameters getParameters() Field which can help to automatically handle the error |
29
|
|
|
*/ |
30
|
|
|
class ServerResponse extends Entity |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* ServerResponse constructor. |
34
|
|
|
* |
35
|
|
|
* @param array $data |
36
|
|
|
* @param string $bot_username |
37
|
|
|
*/ |
38
|
11 |
|
public function __construct(array $data, string $bot_username = '') |
39
|
|
|
{ |
40
|
11 |
|
$is_ok = (bool) ($data['ok'] ?? false); |
41
|
11 |
|
$result = $data['result'] ?? null; |
42
|
|
|
|
43
|
11 |
|
if ($is_ok && is_array($result)) { |
44
|
7 |
|
if ($this->isAssoc($result)) { |
45
|
5 |
|
$data['result'] = $this->createResultObject($result, $bot_username); |
46
|
|
|
} else { |
47
|
2 |
|
$data['result'] = $this->createResultObjects($result, $bot_username); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
11 |
|
parent::__construct($data, $bot_username); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Check if array is associative |
56
|
|
|
* |
57
|
|
|
* @link https://stackoverflow.com/a/4254008 |
58
|
|
|
* |
59
|
|
|
* @param array $array |
60
|
|
|
* |
61
|
|
|
* @return bool |
62
|
|
|
*/ |
63
|
7 |
|
protected function isAssoc(array $array): bool |
64
|
|
|
{ |
65
|
7 |
|
return count(array_filter(array_keys($array), 'is_string')) > 0; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* If response is ok |
70
|
|
|
* |
71
|
|
|
* @return bool |
72
|
|
|
*/ |
73
|
6 |
|
public function isOk(): bool |
74
|
|
|
{ |
75
|
6 |
|
return (bool) $this->getOk(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Print error |
80
|
|
|
* |
81
|
|
|
* @see https://secure.php.net/manual/en/function.print-r.php |
82
|
|
|
* |
83
|
|
|
* @param bool $return |
84
|
|
|
* |
85
|
|
|
* @return bool|string |
86
|
|
|
*/ |
87
|
|
|
public function printError($return = false) |
88
|
|
|
{ |
89
|
|
|
$error = sprintf('Error N: %s, Description: %s', $this->getErrorCode(), $this->getDescription()); |
90
|
|
|
|
91
|
|
|
if ($return) { |
92
|
|
|
return $error; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
echo $error; |
96
|
|
|
|
97
|
|
|
return true; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Create and return the object of the received result |
102
|
|
|
* |
103
|
|
|
* @param array $result |
104
|
|
|
* @param string $bot_username |
105
|
|
|
* |
106
|
|
|
* @return BotDescription|BotName|BotShortDescription|Chat|ChatAdministratorRights|ChatMember|File|Message|MenuButton|Poll|SentWebAppMessage|StickerSet|User|UserProfilePhotos|WebhookInfo |
107
|
|
|
*/ |
108
|
5 |
|
private function createResultObject(array $result, string $bot_username): Entity |
109
|
|
|
{ |
110
|
5 |
|
$result_object_types = [ |
111
|
5 |
|
'getWebhookInfo' => WebhookInfo::class, |
112
|
5 |
|
'getMe' => User::class, |
113
|
5 |
|
'getUserProfilePhotos' => UserProfilePhotos::class, |
114
|
5 |
|
'getFile' => File::class, |
115
|
5 |
|
'getChat' => Chat::class, |
116
|
5 |
|
'getChatMember' => ChatMemberFactory::class, |
117
|
5 |
|
'getMyName' => BotName::class, |
118
|
5 |
|
'getMyDescription' => BotDescription::class, |
119
|
5 |
|
'getMyShortDescription' => BotShortDescription::class, |
120
|
5 |
|
'getChatMenuButton' => MenuButtonFactory::class, |
121
|
5 |
|
'getMyDefaultAdministratorRights' => ChatAdministratorRights::class, |
122
|
5 |
|
'getStickerSet' => StickerSet::class, |
123
|
5 |
|
'stopPoll' => Poll::class, |
124
|
5 |
|
'answerWebAppQuery' => SentWebAppMessage::class, |
125
|
5 |
|
]; |
126
|
|
|
|
127
|
5 |
|
$action = Request::getCurrentAction(); |
128
|
5 |
|
$object_class = $result_object_types[$action] ?? Message::class; |
129
|
|
|
|
130
|
5 |
|
return Factory::resolveEntityClass($object_class, $result, $bot_username); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Create and return the objects array of the received result |
135
|
|
|
* |
136
|
|
|
* @param array $results |
137
|
|
|
* @param string $bot_username |
138
|
|
|
* |
139
|
|
|
* @return BotCommand[]|ChatMember[]|GameHighScore[]|Message[]|Sticker[]|Update[] |
140
|
|
|
*/ |
141
|
2 |
|
private function createResultObjects(array $results, string $bot_username): array |
142
|
|
|
{ |
143
|
2 |
|
$result_object_types = [ |
144
|
2 |
|
'getUpdates' => Update::class, |
145
|
2 |
|
'getChatAdministrators' => ChatMemberFactory::class, |
146
|
2 |
|
'getForumTopicIconStickers' => Sticker::class, |
147
|
2 |
|
'getMyCommands' => BotCommand::class, |
148
|
2 |
|
'getCustomEmojiStickers' => Sticker::class, |
149
|
2 |
|
'getGameHighScores' => GameHighScore::class, |
150
|
2 |
|
'sendMediaGroup' => Message::class, |
151
|
2 |
|
]; |
152
|
|
|
|
153
|
2 |
|
$action = Request::getCurrentAction(); |
154
|
2 |
|
$object_class = $result_object_types[$action] ?? Update::class; |
155
|
|
|
|
156
|
2 |
|
$objects = []; |
157
|
|
|
|
158
|
2 |
|
foreach ($results as $result) { |
159
|
1 |
|
$objects[] = Factory::resolveEntityClass($object_class, $result, $bot_username); |
160
|
|
|
} |
161
|
|
|
|
162
|
2 |
|
return $objects; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|