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
|
|
|
class ServerResponse extends Entity |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var bool |
15
|
|
|
*/ |
16
|
|
|
protected $ok; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var null |
20
|
|
|
*/ |
21
|
|
|
protected $result; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var null |
25
|
|
|
*/ |
26
|
|
|
protected $error_code; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var null |
30
|
|
|
*/ |
31
|
|
|
protected $description; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* ServerResponse constructor. |
35
|
|
|
* |
36
|
|
|
* @param array $data |
37
|
|
|
* @param $bot_name |
38
|
|
|
* |
39
|
|
|
* @throws \Longman\TelegramBot\Exception\TelegramException |
40
|
|
|
*/ |
41
|
14 |
|
public function __construct(array $data, $bot_name) |
42
|
|
|
{ |
43
|
14 |
|
if (isset($data['ok'], $data['result'])) { |
44
|
12 |
|
if (is_array($data['result'])) { |
45
|
11 |
|
if ($data['ok'] && !$this->isAssoc($data['result']) && !isset($data['result'][0]['user'])) { |
46
|
|
|
//Get Update |
47
|
2 |
|
foreach ($data['result'] as $update) { |
48
|
2 |
|
$this->result[] = new Update($update, $bot_name); |
49
|
|
|
} |
50
|
9 |
|
} elseif ($data['ok'] && !$this->isAssoc($data['result']) && isset($data['result'][0]['user'])) { |
51
|
|
|
//Response from getChatAdministrators |
52
|
|
|
$this->result = []; |
|
|
|
|
53
|
|
|
foreach ($data['result'] as $user) { |
54
|
|
|
$this->result[] = new ChatMember($user); |
55
|
|
|
} |
56
|
9 |
|
} elseif ($data['ok'] && $this->isAssoc($data['result'])) { |
57
|
9 |
|
if (isset($data['result']['total_count'])) { |
58
|
|
|
//Response from getUserProfilePhotos |
59
|
1 |
|
$this->result = new UserProfilePhotos($data['result']); |
|
|
|
|
60
|
8 |
|
} elseif (isset($data['result']['file_id'])) { |
61
|
|
|
//Response from getFile |
62
|
1 |
|
$this->result = new File($data['result']); |
|
|
|
|
63
|
7 |
|
} elseif (isset($data['result']['username'])) { |
64
|
|
|
//Response from getMe |
65
|
|
|
$this->result = new User($data['result']); |
|
|
|
|
66
|
7 |
|
} elseif (isset($data['result']['id'])) { |
67
|
|
|
//Response from getChat |
68
|
|
|
$this->result = new Chat($data['result']); |
|
|
|
|
69
|
7 |
|
} elseif (isset($data['result']['user'])) { |
70
|
|
|
//Response from getChatMember |
71
|
|
|
$this->result = new ChatMember($data['result']); |
|
|
|
|
72
|
7 |
|
} elseif (isset($data['result']['has_custom_certificate'])) { |
73
|
|
|
//Response from getWebhookInfo |
74
|
|
|
$this->result = new WebhookInfo($data['result']); |
|
|
|
|
75
|
|
|
} else { |
76
|
|
|
//Response from sendMessage |
77
|
7 |
|
$this->result = new Message($data['result'], $bot_name); |
|
|
|
|
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
11 |
|
$this->ok = $data['ok']; |
82
|
11 |
|
$this->error_code = null; |
83
|
11 |
|
$this->description = null; |
84
|
|
|
} else { |
85
|
2 |
|
if ($data['ok'] && $data['result'] === true) { |
86
|
|
|
//Response from setWebhook set |
87
|
2 |
|
$this->ok = $data['ok']; |
88
|
2 |
|
$this->result = true; |
|
|
|
|
89
|
2 |
|
$this->error_code = null; |
90
|
2 |
|
$this->description = isset($data['description']) ? $data['description'] : ''; |
91
|
|
|
} elseif (is_numeric($data['result'])) { |
92
|
|
|
//Response from getChatMembersCount |
93
|
|
|
$this->result = $data['result']; |
|
|
|
|
94
|
|
|
} else { |
95
|
|
|
$this->ok = false; |
96
|
|
|
$this->result = null; |
97
|
|
|
$this->error_code = $data['error_code']; |
98
|
12 |
|
$this->description = $data['description']; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
} else { |
102
|
|
|
//webHook not set |
103
|
2 |
|
$this->ok = false; |
104
|
2 |
|
$this->result = isset($data['result']) ? $data['result'] : null; |
105
|
2 |
|
$this->error_code = isset($data['error_code']) ? $data['error_code'] : null; |
106
|
2 |
|
$this->description = isset($data['description']) ? $data['description'] : null; |
107
|
|
|
|
108
|
|
|
//throw new TelegramException('ok(variable) is not set!'); |
|
|
|
|
109
|
|
|
} |
110
|
14 |
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Check if array is associative |
114
|
|
|
* |
115
|
|
|
* @param array $array |
116
|
|
|
* @return bool |
117
|
|
|
*/ |
118
|
11 |
|
protected function isAssoc(array $array) |
119
|
|
|
{ |
120
|
11 |
|
return (bool) count(array_filter(array_keys($array), 'is_string')); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* If response is ok |
125
|
|
|
* |
126
|
|
|
* @return bool |
127
|
|
|
*/ |
128
|
5 |
|
public function isOk() |
129
|
|
|
{ |
130
|
5 |
|
return $this->ok; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Get result |
135
|
|
|
* |
136
|
|
|
* @return string |
137
|
|
|
*/ |
138
|
14 |
|
public function getResult() |
139
|
|
|
{ |
140
|
14 |
|
return $this->result; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Get error code |
145
|
|
|
* |
146
|
|
|
* @return string |
147
|
|
|
*/ |
148
|
5 |
|
public function getErrorCode() |
149
|
|
|
{ |
150
|
5 |
|
return $this->error_code; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Get description |
155
|
|
|
* |
156
|
|
|
* @return null |
157
|
|
|
*/ |
158
|
5 |
|
public function getDescription() |
159
|
|
|
{ |
160
|
5 |
|
return $this->description; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Print error |
165
|
|
|
* |
166
|
|
|
* @return string |
167
|
|
|
*/ |
168
|
|
|
public function printError() |
169
|
|
|
{ |
170
|
|
|
return 'Error N: ' . $this->getErrorCode() . ' Description: ' . $this->getDescription(); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..