@@ -42,7 +42,7 @@ |
||
42 | 42 | $data['raw_data'] = $data; |
43 | 43 | $this->response = $data; |
44 | 44 | |
45 | - $is_ok = (bool)($data['ok'] ?? false); |
|
45 | + $is_ok = (bool) ($data['ok'] ?? false); |
|
46 | 46 | $result = $data['result'] ?? null; |
47 | 47 | |
48 | 48 | if ($is_ok) { |
@@ -37,7 +37,7 @@ |
||
37 | 37 | |
38 | 38 | if ($these_photos = $this->getProperty('photos')) { |
39 | 39 | foreach ($these_photos as $photos) { |
40 | - $all_photos[] = array_map(function ($photo) { |
|
40 | + $all_photos[] = array_map(function($photo) { |
|
41 | 41 | return new PhotoSize($photo); |
42 | 42 | }, $photos); |
43 | 43 | } |
@@ -50,7 +50,9 @@ |
||
50 | 50 | */ |
51 | 51 | protected function getEnvToken(string $file): string|null |
52 | 52 | { |
53 | - if (!file_exists($file)) return null; |
|
53 | + if (!file_exists($file)) { |
|
54 | + return null; |
|
55 | + } |
|
54 | 56 | return $_ENV['TELEGRAM_BOT_TOKEN'] ?? null; |
55 | 57 | } |
56 | 58 |
@@ -27,7 +27,7 @@ discard block |
||
27 | 27 | * |
28 | 28 | * @return string|null |
29 | 29 | */ |
30 | - protected static function guessEnvPath(): string|null |
|
30 | + protected static function guessEnvPath(): string|null |
|
31 | 31 | { |
32 | 32 | $defaultEnvPaths = [ |
33 | 33 | getcwd() . '/.env', |
@@ -45,10 +45,10 @@ discard block |
||
45 | 45 | } |
46 | 46 | |
47 | 47 | protected static function tryAutoloadEnv(): void { |
48 | - $envPath = static::guessEnvPath(); |
|
49 | - if ($envPath !== null) { |
|
48 | + $envPath = static::guessEnvPath(); |
|
49 | + if ($envPath !== null) { |
|
50 | 50 | (new Dotenv())->load($envPath); |
51 | - } |
|
51 | + } |
|
52 | 52 | } |
53 | 53 | |
54 | 54 | /** |
@@ -107,7 +107,9 @@ discard block |
||
107 | 107 | public function fetchWith(UpdateHandler $update_handler, Update|null $update = null): void |
108 | 108 | { |
109 | 109 | if (is_subclass_of($update_handler, UpdateHandler::class)) { |
110 | - if ($update === null) $update = self::getUpdate(); |
|
110 | + if ($update === null) { |
|
111 | + $update = self::getUpdate(); |
|
112 | + } |
|
111 | 113 | $update_handler->resolve($update); |
112 | 114 | } |
113 | 115 | } |
@@ -120,7 +122,9 @@ discard block |
||
120 | 122 | public static function getUpdate(): Update|null |
121 | 123 | { |
122 | 124 | $input = self::getInput(); |
123 | - if (empty($input)) return null; |
|
125 | + if (empty($input)) { |
|
126 | + return null; |
|
127 | + } |
|
124 | 128 | return Telegram::processUpdate($input, self::getApiToken()); |
125 | 129 | } |
126 | 130 |
@@ -115,7 +115,7 @@ |
||
115 | 115 | { |
116 | 116 | // Get the correct logger instance. |
117 | 117 | $logger = null; |
118 | - if (in_array($name, ['emergency', 'alert', 'critical', 'error', 'warning', 'notice', 'info', 'debug',], true)) { |
|
118 | + if (in_array($name, ['emergency', 'alert', 'critical', 'error', 'warning', 'notice', 'info', 'debug', ], true)) { |
|
119 | 119 | $logger = self::$logger; |
120 | 120 | } elseif ($name === 'update') { |
121 | 121 | $logger = self::$update_logger; |
@@ -17,237 +17,237 @@ |
||
17 | 17 | */ |
18 | 18 | class UpdateHandler extends Telegram implements HandlerInterface { |
19 | 19 | |
20 | - use HandlerTrait; |
|
21 | - |
|
22 | - /** |
|
23 | - * @var ?Update |
|
24 | - */ |
|
25 | - protected ?Update $update; |
|
26 | - |
|
27 | - /** |
|
28 | - * @var Plugin[] |
|
29 | - */ |
|
30 | - private array $plugins = []; |
|
31 | - |
|
32 | - /** |
|
33 | - * @var bool |
|
34 | - */ |
|
35 | - private bool $active_spreader = false; |
|
36 | - |
|
37 | - /** |
|
38 | - * The default configuration of the webhook. |
|
39 | - * |
|
40 | - * @var array |
|
41 | - */ |
|
42 | - private array $config = [ |
|
43 | - 'autoload_env_file' => false, |
|
44 | - 'env_file_path' => null, |
|
45 | - ]; |
|
46 | - |
|
47 | - /** |
|
48 | - * Filter incoming updates. |
|
49 | - * |
|
50 | - * @var array |
|
51 | - */ |
|
52 | - private array $filterIncomingUpdates = []; |
|
53 | - |
|
54 | - /** |
|
55 | - * Webhook constructor. |
|
56 | - * |
|
57 | - * @param string $api_token The API key of the bot. Leave it blank for autoload from .env file. |
|
58 | - */ |
|
59 | - public function __construct(string $api_token = '') { |
|
60 | - parent::__construct($api_token); |
|
61 | - |
|
62 | - if (!Telegram::validateToken(self::getApiToken())) { |
|
63 | - throw new InvalidBotTokenException(); |
|
64 | - } |
|
65 | - } |
|
66 | - |
|
67 | - /** |
|
68 | - * Resolve the request on single plugin. |
|
69 | - * |
|
70 | - * @param Plugin $plugin The plugin to work with |
|
71 | - * @param ?Update $update The custom to work with |
|
72 | - * @param array $config The configuration of the receiver |
|
73 | - * @return void |
|
74 | - */ |
|
75 | - public static function resolveOn(Plugin $plugin, Update $update = null, array $config = []): void { |
|
76 | - // TODO: Implement resolveOn() method. |
|
77 | - } |
|
78 | - |
|
79 | - /** |
|
80 | - * Add plugins to the receiver |
|
81 | - * |
|
82 | - * @param Plugin[]|array $plugins |
|
83 | - * @retrun void |
|
84 | - */ |
|
85 | - public function addPlugins(Plugin|array $plugins): UpdateHandler { |
|
86 | - if (is_object($plugins)) { |
|
87 | - $plugins = [$plugins]; |
|
88 | - } |
|
89 | - |
|
90 | - foreach ($plugins as $plugin) { |
|
91 | - if (!is_subclass_of($plugin, Plugin::class)) { |
|
20 | + use HandlerTrait; |
|
21 | + |
|
22 | + /** |
|
23 | + * @var ?Update |
|
24 | + */ |
|
25 | + protected ?Update $update; |
|
26 | + |
|
27 | + /** |
|
28 | + * @var Plugin[] |
|
29 | + */ |
|
30 | + private array $plugins = []; |
|
31 | + |
|
32 | + /** |
|
33 | + * @var bool |
|
34 | + */ |
|
35 | + private bool $active_spreader = false; |
|
36 | + |
|
37 | + /** |
|
38 | + * The default configuration of the webhook. |
|
39 | + * |
|
40 | + * @var array |
|
41 | + */ |
|
42 | + private array $config = [ |
|
43 | + 'autoload_env_file' => false, |
|
44 | + 'env_file_path' => null, |
|
45 | + ]; |
|
46 | + |
|
47 | + /** |
|
48 | + * Filter incoming updates. |
|
49 | + * |
|
50 | + * @var array |
|
51 | + */ |
|
52 | + private array $filterIncomingUpdates = []; |
|
53 | + |
|
54 | + /** |
|
55 | + * Webhook constructor. |
|
56 | + * |
|
57 | + * @param string $api_token The API key of the bot. Leave it blank for autoload from .env file. |
|
58 | + */ |
|
59 | + public function __construct(string $api_token = '') { |
|
60 | + parent::__construct($api_token); |
|
61 | + |
|
62 | + if (!Telegram::validateToken(self::getApiToken())) { |
|
63 | + throw new InvalidBotTokenException(); |
|
64 | + } |
|
65 | + } |
|
66 | + |
|
67 | + /** |
|
68 | + * Resolve the request on single plugin. |
|
69 | + * |
|
70 | + * @param Plugin $plugin The plugin to work with |
|
71 | + * @param ?Update $update The custom to work with |
|
72 | + * @param array $config The configuration of the receiver |
|
73 | + * @return void |
|
74 | + */ |
|
75 | + public static function resolveOn(Plugin $plugin, Update $update = null, array $config = []): void { |
|
76 | + // TODO: Implement resolveOn() method. |
|
77 | + } |
|
78 | + |
|
79 | + /** |
|
80 | + * Add plugins to the receiver |
|
81 | + * |
|
82 | + * @param Plugin[]|array $plugins |
|
83 | + * @retrun void |
|
84 | + */ |
|
85 | + public function addPlugins(Plugin|array $plugins): UpdateHandler { |
|
86 | + if (is_object($plugins)) { |
|
87 | + $plugins = [$plugins]; |
|
88 | + } |
|
89 | + |
|
90 | + foreach ($plugins as $plugin) { |
|
91 | + if (!is_subclass_of($plugin, Plugin::class)) { |
|
92 | 92 | throw new \RuntimeException( |
93 | - sprintf('The plugin %s must be an instance of %s', get_class($plugin), Plugin::class) |
|
93 | + sprintf('The plugin %s must be an instance of %s', get_class($plugin), Plugin::class) |
|
94 | 94 | ); |
95 | - } |
|
95 | + } |
|
96 | 96 | |
97 | - $reflection = Toolkit::reflectionClass($plugin); |
|
98 | - $this->plugins[] = [ |
|
97 | + $reflection = Toolkit::reflectionClass($plugin); |
|
98 | + $this->plugins[] = [ |
|
99 | 99 | 'class' => $plugin, |
100 | 100 | 'initialized' => is_object($plugin), |
101 | - ]; |
|
102 | - } |
|
103 | - |
|
104 | - return $this; |
|
105 | - } |
|
106 | - |
|
107 | - /** |
|
108 | - * Updates the filter for incoming updates. |
|
109 | - * |
|
110 | - * @param array $filter |
|
111 | - * @return void |
|
112 | - */ |
|
113 | - public function filterIncomingUpdates(array $filter): void { |
|
114 | - $this->filterIncomingUpdates = $filter; |
|
115 | - } |
|
116 | - |
|
117 | - /** |
|
118 | - * Do not process updates that match the filter. |
|
119 | - * |
|
120 | - * @param Update $update |
|
121 | - * @return bool |
|
122 | - */ |
|
123 | - private function isFiltered(Update $update): bool { |
|
124 | - if (empty($this->filterIncomingUpdates)) { |
|
125 | - return false; |
|
126 | - } |
|
127 | - |
|
128 | - foreach ($this->filterIncomingUpdates as $type => $value) { |
|
129 | - if (is_int($type)) { |
|
101 | + ]; |
|
102 | + } |
|
103 | + |
|
104 | + return $this; |
|
105 | + } |
|
106 | + |
|
107 | + /** |
|
108 | + * Updates the filter for incoming updates. |
|
109 | + * |
|
110 | + * @param array $filter |
|
111 | + * @return void |
|
112 | + */ |
|
113 | + public function filterIncomingUpdates(array $filter): void { |
|
114 | + $this->filterIncomingUpdates = $filter; |
|
115 | + } |
|
116 | + |
|
117 | + /** |
|
118 | + * Do not process updates that match the filter. |
|
119 | + * |
|
120 | + * @param Update $update |
|
121 | + * @return bool |
|
122 | + */ |
|
123 | + private function isFiltered(Update $update): bool { |
|
124 | + if (empty($this->filterIncomingUpdates)) { |
|
125 | + return false; |
|
126 | + } |
|
127 | + |
|
128 | + foreach ($this->filterIncomingUpdates as $type => $value) { |
|
129 | + if (is_int($type)) { |
|
130 | 130 | if ($update->getUpdateType() === $value) { |
131 | - return true; |
|
131 | + return true; |
|
132 | 132 | } |
133 | - } elseif (is_string($type)) { |
|
133 | + } elseif (is_string($type)) { |
|
134 | 134 | if ($update->getUpdateType() === $type) { |
135 | - if (is_callable($value)) { |
|
136 | - return $value($update); |
|
137 | - } elseif (is_bool($value)) { |
|
138 | - return $value; |
|
139 | - } |
|
140 | - throw new \InvalidArgumentException('The value of the filter must be a callable or a boolean'); |
|
135 | + if (is_callable($value)) { |
|
136 | + return $value($update); |
|
137 | + } elseif (is_bool($value)) { |
|
138 | + return $value; |
|
139 | + } |
|
140 | + throw new \InvalidArgumentException('The value of the filter must be a callable or a boolean'); |
|
141 | + } |
|
141 | 142 | } |
142 | - } |
|
143 | - throw new \InvalidArgumentException('Invalid filter'); |
|
144 | - } |
|
145 | - |
|
146 | - return false; |
|
147 | - } |
|
148 | - |
|
149 | - /** |
|
150 | - * Resolve the request. |
|
151 | - * |
|
152 | - * @param ?Update $update The custom to work with |
|
153 | - * @param array $config The configuration of the receiver |
|
154 | - * |
|
155 | - * @retrun void |
|
156 | - */ |
|
157 | - public function resolve(Update|null $update = null, array $config = []): void { |
|
158 | - $this->update = $update ?? Telegram::getUpdate(); |
|
159 | - |
|
160 | - if (empty($this->update)) { |
|
161 | - TelegramLog::error('The update is empty, the request is not processed'); |
|
162 | - return; |
|
163 | - } |
|
164 | - |
|
165 | - if ($this->isFiltered($this->update)) { |
|
166 | - TelegramLog::notice('The update is filtered, the request is not processed'); |
|
167 | - return; |
|
168 | - } |
|
169 | - |
|
170 | - if (!method_exists($this, '__process')) { |
|
171 | - throw new \RuntimeException('The method __process does not exist'); |
|
172 | - } |
|
173 | - |
|
174 | - if (is_array($config)) { |
|
175 | - $this->updateConfiguration($config); |
|
176 | - } |
|
177 | - |
|
178 | - putenv('TG_CURRENT_UPDATE=' . $this->update->getRawData(false)); |
|
179 | - |
|
180 | - $this->__process($this->update); |
|
181 | - $this->loadPlugins($this->plugins); |
|
182 | - } |
|
183 | - |
|
184 | - /** |
|
185 | - * Update the configuration |
|
186 | - * |
|
187 | - * @param array $configuration |
|
188 | - * @return void |
|
189 | - */ |
|
190 | - public function updateConfiguration(array $configuration): void { |
|
191 | - $this->config = array_merge($this->config, $configuration); |
|
192 | - } |
|
193 | - |
|
194 | - /** |
|
195 | - * Match the update with the given plugins |
|
196 | - * |
|
197 | - * @param Plugin[]|array $plugins |
|
198 | - * @return void |
|
199 | - */ |
|
200 | - private function loadPlugins(array $plugins): void { |
|
201 | - $update = $update ?? ($this->update ?? Telegram::getUpdate()); |
|
202 | - |
|
203 | - foreach ($plugins as $plugin) { |
|
204 | - if (!is_subclass_of($plugin['class'], Plugin::class)) { |
|
143 | + throw new \InvalidArgumentException('Invalid filter'); |
|
144 | + } |
|
145 | + |
|
146 | + return false; |
|
147 | + } |
|
148 | + |
|
149 | + /** |
|
150 | + * Resolve the request. |
|
151 | + * |
|
152 | + * @param ?Update $update The custom to work with |
|
153 | + * @param array $config The configuration of the receiver |
|
154 | + * |
|
155 | + * @retrun void |
|
156 | + */ |
|
157 | + public function resolve(Update|null $update = null, array $config = []): void { |
|
158 | + $this->update = $update ?? Telegram::getUpdate(); |
|
159 | + |
|
160 | + if (empty($this->update)) { |
|
161 | + TelegramLog::error('The update is empty, the request is not processed'); |
|
162 | + return; |
|
163 | + } |
|
164 | + |
|
165 | + if ($this->isFiltered($this->update)) { |
|
166 | + TelegramLog::notice('The update is filtered, the request is not processed'); |
|
167 | + return; |
|
168 | + } |
|
169 | + |
|
170 | + if (!method_exists($this, '__process')) { |
|
171 | + throw new \RuntimeException('The method __process does not exist'); |
|
172 | + } |
|
173 | + |
|
174 | + if (is_array($config)) { |
|
175 | + $this->updateConfiguration($config); |
|
176 | + } |
|
177 | + |
|
178 | + putenv('TG_CURRENT_UPDATE=' . $this->update->getRawData(false)); |
|
179 | + |
|
180 | + $this->__process($this->update); |
|
181 | + $this->loadPlugins($this->plugins); |
|
182 | + } |
|
183 | + |
|
184 | + /** |
|
185 | + * Update the configuration |
|
186 | + * |
|
187 | + * @param array $configuration |
|
188 | + * @return void |
|
189 | + */ |
|
190 | + public function updateConfiguration(array $configuration): void { |
|
191 | + $this->config = array_merge($this->config, $configuration); |
|
192 | + } |
|
193 | + |
|
194 | + /** |
|
195 | + * Match the update with the given plugins |
|
196 | + * |
|
197 | + * @param Plugin[]|array $plugins |
|
198 | + * @return void |
|
199 | + */ |
|
200 | + private function loadPlugins(array $plugins): void { |
|
201 | + $update = $update ?? ($this->update ?? Telegram::getUpdate()); |
|
202 | + |
|
203 | + foreach ($plugins as $plugin) { |
|
204 | + if (!is_subclass_of($plugin['class'], Plugin::class)) { |
|
205 | 205 | throw new \InvalidArgumentException(sprintf( |
206 | - 'The plugin %s must be an instance of %s', |
|
207 | - get_class($plugin['class']), Plugin::class |
|
206 | + 'The plugin %s must be an instance of %s', |
|
207 | + get_class($plugin['class']), Plugin::class |
|
208 | 208 | )); |
209 | - } |
|
210 | - } |
|
209 | + } |
|
210 | + } |
|
211 | 211 | |
212 | - if (!$update instanceof Update) { |
|
213 | - throw new \InvalidArgumentException(sprintf( |
|
212 | + if (!$update instanceof Update) { |
|
213 | + throw new \InvalidArgumentException(sprintf( |
|
214 | 214 | 'The update must be an instance of %s. %s given', |
215 | 215 | Update::class, gettype($update) |
216 | - )); |
|
217 | - } |
|
218 | - |
|
219 | - $this->spreadUpdateWith($update, $plugins); |
|
220 | - } |
|
221 | - |
|
222 | - /** |
|
223 | - * This function will get update and spread it to the plugins |
|
224 | - * |
|
225 | - * @param Update $update |
|
226 | - * @param array<Plugin> $plugins |
|
227 | - * @return void |
|
228 | - */ |
|
229 | - private function spreadUpdateWith(Update $update, array $plugins): void { |
|
230 | - $this->active_spreader = true; |
|
231 | - |
|
232 | - foreach ($plugins as $plugin) { |
|
233 | - if ($plugin['initialized'] === false) { |
|
216 | + )); |
|
217 | + } |
|
218 | + |
|
219 | + $this->spreadUpdateWith($update, $plugins); |
|
220 | + } |
|
221 | + |
|
222 | + /** |
|
223 | + * This function will get update and spread it to the plugins |
|
224 | + * |
|
225 | + * @param Update $update |
|
226 | + * @param array<Plugin> $plugins |
|
227 | + * @return void |
|
228 | + */ |
|
229 | + private function spreadUpdateWith(Update $update, array $plugins): void { |
|
230 | + $this->active_spreader = true; |
|
231 | + |
|
232 | + foreach ($plugins as $plugin) { |
|
233 | + if ($plugin['initialized'] === false) { |
|
234 | 234 | $plugin['class'] = new $plugin['class'](); |
235 | - } |
|
236 | - |
|
237 | - $plugin['class']->__execute($this, $update); |
|
238 | - if ($this->active_spreader === false) break; |
|
239 | - } |
|
240 | - |
|
241 | - $this->active_spreader = false; |
|
242 | - } |
|
243 | - |
|
244 | - /** |
|
245 | - * Stop the spreader process |
|
246 | - * |
|
247 | - * @return void |
|
248 | - */ |
|
249 | - public function stop(): void { |
|
250 | - $this->active_spreader = false; |
|
251 | - } |
|
235 | + } |
|
236 | + |
|
237 | + $plugin['class']->__execute($this, $update); |
|
238 | + if ($this->active_spreader === false) break; |
|
239 | + } |
|
240 | + |
|
241 | + $this->active_spreader = false; |
|
242 | + } |
|
243 | + |
|
244 | + /** |
|
245 | + * Stop the spreader process |
|
246 | + * |
|
247 | + * @return void |
|
248 | + */ |
|
249 | + public function stop(): void { |
|
250 | + $this->active_spreader = false; |
|
251 | + } |
|
252 | 252 | |
253 | 253 | } |
254 | 254 | \ No newline at end of file |
@@ -235,7 +235,9 @@ |
||
235 | 235 | } |
236 | 236 | |
237 | 237 | $plugin['class']->__execute($this, $update); |
238 | - if ($this->active_spreader === false) break; |
|
238 | + if ($this->active_spreader === false) { |
|
239 | + break; |
|
240 | + } |
|
239 | 241 | } |
240 | 242 | |
241 | 243 | $this->active_spreader = false; |
@@ -16,98 +16,98 @@ |
||
16 | 16 | */ |
17 | 17 | trait PluginTrait { |
18 | 18 | |
19 | - /** |
|
20 | - * @var UpdateHandler |
|
21 | - */ |
|
22 | - protected UpdateHandler $hook; |
|
23 | - |
|
24 | - /** |
|
25 | - * This property is used to kill the plugin when you yield a Response object. |
|
26 | - * |
|
27 | - * @var bool |
|
28 | - */ |
|
29 | - protected bool $KILL_ON_YIELD = true; |
|
30 | - |
|
31 | - /** |
|
32 | - * @var \Generator |
|
33 | - */ |
|
34 | - private \Generator $returns; |
|
35 | - |
|
36 | - /** |
|
37 | - * Execute the plugin. |
|
38 | - * |
|
39 | - * @param UpdateHandler $receiver |
|
40 | - * @param Update $update |
|
41 | - * @return void |
|
42 | - */ |
|
43 | - public function __execute(UpdateHandler $receiver, Update $update): void { |
|
44 | - $this->hook = $receiver; |
|
45 | - |
|
46 | - if (method_exists($this, '__process')) { |
|
47 | - $this->__process($update); |
|
48 | - } |
|
49 | - |
|
50 | - if (method_exists($this, 'onReceivedUpdate')) { |
|
51 | - $return = $this->onUpdate($update); |
|
52 | - $this->__checkExit($return); |
|
53 | - } |
|
54 | - |
|
55 | - $type = $this->__identify($update); |
|
56 | - if (method_exists($this, ($method = 'on' . $type)) && $type !== null) { |
|
57 | - $this->__checkExit($this->__callEvent($method, $update)); |
|
58 | - } |
|
59 | - } |
|
60 | - |
|
61 | - /** |
|
62 | - * Check for the exit of the plugin. |
|
63 | - * |
|
64 | - * @param \Generator $return |
|
65 | - * @return void |
|
66 | - */ |
|
67 | - private function __checkExit(\Generator $return): void { |
|
68 | - if ($return->valid()) { |
|
69 | - if ($return->current() !== null && $this->KILL_ON_YIELD === true) { |
|
19 | + /** |
|
20 | + * @var UpdateHandler |
|
21 | + */ |
|
22 | + protected UpdateHandler $hook; |
|
23 | + |
|
24 | + /** |
|
25 | + * This property is used to kill the plugin when you yield a Response object. |
|
26 | + * |
|
27 | + * @var bool |
|
28 | + */ |
|
29 | + protected bool $KILL_ON_YIELD = true; |
|
30 | + |
|
31 | + /** |
|
32 | + * @var \Generator |
|
33 | + */ |
|
34 | + private \Generator $returns; |
|
35 | + |
|
36 | + /** |
|
37 | + * Execute the plugin. |
|
38 | + * |
|
39 | + * @param UpdateHandler $receiver |
|
40 | + * @param Update $update |
|
41 | + * @return void |
|
42 | + */ |
|
43 | + public function __execute(UpdateHandler $receiver, Update $update): void { |
|
44 | + $this->hook = $receiver; |
|
45 | + |
|
46 | + if (method_exists($this, '__process')) { |
|
47 | + $this->__process($update); |
|
48 | + } |
|
49 | + |
|
50 | + if (method_exists($this, 'onReceivedUpdate')) { |
|
51 | + $return = $this->onUpdate($update); |
|
52 | + $this->__checkExit($return); |
|
53 | + } |
|
54 | + |
|
55 | + $type = $this->__identify($update); |
|
56 | + if (method_exists($this, ($method = 'on' . $type)) && $type !== null) { |
|
57 | + $this->__checkExit($this->__callEvent($method, $update)); |
|
58 | + } |
|
59 | + } |
|
60 | + |
|
61 | + /** |
|
62 | + * Check for the exit of the plugin. |
|
63 | + * |
|
64 | + * @param \Generator $return |
|
65 | + * @return void |
|
66 | + */ |
|
67 | + private function __checkExit(\Generator $return): void { |
|
68 | + if ($return->valid()) { |
|
69 | + if ($return->current() !== null && $this->KILL_ON_YIELD === true) { |
|
70 | 70 | if ($return->current() instanceof Response) { |
71 | - $this->stop(); |
|
71 | + $this->stop(); |
|
72 | 72 | } |
73 | - } |
|
74 | - } |
|
75 | - |
|
76 | - if ($return->valid()) { |
|
77 | - $return->next(); |
|
78 | - $this->__checkExit($return); |
|
79 | - } |
|
80 | - } |
|
81 | - |
|
82 | - /** |
|
83 | - * Identify the update type. e.g. Message, EditedMessage, etc. |
|
84 | - * |
|
85 | - * @param Update $update |
|
86 | - * @return string|null |
|
87 | - */ |
|
88 | - private function __identify(Update $update): ?string { |
|
89 | - $type = $update->getUpdateType(); |
|
90 | - |
|
91 | - if ($type === null) { |
|
92 | - return null; |
|
93 | - } |
|
94 | - |
|
95 | - return str_replace('_', '', ucwords($type, '_')); |
|
96 | - } |
|
97 | - |
|
98 | - /** |
|
99 | - * Pass data to the method. |
|
100 | - * |
|
101 | - * @param string $method The method name. |
|
102 | - * @param Update $update The update object. |
|
103 | - * @return \Generator |
|
104 | - */ |
|
105 | - private function __callEvent(string $method, Update $update): \Generator { |
|
106 | - $upperName = 'get' . ucfirst(substr($method, 2)); |
|
107 | - return match ($method) { |
|
108 | - 'onWebAppData' => $this->onWebAppData($update->getWebAppData()), |
|
109 | - default => $this->$method($update->getUpdateId(), $update->$upperName()), |
|
110 | - }; |
|
111 | - } |
|
73 | + } |
|
74 | + } |
|
75 | + |
|
76 | + if ($return->valid()) { |
|
77 | + $return->next(); |
|
78 | + $this->__checkExit($return); |
|
79 | + } |
|
80 | + } |
|
81 | + |
|
82 | + /** |
|
83 | + * Identify the update type. e.g. Message, EditedMessage, etc. |
|
84 | + * |
|
85 | + * @param Update $update |
|
86 | + * @return string|null |
|
87 | + */ |
|
88 | + private function __identify(Update $update): ?string { |
|
89 | + $type = $update->getUpdateType(); |
|
90 | + |
|
91 | + if ($type === null) { |
|
92 | + return null; |
|
93 | + } |
|
94 | + |
|
95 | + return str_replace('_', '', ucwords($type, '_')); |
|
96 | + } |
|
97 | + |
|
98 | + /** |
|
99 | + * Pass data to the method. |
|
100 | + * |
|
101 | + * @param string $method The method name. |
|
102 | + * @param Update $update The update object. |
|
103 | + * @return \Generator |
|
104 | + */ |
|
105 | + private function __callEvent(string $method, Update $update): \Generator { |
|
106 | + $upperName = 'get' . ucfirst(substr($method, 2)); |
|
107 | + return match ($method) { |
|
108 | + 'onWebAppData' => $this->onWebAppData($update->getWebAppData()), |
|
109 | + default => $this->$method($update->getUpdateId(), $update->$upperName()), |
|
110 | + }; |
|
111 | + } |
|
112 | 112 | |
113 | 113 | } |
114 | 114 | \ No newline at end of file |
@@ -60,8 +60,8 @@ |
||
60 | 60 | */ |
61 | 61 | public static function escapeMarkdown(string $string): string { |
62 | 62 | return str_replace( |
63 | - ['[', '`', '*', '_',], |
|
64 | - ['\[', '\`', '\*', '\_',], |
|
63 | + ['[', '`', '*', '_', ], |
|
64 | + ['\[', '\`', '\*', '\_', ], |
|
65 | 65 | $string |
66 | 66 | ); |
67 | 67 | } |
@@ -12,164 +12,164 @@ |
||
12 | 12 | */ |
13 | 13 | abstract class Entity { |
14 | 14 | |
15 | - /** |
|
16 | - * @var array The raw data passed to this entity |
|
17 | - */ |
|
18 | - protected array $raw_data = []; |
|
19 | - |
|
20 | - /** |
|
21 | - * Entity constructor. |
|
22 | - * |
|
23 | - * @param ?array $data The raw data passed to this entity |
|
24 | - */ |
|
25 | - public function __construct(?array $data) { |
|
26 | - if (!empty($data)) { |
|
27 | - $this->assignMemberVariables(($this->raw_data = $data)); |
|
28 | - $this->validate(); |
|
29 | - } |
|
30 | - } |
|
31 | - |
|
32 | - /** |
|
33 | - * Helper to set member variables |
|
34 | - * |
|
35 | - * @param array $data |
|
36 | - * @return void |
|
37 | - */ |
|
38 | - protected function assignMemberVariables(array $data): void { |
|
39 | - foreach ($data as $key => $value) { |
|
40 | - $this->$key = $value; |
|
41 | - } |
|
42 | - } |
|
43 | - |
|
44 | - /** |
|
45 | - * Perform any special entity validation |
|
46 | - * |
|
47 | - * @return void |
|
48 | - */ |
|
49 | - protected function validate(): void { |
|
50 | - // Do nothing by default |
|
51 | - } |
|
52 | - |
|
53 | - /** |
|
54 | - * Escape markdown (v1) special characters |
|
55 | - * |
|
56 | - * @see https://core.telegram.org/bots/api#markdown-style |
|
57 | - * |
|
58 | - * @param string $string |
|
59 | - * |
|
60 | - * @return string |
|
61 | - */ |
|
62 | - public static function escapeMarkdown(string $string): string { |
|
63 | - return str_replace( |
|
64 | - ['[', '`', '*', '_',], |
|
65 | - ['\[', '\`', '\*', '\_',], |
|
66 | - $string |
|
67 | - ); |
|
68 | - } |
|
69 | - |
|
70 | - /** |
|
71 | - * Escape markdown (v2) special characters |
|
72 | - * |
|
73 | - * @see https://core.telegram.org/bots/api#markdownv2-style |
|
74 | - * |
|
75 | - * @param string $string |
|
76 | - * |
|
77 | - * @return string |
|
78 | - */ |
|
79 | - public static function escapeMarkdownV2(string $string): string { |
|
80 | - return str_replace( |
|
81 | - ['_', '*', '[', ']', '(', ')', '~', '`', '>', '#', '+', '-', '=', '|', '{', '}', '.', '!'], |
|
82 | - ['\_', '\*', '\[', '\]', '\(', '\)', '\~', '\`', '\>', '\#', '\+', '\-', '\=', '\|', '\{', '\}', '\.', '\!'], |
|
83 | - $string |
|
84 | - ); |
|
85 | - } |
|
86 | - |
|
87 | - /** |
|
88 | - * Get the raw data passed to this entity |
|
89 | - * |
|
90 | - * @param bool $associated |
|
91 | - * @return array|string |
|
92 | - */ |
|
93 | - public function getRawData(bool $associated = true): array|string { |
|
94 | - return $associated ? $this->raw_data : json_encode($this->raw_data); |
|
95 | - } |
|
96 | - |
|
97 | - /** |
|
98 | - * Get a property from the current Entity |
|
99 | - * |
|
100 | - * @param string $property |
|
101 | - * @param mixed $default |
|
102 | - * |
|
103 | - * @return mixed |
|
104 | - */ |
|
105 | - public function getProperty(string $property, mixed $default = null): mixed { |
|
106 | - return $this->raw_data[$property] ?? $default; |
|
107 | - } |
|
108 | - |
|
109 | - /** |
|
110 | - * Get the list of the properties that are themselves Entities |
|
111 | - * |
|
112 | - * @return array |
|
113 | - */ |
|
114 | - protected function subEntities(): array { |
|
115 | - return []; |
|
116 | - } |
|
117 | - |
|
118 | - /** |
|
119 | - * Get the key value from the raw data passed to this entity |
|
120 | - * |
|
121 | - * @param string $key |
|
122 | - * @return mixed The value can be {Entity, array, string, int, float, bool, null} |
|
123 | - */ |
|
124 | - public function get(string $key): mixed { |
|
125 | - $property = $this->getProperty($key); |
|
126 | - $sub_entities = $this->subEntities() ?? []; |
|
127 | - |
|
128 | - if (isset($sub_entities[$key])) { |
|
129 | - $class_name = $sub_entities[$key]; |
|
130 | - return Factory::resolveEntityClass($class_name, $property); |
|
131 | - } |
|
132 | - |
|
133 | - return $property ?? null; |
|
134 | - } |
|
135 | - |
|
136 | - /** |
|
137 | - * Rewrite the entity values with the new data |
|
138 | - * |
|
139 | - * @param string $key |
|
140 | - * @param mixed $value |
|
141 | - * @return void |
|
142 | - */ |
|
143 | - public function set(string $key, mixed $value): void { |
|
144 | - $this->raw_data[$key] = $value; |
|
145 | - |
|
146 | - if (property_exists($this, $key)) { |
|
147 | - $this->{$key} = $value; |
|
148 | - } |
|
149 | - } |
|
150 | - |
|
151 | - /** |
|
152 | - * @param string $name The name of the property |
|
153 | - * @param array $arguments The arguments passed to the method |
|
154 | - * @return mixed |
|
155 | - */ |
|
156 | - public function __call(string $name, array $arguments): mixed { |
|
157 | - if (method_exists($this, $name)) { |
|
158 | - return $this->{$name}(...$arguments); |
|
159 | - } |
|
160 | - |
|
161 | - if (str_starts_with($name, 'get')) { |
|
162 | - $property_name = strtolower(ltrim(preg_replace('/[A-Z]/', '_$0', substr($name, 3)), '_')); |
|
163 | - return $this->get($property_name); |
|
164 | - } |
|
165 | - |
|
166 | - if (str_starts_with($name, 'set')) { |
|
167 | - $property_name = strtolower(ltrim(preg_replace('/[A-Z]/', '_$0', substr($name, 3)), '_')); |
|
168 | - $this->set($property_name, $arguments[0]); |
|
169 | - return $this; |
|
170 | - } |
|
15 | + /** |
|
16 | + * @var array The raw data passed to this entity |
|
17 | + */ |
|
18 | + protected array $raw_data = []; |
|
19 | + |
|
20 | + /** |
|
21 | + * Entity constructor. |
|
22 | + * |
|
23 | + * @param ?array $data The raw data passed to this entity |
|
24 | + */ |
|
25 | + public function __construct(?array $data) { |
|
26 | + if (!empty($data)) { |
|
27 | + $this->assignMemberVariables(($this->raw_data = $data)); |
|
28 | + $this->validate(); |
|
29 | + } |
|
30 | + } |
|
31 | + |
|
32 | + /** |
|
33 | + * Helper to set member variables |
|
34 | + * |
|
35 | + * @param array $data |
|
36 | + * @return void |
|
37 | + */ |
|
38 | + protected function assignMemberVariables(array $data): void { |
|
39 | + foreach ($data as $key => $value) { |
|
40 | + $this->$key = $value; |
|
41 | + } |
|
42 | + } |
|
43 | + |
|
44 | + /** |
|
45 | + * Perform any special entity validation |
|
46 | + * |
|
47 | + * @return void |
|
48 | + */ |
|
49 | + protected function validate(): void { |
|
50 | + // Do nothing by default |
|
51 | + } |
|
52 | + |
|
53 | + /** |
|
54 | + * Escape markdown (v1) special characters |
|
55 | + * |
|
56 | + * @see https://core.telegram.org/bots/api#markdown-style |
|
57 | + * |
|
58 | + * @param string $string |
|
59 | + * |
|
60 | + * @return string |
|
61 | + */ |
|
62 | + public static function escapeMarkdown(string $string): string { |
|
63 | + return str_replace( |
|
64 | + ['[', '`', '*', '_',], |
|
65 | + ['\[', '\`', '\*', '\_',], |
|
66 | + $string |
|
67 | + ); |
|
68 | + } |
|
69 | + |
|
70 | + /** |
|
71 | + * Escape markdown (v2) special characters |
|
72 | + * |
|
73 | + * @see https://core.telegram.org/bots/api#markdownv2-style |
|
74 | + * |
|
75 | + * @param string $string |
|
76 | + * |
|
77 | + * @return string |
|
78 | + */ |
|
79 | + public static function escapeMarkdownV2(string $string): string { |
|
80 | + return str_replace( |
|
81 | + ['_', '*', '[', ']', '(', ')', '~', '`', '>', '#', '+', '-', '=', '|', '{', '}', '.', '!'], |
|
82 | + ['\_', '\*', '\[', '\]', '\(', '\)', '\~', '\`', '\>', '\#', '\+', '\-', '\=', '\|', '\{', '\}', '\.', '\!'], |
|
83 | + $string |
|
84 | + ); |
|
85 | + } |
|
86 | + |
|
87 | + /** |
|
88 | + * Get the raw data passed to this entity |
|
89 | + * |
|
90 | + * @param bool $associated |
|
91 | + * @return array|string |
|
92 | + */ |
|
93 | + public function getRawData(bool $associated = true): array|string { |
|
94 | + return $associated ? $this->raw_data : json_encode($this->raw_data); |
|
95 | + } |
|
96 | + |
|
97 | + /** |
|
98 | + * Get a property from the current Entity |
|
99 | + * |
|
100 | + * @param string $property |
|
101 | + * @param mixed $default |
|
102 | + * |
|
103 | + * @return mixed |
|
104 | + */ |
|
105 | + public function getProperty(string $property, mixed $default = null): mixed { |
|
106 | + return $this->raw_data[$property] ?? $default; |
|
107 | + } |
|
108 | + |
|
109 | + /** |
|
110 | + * Get the list of the properties that are themselves Entities |
|
111 | + * |
|
112 | + * @return array |
|
113 | + */ |
|
114 | + protected function subEntities(): array { |
|
115 | + return []; |
|
116 | + } |
|
117 | + |
|
118 | + /** |
|
119 | + * Get the key value from the raw data passed to this entity |
|
120 | + * |
|
121 | + * @param string $key |
|
122 | + * @return mixed The value can be {Entity, array, string, int, float, bool, null} |
|
123 | + */ |
|
124 | + public function get(string $key): mixed { |
|
125 | + $property = $this->getProperty($key); |
|
126 | + $sub_entities = $this->subEntities() ?? []; |
|
127 | + |
|
128 | + if (isset($sub_entities[$key])) { |
|
129 | + $class_name = $sub_entities[$key]; |
|
130 | + return Factory::resolveEntityClass($class_name, $property); |
|
131 | + } |
|
132 | + |
|
133 | + return $property ?? null; |
|
134 | + } |
|
135 | + |
|
136 | + /** |
|
137 | + * Rewrite the entity values with the new data |
|
138 | + * |
|
139 | + * @param string $key |
|
140 | + * @param mixed $value |
|
141 | + * @return void |
|
142 | + */ |
|
143 | + public function set(string $key, mixed $value): void { |
|
144 | + $this->raw_data[$key] = $value; |
|
145 | + |
|
146 | + if (property_exists($this, $key)) { |
|
147 | + $this->{$key} = $value; |
|
148 | + } |
|
149 | + } |
|
150 | + |
|
151 | + /** |
|
152 | + * @param string $name The name of the property |
|
153 | + * @param array $arguments The arguments passed to the method |
|
154 | + * @return mixed |
|
155 | + */ |
|
156 | + public function __call(string $name, array $arguments): mixed { |
|
157 | + if (method_exists($this, $name)) { |
|
158 | + return $this->{$name}(...$arguments); |
|
159 | + } |
|
160 | + |
|
161 | + if (str_starts_with($name, 'get')) { |
|
162 | + $property_name = strtolower(ltrim(preg_replace('/[A-Z]/', '_$0', substr($name, 3)), '_')); |
|
163 | + return $this->get($property_name); |
|
164 | + } |
|
165 | + |
|
166 | + if (str_starts_with($name, 'set')) { |
|
167 | + $property_name = strtolower(ltrim(preg_replace('/[A-Z]/', '_$0', substr($name, 3)), '_')); |
|
168 | + $this->set($property_name, $arguments[0]); |
|
169 | + return $this; |
|
170 | + } |
|
171 | 171 | |
172 | 172 | throw new \BadMethodCallException(`Method {$name} does not exist`); |
173 | - } |
|
173 | + } |
|
174 | 174 | |
175 | 175 | } |
@@ -79,7 +79,7 @@ |
||
79 | 79 | } |
80 | 80 | |
81 | 81 | if (!Telegram::validateToken($_ENV['TELEGRAM_BOT_TOKEN'] ?? '')) { |
82 | - Telegram::tryAutoloadEnv(); |
|
82 | + Telegram::tryAutoloadEnv(); |
|
83 | 83 | } |
84 | 84 | |
85 | 85 | if (($token = self::loadToken()) === null) { |
@@ -23,7 +23,7 @@ |
||
23 | 23 | */ |
24 | 24 | public static function enableCrashHandler(): void |
25 | 25 | { |
26 | - $handler = function (Throwable $throwable) { |
|
26 | + $handler = function(Throwable $throwable) { |
|
27 | 27 | if (Telegram::getAdminId() !== -1) { |
28 | 28 | $input = Telegram::getInput(); |
29 | 29 | CrashPad::sendCrash(Telegram::getAdminId(), $throwable, $input); |