1 | <?php |
||
35 | abstract class Command |
||
36 | { |
||
37 | /** |
||
38 | * Telegram object |
||
39 | * |
||
40 | * @var \Longman\TelegramBot\Telegram |
||
41 | */ |
||
42 | protected $telegram; |
||
43 | |||
44 | /** |
||
45 | * Update object |
||
46 | * |
||
47 | * @var \Longman\TelegramBot\Entities\Update |
||
48 | */ |
||
49 | protected $update; |
||
50 | |||
51 | /** |
||
52 | * Name |
||
53 | * |
||
54 | * @var string |
||
55 | */ |
||
56 | protected $name = ''; |
||
57 | |||
58 | /** |
||
59 | * Description |
||
60 | * |
||
61 | * @var string |
||
62 | */ |
||
63 | protected $description = 'Command description'; |
||
64 | |||
65 | /** |
||
66 | * Usage |
||
67 | * |
||
68 | * @var string |
||
69 | */ |
||
70 | protected $usage = 'Command usage'; |
||
71 | |||
72 | /** |
||
73 | * Show in Help |
||
74 | * |
||
75 | * @var bool |
||
76 | */ |
||
77 | protected $show_in_help = true; |
||
78 | |||
79 | /** |
||
80 | * Version |
||
81 | * |
||
82 | * @var string |
||
83 | */ |
||
84 | protected $version = '1.0.0'; |
||
85 | |||
86 | /** |
||
87 | * If this command is enabled |
||
88 | * |
||
89 | * @var boolean |
||
90 | */ |
||
91 | protected $enabled = true; |
||
92 | |||
93 | /** |
||
94 | * If this command needs mysql |
||
95 | * |
||
96 | * @var boolean |
||
97 | */ |
||
98 | protected $need_mysql = false; |
||
99 | |||
100 | /** |
||
101 | * Command config |
||
102 | * |
||
103 | * @var array |
||
104 | */ |
||
105 | protected $config = []; |
||
106 | |||
107 | /** |
||
108 | * Constructor |
||
109 | * |
||
110 | * @param \Longman\TelegramBot\Telegram $telegram |
||
111 | * @param \Longman\TelegramBot\Entities\Update $update |
||
112 | */ |
||
113 | 15 | public function __construct(Telegram $telegram, Update $update = null) |
|
119 | |||
120 | /** |
||
121 | * Set update object |
||
122 | * |
||
123 | * @param \Longman\TelegramBot\Entities\Update $update |
||
124 | * |
||
125 | * @return \Longman\TelegramBot\Commands\Command |
||
126 | */ |
||
127 | 15 | public function setUpdate(Update $update = null) |
|
128 | { |
||
129 | 15 | if ($update !== null) { |
|
130 | 1 | $this->update = $update; |
|
131 | } |
||
132 | |||
133 | 15 | return $this; |
|
134 | } |
||
135 | |||
136 | /** |
||
137 | * Pre-execute command |
||
138 | * |
||
139 | * @return \Longman\TelegramBot\Entities\ServerResponse |
||
140 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
141 | */ |
||
142 | public function preExecute() |
||
150 | |||
151 | /** |
||
152 | * Execute command |
||
153 | * |
||
154 | * @return \Longman\TelegramBot\Entities\ServerResponse |
||
155 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
156 | */ |
||
157 | abstract public function execute(); |
||
158 | |||
159 | /** |
||
160 | * Execution if MySQL is required but not available |
||
161 | * |
||
162 | * @return \Longman\TelegramBot\Entities\ServerResponse |
||
163 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
164 | */ |
||
165 | public function executeNoDb() |
||
178 | |||
179 | /** |
||
180 | * Get update object |
||
181 | * |
||
182 | * @return \Longman\TelegramBot\Entities\Update |
||
183 | */ |
||
184 | 1 | public function getUpdate() |
|
188 | |||
189 | /** |
||
190 | * Relay any non-existing function calls to Update object. |
||
191 | * |
||
192 | * This is purely a helper method to make requests from within execute() method easier. |
||
193 | * |
||
194 | * @param string $name |
||
195 | * @param array $arguments |
||
196 | * |
||
197 | * @return Command |
||
198 | */ |
||
199 | 1 | public function __call($name, array $arguments) |
|
206 | |||
207 | /** |
||
208 | * Get command config |
||
209 | * |
||
210 | * Look for config $name if found return it, if not return null. |
||
211 | * If $name is not set return all set config. |
||
212 | * |
||
213 | * @param string|null $name |
||
214 | * |
||
215 | * @return array|mixed|null |
||
216 | */ |
||
217 | 1 | public function getConfig($name = null) |
|
228 | |||
229 | /** |
||
230 | * Get telegram object |
||
231 | * |
||
232 | * @return \Longman\TelegramBot\Telegram |
||
233 | */ |
||
234 | 1 | public function getTelegram() |
|
238 | |||
239 | /** |
||
240 | * Get usage |
||
241 | * |
||
242 | * @return string |
||
243 | */ |
||
244 | 1 | public function getUsage() |
|
248 | |||
249 | /** |
||
250 | * Get version |
||
251 | * |
||
252 | * @return string |
||
253 | */ |
||
254 | 1 | public function getVersion() |
|
258 | |||
259 | /** |
||
260 | * Get description |
||
261 | * |
||
262 | * @return string |
||
263 | */ |
||
264 | 1 | public function getDescription() |
|
268 | |||
269 | /** |
||
270 | * Get name |
||
271 | * |
||
272 | * @return string |
||
273 | */ |
||
274 | 1 | public function getName() |
|
278 | |||
279 | /** |
||
280 | * Get Show in Help |
||
281 | * |
||
282 | * @return bool |
||
283 | */ |
||
284 | 1 | public function showInHelp() |
|
288 | |||
289 | /** |
||
290 | * Check if command is enabled |
||
291 | * |
||
292 | * @return bool |
||
293 | */ |
||
294 | 1 | public function isEnabled() |
|
298 | |||
299 | /** |
||
300 | * If this is a SystemCommand |
||
301 | * |
||
302 | * @return bool |
||
303 | */ |
||
304 | public function isSystemCommand() |
||
308 | |||
309 | /** |
||
310 | * If this is an AdminCommand |
||
311 | * |
||
312 | * @return bool |
||
313 | */ |
||
314 | public function isAdminCommand() |
||
318 | |||
319 | /** |
||
320 | * If this is a UserCommand |
||
321 | * |
||
322 | * @return bool |
||
323 | */ |
||
324 | public function isUserCommand() |
||
328 | } |
||
329 |