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