@@ -12,26 +12,26 @@ |
||
| 12 | 12 | abstract class Factory |
| 13 | 13 | { |
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * @param array $data |
|
| 17 | - * @return Entity |
|
| 18 | - */ |
|
| 19 | - abstract public static function make(array $data): Entity; |
|
| 15 | + /** |
|
| 16 | + * @param array $data |
|
| 17 | + * @return Entity |
|
| 18 | + */ |
|
| 19 | + abstract public static function make(array $data): Entity; |
|
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * This method is used to create a new instance of the entity. |
|
| 23 | - * |
|
| 24 | - * @param string $class |
|
| 25 | - * @param mixed $property |
|
| 26 | - * @return Entity |
|
| 27 | - */ |
|
| 28 | - public static function resolveEntityClass(string $class, mixed $property): Entity |
|
| 29 | - { |
|
| 30 | - if (is_subclass_of($class, Factory::class)) { |
|
| 31 | - return $class::make($property); |
|
| 32 | - } |
|
| 21 | + /** |
|
| 22 | + * This method is used to create a new instance of the entity. |
|
| 23 | + * |
|
| 24 | + * @param string $class |
|
| 25 | + * @param mixed $property |
|
| 26 | + * @return Entity |
|
| 27 | + */ |
|
| 28 | + public static function resolveEntityClass(string $class, mixed $property): Entity |
|
| 29 | + { |
|
| 30 | + if (is_subclass_of($class, Factory::class)) { |
|
| 31 | + return $class::make($property); |
|
| 32 | + } |
|
| 33 | 33 | |
| 34 | - return new $class($property); |
|
| 35 | - } |
|
| 34 | + return new $class($property); |
|
| 35 | + } |
|
| 36 | 36 | |
| 37 | 37 | } |
@@ -143,227 +143,227 @@ |
||
| 143 | 143 | class Request |
| 144 | 144 | { |
| 145 | 145 | |
| 146 | - /** |
|
| 147 | - * Available fields for InputFile helper |
|
| 148 | - * |
|
| 149 | - * This is basically the list of all fields that allow InputFile objects |
|
| 150 | - * for which input can be simplified by providing local path directly as string. |
|
| 151 | - * |
|
| 152 | - * @var array |
|
| 153 | - */ |
|
| 154 | - private static array $input_file_fields = [ |
|
| 155 | - 'setWebhook' => ['certificate'], |
|
| 156 | - 'sendPhoto' => ['photo'], |
|
| 157 | - 'sendAudio' => ['audio', 'thumb'], |
|
| 158 | - 'sendDocument' => ['document', 'thumb'], |
|
| 159 | - 'sendVideo' => ['video', 'thumb'], |
|
| 160 | - 'sendAnimation' => ['animation', 'thumb'], |
|
| 161 | - 'sendVoice' => ['voice', 'thumb'], |
|
| 162 | - 'sendVideoNote' => ['video_note', 'thumb'], |
|
| 163 | - 'setChatPhoto' => ['photo'], |
|
| 164 | - 'sendSticker' => ['sticker'], |
|
| 165 | - 'uploadStickerFile' => ['png_sticker'], |
|
| 166 | - 'createNewStickerSet' => ['png_sticker', 'tgs_sticker', 'webm_sticker'], |
|
| 167 | - 'addStickerToSet' => ['png_sticker', 'tgs_sticker', 'webm_sticker'], |
|
| 168 | - 'setStickerSetThumb' => ['thumb'], |
|
| 169 | - ]; |
|
| 170 | - |
|
| 171 | - /** |
|
| 172 | - * URI of the Telegram API |
|
| 173 | - * |
|
| 174 | - * @var string |
|
| 175 | - */ |
|
| 176 | - private static string $api_base_uri = 'https://api.telegram.org'; |
|
| 177 | - |
|
| 178 | - /** |
|
| 179 | - * URI of the Telegram API for downloading files (relative to $api_base_url or absolute) |
|
| 180 | - * |
|
| 181 | - * @var string |
|
| 182 | - */ |
|
| 183 | - private static string $api_base_download_uri = '/file/bot{API_KEY}'; |
|
| 184 | - |
|
| 185 | - /** |
|
| 186 | - * The current action that is being executed |
|
| 187 | - * |
|
| 188 | - * @var string |
|
| 189 | - */ |
|
| 190 | - private static string $current_action = ''; |
|
| 191 | - |
|
| 192 | - /** |
|
| 193 | - * Get the Telegram API path |
|
| 194 | - * |
|
| 195 | - * @return string |
|
| 196 | - */ |
|
| 197 | - public static function getApiPath(): string |
|
| 198 | - { |
|
| 199 | - return self::$api_base_uri . '/bot' . Telegram::getApiKey() . '/'; |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - /** |
|
| 203 | - * Initialize a http client |
|
| 204 | - * |
|
| 205 | - * @return Client |
|
| 206 | - */ |
|
| 207 | - private static function getClient(): Client |
|
| 208 | - { |
|
| 209 | - return new Client(); |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - /** |
|
| 213 | - * Use this method to get stats of given user in a supergroup or channel. |
|
| 214 | - * |
|
| 215 | - * @param int $user_id User identifier |
|
| 216 | - * @param int $chat_id Identifier of the chat to get stats for |
|
| 217 | - * |
|
| 218 | - * @return string [left, member, administrator, creator] |
|
| 219 | - */ |
|
| 220 | - public static function getChatMemberStatus(int $user_id, int $chat_id): string |
|
| 221 | - { |
|
| 222 | - $response = self::getChatMember([ |
|
| 223 | - 'user_id' => $user_id, |
|
| 224 | - 'chat_id' => $chat_id, |
|
| 225 | - ]); |
|
| 226 | - |
|
| 227 | - return $response->getResult()->status ?? "left"; |
|
| 228 | - } |
|
| 229 | - |
|
| 230 | - /** |
|
| 231 | - * Properly set up the request params |
|
| 232 | - * |
|
| 233 | - * If any item of the array is a resource, reformat it to a multipart request. |
|
| 234 | - * Else, just return the passed data as form params. |
|
| 235 | - * |
|
| 236 | - * @param array $data |
|
| 237 | - * @return array |
|
| 238 | - */ |
|
| 239 | - private static function setUpRequestParams(array $data): array |
|
| 240 | - { |
|
| 241 | - $multipart = []; |
|
| 242 | - $has_resource = false; |
|
| 243 | - |
|
| 244 | - $options = [ |
|
| 245 | - 'header' => [ |
|
| 246 | - 'Content-Type' => 'application/json', |
|
| 247 | - 'Accept' => 'application/json', |
|
| 248 | - 'User-Agent' => 'TelegramBot-PHP/' . Telegram::$VERSION |
|
| 249 | - ] |
|
| 250 | - ]; |
|
| 251 | - |
|
| 252 | - foreach ($data as $key => &$item) { |
|
| 253 | - if (array_key_exists(self::$current_action, self::$input_file_fields) && in_array($key, self::$input_file_fields[self::$current_action], true)) { |
|
| 254 | - |
|
| 255 | - if (is_string($item) && file_exists($item)) { |
|
| 256 | - $has_resource = true; |
|
| 257 | - |
|
| 258 | - } elseif (filter_var($item, FILTER_VALIDATE_URL)) { |
|
| 259 | - $has_resource = false; |
|
| 260 | - continue; |
|
| 261 | - |
|
| 262 | - } else { |
|
| 263 | - throw new TelegramException( |
|
| 264 | - 'Invalid file path or URL: ' . $item . ' for ' . self::$current_action . ' action' |
|
| 265 | - ); |
|
| 266 | - } |
|
| 267 | - |
|
| 268 | - $multipart[$key] = $item; |
|
| 269 | - continue; |
|
| 270 | - } |
|
| 271 | - |
|
| 272 | - if ($item instanceof Entity) { |
|
| 273 | - $item = $item->getRawData(); |
|
| 274 | - } |
|
| 275 | - |
|
| 276 | - if (is_array($item)) { |
|
| 277 | - $item = json_encode($item); |
|
| 278 | - } |
|
| 279 | - |
|
| 280 | - $options['query'][$key] = $item; |
|
| 281 | - } |
|
| 282 | - unset($item); |
|
| 283 | - |
|
| 284 | - if ($has_resource) { |
|
| 285 | - $options['multipart'] = FormData::create($multipart); |
|
| 286 | - } |
|
| 287 | - |
|
| 288 | - return $options; |
|
| 289 | - } |
|
| 290 | - |
|
| 291 | - /** |
|
| 292 | - * Create a Http Request |
|
| 293 | - * |
|
| 294 | - * @param string $action Action to execute |
|
| 295 | - * @param array $data Data to attach to the execution |
|
| 296 | - * |
|
| 297 | - * @return array An array of the setUpRequestParams and the url |
|
| 298 | - */ |
|
| 299 | - public static function create(string $action, array $data): array |
|
| 300 | - { |
|
| 301 | - return [ |
|
| 302 | - 'url' => self::getApiPath() . $action, |
|
| 303 | - 'options' => self::setUpRequestParams($data) |
|
| 304 | - ]; |
|
| 305 | - } |
|
| 306 | - |
|
| 307 | - /** |
|
| 308 | - * Execute HTTP Request |
|
| 309 | - * |
|
| 310 | - * @param string $action Action to execute |
|
| 311 | - * @param array $data Data to attach to the execution |
|
| 312 | - * |
|
| 313 | - * @return string Result of the HTTP Request |
|
| 314 | - */ |
|
| 315 | - private static function execute(string $action, array $data): string |
|
| 316 | - { |
|
| 317 | - $request = self::create($action, $data); |
|
| 318 | - |
|
| 319 | - $response = self::getClient()->get($request['url'], $request['options']); |
|
| 320 | - |
|
| 321 | - return $response->getBody(); |
|
| 322 | - } |
|
| 323 | - |
|
| 324 | - /** |
|
| 325 | - * Send command |
|
| 326 | - * |
|
| 327 | - * @param string $action |
|
| 328 | - * @param array $data |
|
| 329 | - * |
|
| 330 | - * @return Response |
|
| 331 | - * @throws TelegramException |
|
| 332 | - */ |
|
| 333 | - public static function send(string $action, array $data = []): Response |
|
| 334 | - { |
|
| 335 | - self::$current_action = $action; |
|
| 336 | - |
|
| 337 | - $raw_response = self::execute($action, $data); |
|
| 338 | - |
|
| 339 | - if (!Common::isJson($raw_response)) { |
|
| 340 | - TelegramLog::debug($raw_response); |
|
| 341 | - throw new TelegramException('Invalid response from API'); |
|
| 342 | - } |
|
| 343 | - |
|
| 344 | - $response = new Response(json_decode($raw_response, true)); |
|
| 345 | - |
|
| 346 | - if (!$response->isOk() && $response->getErrorCode() === 401 && $response->getDescription() === 'Unauthorized') { |
|
| 347 | - throw new InvalidBotTokenException(); |
|
| 348 | - } |
|
| 349 | - |
|
| 350 | - self::$current_action = ''; |
|
| 351 | - |
|
| 352 | - return $response; |
|
| 353 | - } |
|
| 354 | - |
|
| 355 | - /** |
|
| 356 | - * Any statically called method should be relayed to the `send` method. |
|
| 357 | - * |
|
| 358 | - * @param string $action |
|
| 359 | - * @param array $data |
|
| 360 | - * |
|
| 361 | - * @return Response |
|
| 362 | - * @throws TelegramException |
|
| 363 | - */ |
|
| 364 | - public static function __callStatic(string $action, array $data): Response |
|
| 365 | - { |
|
| 366 | - return static::send($action, reset($data) ?: []); |
|
| 367 | - } |
|
| 146 | + /** |
|
| 147 | + * Available fields for InputFile helper |
|
| 148 | + * |
|
| 149 | + * This is basically the list of all fields that allow InputFile objects |
|
| 150 | + * for which input can be simplified by providing local path directly as string. |
|
| 151 | + * |
|
| 152 | + * @var array |
|
| 153 | + */ |
|
| 154 | + private static array $input_file_fields = [ |
|
| 155 | + 'setWebhook' => ['certificate'], |
|
| 156 | + 'sendPhoto' => ['photo'], |
|
| 157 | + 'sendAudio' => ['audio', 'thumb'], |
|
| 158 | + 'sendDocument' => ['document', 'thumb'], |
|
| 159 | + 'sendVideo' => ['video', 'thumb'], |
|
| 160 | + 'sendAnimation' => ['animation', 'thumb'], |
|
| 161 | + 'sendVoice' => ['voice', 'thumb'], |
|
| 162 | + 'sendVideoNote' => ['video_note', 'thumb'], |
|
| 163 | + 'setChatPhoto' => ['photo'], |
|
| 164 | + 'sendSticker' => ['sticker'], |
|
| 165 | + 'uploadStickerFile' => ['png_sticker'], |
|
| 166 | + 'createNewStickerSet' => ['png_sticker', 'tgs_sticker', 'webm_sticker'], |
|
| 167 | + 'addStickerToSet' => ['png_sticker', 'tgs_sticker', 'webm_sticker'], |
|
| 168 | + 'setStickerSetThumb' => ['thumb'], |
|
| 169 | + ]; |
|
| 170 | + |
|
| 171 | + /** |
|
| 172 | + * URI of the Telegram API |
|
| 173 | + * |
|
| 174 | + * @var string |
|
| 175 | + */ |
|
| 176 | + private static string $api_base_uri = 'https://api.telegram.org'; |
|
| 177 | + |
|
| 178 | + /** |
|
| 179 | + * URI of the Telegram API for downloading files (relative to $api_base_url or absolute) |
|
| 180 | + * |
|
| 181 | + * @var string |
|
| 182 | + */ |
|
| 183 | + private static string $api_base_download_uri = '/file/bot{API_KEY}'; |
|
| 184 | + |
|
| 185 | + /** |
|
| 186 | + * The current action that is being executed |
|
| 187 | + * |
|
| 188 | + * @var string |
|
| 189 | + */ |
|
| 190 | + private static string $current_action = ''; |
|
| 191 | + |
|
| 192 | + /** |
|
| 193 | + * Get the Telegram API path |
|
| 194 | + * |
|
| 195 | + * @return string |
|
| 196 | + */ |
|
| 197 | + public static function getApiPath(): string |
|
| 198 | + { |
|
| 199 | + return self::$api_base_uri . '/bot' . Telegram::getApiKey() . '/'; |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + /** |
|
| 203 | + * Initialize a http client |
|
| 204 | + * |
|
| 205 | + * @return Client |
|
| 206 | + */ |
|
| 207 | + private static function getClient(): Client |
|
| 208 | + { |
|
| 209 | + return new Client(); |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + /** |
|
| 213 | + * Use this method to get stats of given user in a supergroup or channel. |
|
| 214 | + * |
|
| 215 | + * @param int $user_id User identifier |
|
| 216 | + * @param int $chat_id Identifier of the chat to get stats for |
|
| 217 | + * |
|
| 218 | + * @return string [left, member, administrator, creator] |
|
| 219 | + */ |
|
| 220 | + public static function getChatMemberStatus(int $user_id, int $chat_id): string |
|
| 221 | + { |
|
| 222 | + $response = self::getChatMember([ |
|
| 223 | + 'user_id' => $user_id, |
|
| 224 | + 'chat_id' => $chat_id, |
|
| 225 | + ]); |
|
| 226 | + |
|
| 227 | + return $response->getResult()->status ?? "left"; |
|
| 228 | + } |
|
| 229 | + |
|
| 230 | + /** |
|
| 231 | + * Properly set up the request params |
|
| 232 | + * |
|
| 233 | + * If any item of the array is a resource, reformat it to a multipart request. |
|
| 234 | + * Else, just return the passed data as form params. |
|
| 235 | + * |
|
| 236 | + * @param array $data |
|
| 237 | + * @return array |
|
| 238 | + */ |
|
| 239 | + private static function setUpRequestParams(array $data): array |
|
| 240 | + { |
|
| 241 | + $multipart = []; |
|
| 242 | + $has_resource = false; |
|
| 243 | + |
|
| 244 | + $options = [ |
|
| 245 | + 'header' => [ |
|
| 246 | + 'Content-Type' => 'application/json', |
|
| 247 | + 'Accept' => 'application/json', |
|
| 248 | + 'User-Agent' => 'TelegramBot-PHP/' . Telegram::$VERSION |
|
| 249 | + ] |
|
| 250 | + ]; |
|
| 251 | + |
|
| 252 | + foreach ($data as $key => &$item) { |
|
| 253 | + if (array_key_exists(self::$current_action, self::$input_file_fields) && in_array($key, self::$input_file_fields[self::$current_action], true)) { |
|
| 254 | + |
|
| 255 | + if (is_string($item) && file_exists($item)) { |
|
| 256 | + $has_resource = true; |
|
| 257 | + |
|
| 258 | + } elseif (filter_var($item, FILTER_VALIDATE_URL)) { |
|
| 259 | + $has_resource = false; |
|
| 260 | + continue; |
|
| 261 | + |
|
| 262 | + } else { |
|
| 263 | + throw new TelegramException( |
|
| 264 | + 'Invalid file path or URL: ' . $item . ' for ' . self::$current_action . ' action' |
|
| 265 | + ); |
|
| 266 | + } |
|
| 267 | + |
|
| 268 | + $multipart[$key] = $item; |
|
| 269 | + continue; |
|
| 270 | + } |
|
| 271 | + |
|
| 272 | + if ($item instanceof Entity) { |
|
| 273 | + $item = $item->getRawData(); |
|
| 274 | + } |
|
| 275 | + |
|
| 276 | + if (is_array($item)) { |
|
| 277 | + $item = json_encode($item); |
|
| 278 | + } |
|
| 279 | + |
|
| 280 | + $options['query'][$key] = $item; |
|
| 281 | + } |
|
| 282 | + unset($item); |
|
| 283 | + |
|
| 284 | + if ($has_resource) { |
|
| 285 | + $options['multipart'] = FormData::create($multipart); |
|
| 286 | + } |
|
| 287 | + |
|
| 288 | + return $options; |
|
| 289 | + } |
|
| 290 | + |
|
| 291 | + /** |
|
| 292 | + * Create a Http Request |
|
| 293 | + * |
|
| 294 | + * @param string $action Action to execute |
|
| 295 | + * @param array $data Data to attach to the execution |
|
| 296 | + * |
|
| 297 | + * @return array An array of the setUpRequestParams and the url |
|
| 298 | + */ |
|
| 299 | + public static function create(string $action, array $data): array |
|
| 300 | + { |
|
| 301 | + return [ |
|
| 302 | + 'url' => self::getApiPath() . $action, |
|
| 303 | + 'options' => self::setUpRequestParams($data) |
|
| 304 | + ]; |
|
| 305 | + } |
|
| 306 | + |
|
| 307 | + /** |
|
| 308 | + * Execute HTTP Request |
|
| 309 | + * |
|
| 310 | + * @param string $action Action to execute |
|
| 311 | + * @param array $data Data to attach to the execution |
|
| 312 | + * |
|
| 313 | + * @return string Result of the HTTP Request |
|
| 314 | + */ |
|
| 315 | + private static function execute(string $action, array $data): string |
|
| 316 | + { |
|
| 317 | + $request = self::create($action, $data); |
|
| 318 | + |
|
| 319 | + $response = self::getClient()->get($request['url'], $request['options']); |
|
| 320 | + |
|
| 321 | + return $response->getBody(); |
|
| 322 | + } |
|
| 323 | + |
|
| 324 | + /** |
|
| 325 | + * Send command |
|
| 326 | + * |
|
| 327 | + * @param string $action |
|
| 328 | + * @param array $data |
|
| 329 | + * |
|
| 330 | + * @return Response |
|
| 331 | + * @throws TelegramException |
|
| 332 | + */ |
|
| 333 | + public static function send(string $action, array $data = []): Response |
|
| 334 | + { |
|
| 335 | + self::$current_action = $action; |
|
| 336 | + |
|
| 337 | + $raw_response = self::execute($action, $data); |
|
| 338 | + |
|
| 339 | + if (!Common::isJson($raw_response)) { |
|
| 340 | + TelegramLog::debug($raw_response); |
|
| 341 | + throw new TelegramException('Invalid response from API'); |
|
| 342 | + } |
|
| 343 | + |
|
| 344 | + $response = new Response(json_decode($raw_response, true)); |
|
| 345 | + |
|
| 346 | + if (!$response->isOk() && $response->getErrorCode() === 401 && $response->getDescription() === 'Unauthorized') { |
|
| 347 | + throw new InvalidBotTokenException(); |
|
| 348 | + } |
|
| 349 | + |
|
| 350 | + self::$current_action = ''; |
|
| 351 | + |
|
| 352 | + return $response; |
|
| 353 | + } |
|
| 354 | + |
|
| 355 | + /** |
|
| 356 | + * Any statically called method should be relayed to the `send` method. |
|
| 357 | + * |
|
| 358 | + * @param string $action |
|
| 359 | + * @param array $data |
|
| 360 | + * |
|
| 361 | + * @return Response |
|
| 362 | + * @throws TelegramException |
|
| 363 | + */ |
|
| 364 | + public static function __callStatic(string $action, array $data): Response |
|
| 365 | + { |
|
| 366 | + return static::send($action, reset($data) ?: []); |
|
| 367 | + } |
|
| 368 | 368 | |
| 369 | 369 | } |
@@ -23,103 +23,103 @@ |
||
| 23 | 23 | class Keyboard extends Entity |
| 24 | 24 | { |
| 25 | 25 | |
| 26 | - public function __construct(array $data = []) |
|
| 27 | - { |
|
| 28 | - parent::__construct(array_merge($data, [ |
|
| 29 | - 'resize_keyboard' => false, |
|
| 30 | - 'one_time_keyboard' => false, |
|
| 31 | - 'input_field_placeholder' => '', |
|
| 32 | - 'selective' => false |
|
| 33 | - ])); |
|
| 34 | - } |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * Create a new row in keyboard and add buttons. |
|
| 38 | - * |
|
| 39 | - * @param array<KeyboardButton> $row |
|
| 40 | - * @return Keyboard |
|
| 41 | - */ |
|
| 42 | - public function addRow(array $row): Keyboard |
|
| 43 | - { |
|
| 44 | - $keyboard_type = self::getType(); |
|
| 45 | - |
|
| 46 | - if (!isset($this->raw_data[$keyboard_type]) || !is_array($this->raw_data[$keyboard_type])) { |
|
| 47 | - $this->raw_data[$keyboard_type] = []; |
|
| 48 | - } |
|
| 49 | - |
|
| 50 | - $new_row = []; |
|
| 51 | - foreach ($row as $button) { |
|
| 52 | - $new_row[] = $button->getRawData(); |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - $this->raw_data[$keyboard_type][] = $new_row; |
|
| 56 | - |
|
| 57 | - return $this; |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * Remove the current custom keyboard and display the default letter-keyboard. |
|
| 62 | - * |
|
| 63 | - * @link https://core.telegram.org/bots/api/#replykeyboardremove |
|
| 64 | - * |
|
| 65 | - * @param array $data |
|
| 66 | - * |
|
| 67 | - * @return Keyboard |
|
| 68 | - */ |
|
| 69 | - public static function remove(array $data = []): Keyboard |
|
| 70 | - { |
|
| 71 | - return new static(array_merge(['keyboard' => [], 'remove_keyboard' => true, 'selective' => false], $data)); |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * Display a reply interface to the user (act as if the user has selected the bot's message and tapped 'Reply'). |
|
| 76 | - * |
|
| 77 | - * @link https://core.telegram.org/bots/api#forcereply |
|
| 78 | - * |
|
| 79 | - * @param array $data |
|
| 80 | - * |
|
| 81 | - * @return Keyboard |
|
| 82 | - */ |
|
| 83 | - public static function forceReply(array $data = []): Keyboard |
|
| 84 | - { |
|
| 85 | - return new static(array_merge(['keyboard' => [], 'force_reply' => true, 'selective' => false], $data)); |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - /** |
|
| 89 | - * Get keyboard button type |
|
| 90 | - * |
|
| 91 | - * @return string ["keyboard"|"inline_keyboard"] |
|
| 92 | - */ |
|
| 93 | - public function getType(): string |
|
| 94 | - { |
|
| 95 | - $reflection = new \ReflectionClass(static::class); |
|
| 96 | - |
|
| 97 | - $class_name = $reflection->getShortName(); |
|
| 98 | - |
|
| 99 | - return strtolower(ltrim(preg_replace('/[A-Z]/', '_$0', $class_name), '_')); |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * Creates instance of Keyboard |
|
| 104 | - * |
|
| 105 | - * @return Keyboard |
|
| 106 | - */ |
|
| 107 | - public static function make(): Keyboard |
|
| 108 | - { |
|
| 109 | - return new self(); |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - /** |
|
| 113 | - * @param array $rows |
|
| 114 | - * @return Keyboard |
|
| 115 | - */ |
|
| 116 | - public function setKeyboard(array $rows): Keyboard |
|
| 117 | - { |
|
| 118 | - foreach ($rows as $row) { |
|
| 119 | - $this->addRow($row); |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - return $this; |
|
| 123 | - } |
|
| 26 | + public function __construct(array $data = []) |
|
| 27 | + { |
|
| 28 | + parent::__construct(array_merge($data, [ |
|
| 29 | + 'resize_keyboard' => false, |
|
| 30 | + 'one_time_keyboard' => false, |
|
| 31 | + 'input_field_placeholder' => '', |
|
| 32 | + 'selective' => false |
|
| 33 | + ])); |
|
| 34 | + } |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * Create a new row in keyboard and add buttons. |
|
| 38 | + * |
|
| 39 | + * @param array<KeyboardButton> $row |
|
| 40 | + * @return Keyboard |
|
| 41 | + */ |
|
| 42 | + public function addRow(array $row): Keyboard |
|
| 43 | + { |
|
| 44 | + $keyboard_type = self::getType(); |
|
| 45 | + |
|
| 46 | + if (!isset($this->raw_data[$keyboard_type]) || !is_array($this->raw_data[$keyboard_type])) { |
|
| 47 | + $this->raw_data[$keyboard_type] = []; |
|
| 48 | + } |
|
| 49 | + |
|
| 50 | + $new_row = []; |
|
| 51 | + foreach ($row as $button) { |
|
| 52 | + $new_row[] = $button->getRawData(); |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + $this->raw_data[$keyboard_type][] = $new_row; |
|
| 56 | + |
|
| 57 | + return $this; |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * Remove the current custom keyboard and display the default letter-keyboard. |
|
| 62 | + * |
|
| 63 | + * @link https://core.telegram.org/bots/api/#replykeyboardremove |
|
| 64 | + * |
|
| 65 | + * @param array $data |
|
| 66 | + * |
|
| 67 | + * @return Keyboard |
|
| 68 | + */ |
|
| 69 | + public static function remove(array $data = []): Keyboard |
|
| 70 | + { |
|
| 71 | + return new static(array_merge(['keyboard' => [], 'remove_keyboard' => true, 'selective' => false], $data)); |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * Display a reply interface to the user (act as if the user has selected the bot's message and tapped 'Reply'). |
|
| 76 | + * |
|
| 77 | + * @link https://core.telegram.org/bots/api#forcereply |
|
| 78 | + * |
|
| 79 | + * @param array $data |
|
| 80 | + * |
|
| 81 | + * @return Keyboard |
|
| 82 | + */ |
|
| 83 | + public static function forceReply(array $data = []): Keyboard |
|
| 84 | + { |
|
| 85 | + return new static(array_merge(['keyboard' => [], 'force_reply' => true, 'selective' => false], $data)); |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + /** |
|
| 89 | + * Get keyboard button type |
|
| 90 | + * |
|
| 91 | + * @return string ["keyboard"|"inline_keyboard"] |
|
| 92 | + */ |
|
| 93 | + public function getType(): string |
|
| 94 | + { |
|
| 95 | + $reflection = new \ReflectionClass(static::class); |
|
| 96 | + |
|
| 97 | + $class_name = $reflection->getShortName(); |
|
| 98 | + |
|
| 99 | + return strtolower(ltrim(preg_replace('/[A-Z]/', '_$0', $class_name), '_')); |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * Creates instance of Keyboard |
|
| 104 | + * |
|
| 105 | + * @return Keyboard |
|
| 106 | + */ |
|
| 107 | + public static function make(): Keyboard |
|
| 108 | + { |
|
| 109 | + return new self(); |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + /** |
|
| 113 | + * @param array $rows |
|
| 114 | + * @return Keyboard |
|
| 115 | + */ |
|
| 116 | + public function setKeyboard(array $rows): Keyboard |
|
| 117 | + { |
|
| 118 | + foreach ($rows as $row) { |
|
| 119 | + $this->addRow($row); |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + return $this; |
|
| 123 | + } |
|
| 124 | 124 | |
| 125 | 125 | } |
@@ -32,70 +32,70 @@ |
||
| 32 | 32 | class Update extends Entity |
| 33 | 33 | { |
| 34 | 34 | |
| 35 | - public const TYPE_MESSAGE = 'message'; |
|
| 36 | - public const TYPE_EDITED_MESSAGE = 'edited_message'; |
|
| 37 | - public const TYPE_CHANNEL_POST = 'channel_post'; |
|
| 38 | - public const TYPE_EDITED_CHANNEL_POST = 'edited_channel_post'; |
|
| 39 | - public const TYPE_INLINE_QUERY = 'inline_query'; |
|
| 40 | - public const TYPE_CHOSEN_INLINE_RESULT = 'chosen_inline_result'; |
|
| 41 | - public const TYPE_CALLBACK_QUERY = 'callback_query'; |
|
| 42 | - public const TYPE_SHIPPING_QUERY = 'shipping_query'; |
|
| 43 | - public const TYPE_PRE_CHECKOUT_QUERY = 'pre_checkout_query'; |
|
| 44 | - public const TYPE_POLL = 'poll'; |
|
| 45 | - public const TYPE_POLL_ANSWER = 'poll_answer'; |
|
| 46 | - public const TYPE_MY_CHAT_MEMBER = 'my_chat_member'; |
|
| 47 | - public const TYPE_CHAT_MEMBER = 'chat_member'; |
|
| 48 | - public const TYPE_CHAT_JOIN_REQUEST = 'chat_join_request'; |
|
| 49 | - public const TYPE_WEB_DATA = 'web_data'; |
|
| 35 | + public const TYPE_MESSAGE = 'message'; |
|
| 36 | + public const TYPE_EDITED_MESSAGE = 'edited_message'; |
|
| 37 | + public const TYPE_CHANNEL_POST = 'channel_post'; |
|
| 38 | + public const TYPE_EDITED_CHANNEL_POST = 'edited_channel_post'; |
|
| 39 | + public const TYPE_INLINE_QUERY = 'inline_query'; |
|
| 40 | + public const TYPE_CHOSEN_INLINE_RESULT = 'chosen_inline_result'; |
|
| 41 | + public const TYPE_CALLBACK_QUERY = 'callback_query'; |
|
| 42 | + public const TYPE_SHIPPING_QUERY = 'shipping_query'; |
|
| 43 | + public const TYPE_PRE_CHECKOUT_QUERY = 'pre_checkout_query'; |
|
| 44 | + public const TYPE_POLL = 'poll'; |
|
| 45 | + public const TYPE_POLL_ANSWER = 'poll_answer'; |
|
| 46 | + public const TYPE_MY_CHAT_MEMBER = 'my_chat_member'; |
|
| 47 | + public const TYPE_CHAT_MEMBER = 'chat_member'; |
|
| 48 | + public const TYPE_CHAT_JOIN_REQUEST = 'chat_join_request'; |
|
| 49 | + public const TYPE_WEB_DATA = 'web_data'; |
|
| 50 | 50 | |
| 51 | - /** |
|
| 52 | - * {@inheritdoc} |
|
| 53 | - */ |
|
| 54 | - protected function subEntities(): array |
|
| 55 | - { |
|
| 56 | - return [ |
|
| 57 | - self::TYPE_MESSAGE => Message::class, |
|
| 58 | - self::TYPE_EDITED_MESSAGE => EditedMessage::class, |
|
| 59 | - self::TYPE_CHANNEL_POST => ChannelPost::class, |
|
| 60 | - self::TYPE_EDITED_CHANNEL_POST => EditedChannelPost::class, |
|
| 61 | - self::TYPE_INLINE_QUERY => InlineQuery::class, |
|
| 62 | - self::TYPE_CHOSEN_INLINE_RESULT => ChosenInlineResult::class, |
|
| 63 | - self::TYPE_CALLBACK_QUERY => CallbackQuery::class, |
|
| 64 | - self::TYPE_SHIPPING_QUERY => ShippingQuery::class, |
|
| 65 | - self::TYPE_PRE_CHECKOUT_QUERY => PreCheckoutQuery::class, |
|
| 66 | - self::TYPE_POLL => Poll::class, |
|
| 67 | - self::TYPE_POLL_ANSWER => PollAnswer::class, |
|
| 68 | - self::TYPE_MY_CHAT_MEMBER => ChatMemberUpdated::class, |
|
| 69 | - self::TYPE_CHAT_MEMBER => ChatMemberUpdated::class, |
|
| 70 | - self::TYPE_CHAT_JOIN_REQUEST => ChatJoinRequest::class, |
|
| 71 | - self::TYPE_WEB_DATA => WebAppData::class, |
|
| 72 | - ]; |
|
| 73 | - } |
|
| 51 | + /** |
|
| 52 | + * {@inheritdoc} |
|
| 53 | + */ |
|
| 54 | + protected function subEntities(): array |
|
| 55 | + { |
|
| 56 | + return [ |
|
| 57 | + self::TYPE_MESSAGE => Message::class, |
|
| 58 | + self::TYPE_EDITED_MESSAGE => EditedMessage::class, |
|
| 59 | + self::TYPE_CHANNEL_POST => ChannelPost::class, |
|
| 60 | + self::TYPE_EDITED_CHANNEL_POST => EditedChannelPost::class, |
|
| 61 | + self::TYPE_INLINE_QUERY => InlineQuery::class, |
|
| 62 | + self::TYPE_CHOSEN_INLINE_RESULT => ChosenInlineResult::class, |
|
| 63 | + self::TYPE_CALLBACK_QUERY => CallbackQuery::class, |
|
| 64 | + self::TYPE_SHIPPING_QUERY => ShippingQuery::class, |
|
| 65 | + self::TYPE_PRE_CHECKOUT_QUERY => PreCheckoutQuery::class, |
|
| 66 | + self::TYPE_POLL => Poll::class, |
|
| 67 | + self::TYPE_POLL_ANSWER => PollAnswer::class, |
|
| 68 | + self::TYPE_MY_CHAT_MEMBER => ChatMemberUpdated::class, |
|
| 69 | + self::TYPE_CHAT_MEMBER => ChatMemberUpdated::class, |
|
| 70 | + self::TYPE_CHAT_JOIN_REQUEST => ChatJoinRequest::class, |
|
| 71 | + self::TYPE_WEB_DATA => WebAppData::class, |
|
| 72 | + ]; |
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | - /** |
|
| 76 | - * Get the list of all available update types |
|
| 77 | - * |
|
| 78 | - * @return string[] |
|
| 79 | - */ |
|
| 80 | - public static function getUpdateTypes(): array |
|
| 81 | - { |
|
| 82 | - return array_keys((new self([]))->subEntities()); |
|
| 83 | - } |
|
| 75 | + /** |
|
| 76 | + * Get the list of all available update types |
|
| 77 | + * |
|
| 78 | + * @return string[] |
|
| 79 | + */ |
|
| 80 | + public static function getUpdateTypes(): array |
|
| 81 | + { |
|
| 82 | + return array_keys((new self([]))->subEntities()); |
|
| 83 | + } |
|
| 84 | 84 | |
| 85 | - /** |
|
| 86 | - * Get the update type based on the set properties |
|
| 87 | - * |
|
| 88 | - * @return string|null |
|
| 89 | - */ |
|
| 90 | - public function getUpdateType(): ?string |
|
| 91 | - { |
|
| 92 | - foreach (self::getUpdateTypes() as $type) { |
|
| 93 | - if ($this->getProperty($type)) { |
|
| 94 | - return $type; |
|
| 95 | - } |
|
| 96 | - } |
|
| 85 | + /** |
|
| 86 | + * Get the update type based on the set properties |
|
| 87 | + * |
|
| 88 | + * @return string|null |
|
| 89 | + */ |
|
| 90 | + public function getUpdateType(): ?string |
|
| 91 | + { |
|
| 92 | + foreach (self::getUpdateTypes() as $type) { |
|
| 93 | + if ($this->getProperty($type)) { |
|
| 94 | + return $type; |
|
| 95 | + } |
|
| 96 | + } |
|
| 97 | 97 | |
| 98 | - return null; |
|
| 99 | - } |
|
| 98 | + return null; |
|
| 99 | + } |
|
| 100 | 100 | |
| 101 | 101 | } |
@@ -156,10 +156,15 @@ discard block |
||
| 156 | 156 | throw new \RuntimeException(sprintf('The method %s does not exist', $method)); |
| 157 | 157 | } |
| 158 | 158 | |
| 159 | - if (is_array($config)) $this->updateConfiguration($config); |
|
| 159 | + if (is_array($config)) { |
|
| 160 | + $this->updateConfiguration($config); |
|
| 161 | + } |
|
| 160 | 162 | |
| 161 | - if (!empty($update)) $this->update = $update; |
|
| 162 | - else $this->update = Telegram::getUpdate() !== false ? Telegram::getUpdate() : null; |
|
| 163 | + if (!empty($update)) { |
|
| 164 | + $this->update = $update; |
|
| 165 | + } else { |
|
| 166 | + $this->update = Telegram::getUpdate() !== false ? Telegram::getUpdate() : null; |
|
| 167 | + } |
|
| 163 | 168 | |
| 164 | 169 | if (empty($this->update)) { |
| 165 | 170 | TelegramLog::error( |
@@ -211,7 +216,9 @@ discard block |
||
| 211 | 216 | foreach ($plugins as $plugin) { |
| 212 | 217 | /** @var Plugin $plugin */ |
| 213 | 218 | (new $plugin())->__execute($this, $update); |
| 214 | - if ($this->isActive === false) break; |
|
| 219 | + if ($this->isActive === false) { |
|
| 220 | + break; |
|
| 221 | + } |
|
| 215 | 222 | } |
| 216 | 223 | |
| 217 | 224 | $this->isActive = false; |
@@ -17,221 +17,221 @@ |
||
| 17 | 17 | abstract class WebhookHandler extends Telegram |
| 18 | 18 | { |
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * @var ?Update |
|
| 22 | - */ |
|
| 23 | - protected ?Update $update; |
|
| 24 | - |
|
| 25 | - /** |
|
| 26 | - * @var array<Plugin> |
|
| 27 | - */ |
|
| 28 | - private array $plugins = []; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * @var bool |
|
| 32 | - */ |
|
| 33 | - private bool $isActive = false; |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * The default configuration of the webhook. |
|
| 37 | - * |
|
| 38 | - * @var array |
|
| 39 | - */ |
|
| 40 | - private array $config = [ |
|
| 41 | - 'autoload_env_file' => false, |
|
| 42 | - 'env_file_path' => null, |
|
| 43 | - ]; |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * Webhook constructor. |
|
| 47 | - * |
|
| 48 | - * @param string $api_key The API key of the bot. Leave it blank for autoload from .env file. |
|
| 49 | - */ |
|
| 50 | - public function __construct(string $api_key = '') |
|
| 51 | - { |
|
| 52 | - parent::__construct($api_key); |
|
| 53 | - |
|
| 54 | - if (!Telegram::validateToken(self::getApiKey())) { |
|
| 55 | - throw new InvalidBotTokenException(); |
|
| 56 | - } |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * Initialize the receiver. |
|
| 61 | - * |
|
| 62 | - * @return void |
|
| 63 | - */ |
|
| 64 | - public function init(): void |
|
| 65 | - { |
|
| 66 | - $this->config['env_file_path'] = $_SERVER['DOCUMENT_ROOT'] . '/.env'; |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * Add a plugin to the receiver |
|
| 71 | - * |
|
| 72 | - * @param array<Plugin> $plugins |
|
| 73 | - */ |
|
| 74 | - public function addPlugin(array $plugins): void |
|
| 75 | - { |
|
| 76 | - foreach ($plugins as $plugin) { |
|
| 77 | - if (!is_subclass_of($plugin, Plugin::class)) { |
|
| 78 | - throw new \RuntimeException( |
|
| 79 | - sprintf('The plugin %s must be an instance of %s', get_class($plugin), Plugin::class) |
|
| 80 | - ); |
|
| 81 | - } |
|
| 82 | - $this->plugins[] = $plugin; |
|
| 83 | - } |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * Match the update with the given plugins |
|
| 88 | - * |
|
| 89 | - * @param array<Plugin> $plugins |
|
| 90 | - * @param ?Update $update The update to match |
|
| 91 | - * @return void |
|
| 92 | - */ |
|
| 93 | - public function loadPluginsWith(array $plugins, Update $update = null): void |
|
| 94 | - { |
|
| 95 | - $update = $update ?? ($this->update ?? Telegram::getUpdate()); |
|
| 96 | - |
|
| 97 | - foreach ($plugins as $plugin) { |
|
| 98 | - if (!is_subclass_of($plugin, Plugin::class)) { |
|
| 99 | - throw new \InvalidArgumentException( |
|
| 100 | - sprintf('The plugin %s must be an instance of %s', get_class($plugin), Plugin::class) |
|
| 101 | - ); |
|
| 102 | - } |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - if ($update instanceof Update) { |
|
| 106 | - $this->spreadUpdateWith($update, $plugins); |
|
| 107 | - } |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - /** |
|
| 111 | - * Match the message to the plugins |
|
| 112 | - * |
|
| 113 | - * @param ?Update $update The update to match |
|
| 114 | - * @return void |
|
| 115 | - */ |
|
| 116 | - public function loadPlugins(Update $update = null): void |
|
| 117 | - { |
|
| 118 | - $update = $update ?? ($this->update ?? Telegram::getUpdate()); |
|
| 119 | - $this->loadPluginsWith($this->plugins, $update); |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * Load the environment's |
|
| 124 | - * |
|
| 125 | - * @param string $path |
|
| 126 | - * @retrun void |
|
| 127 | - */ |
|
| 128 | - public function loadFileOfEnv(string $path): void |
|
| 129 | - { |
|
| 130 | - DotEnv::load($path); |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - /** |
|
| 134 | - * Update the configuration |
|
| 135 | - * |
|
| 136 | - * @param array $configuration |
|
| 137 | - * @return void |
|
| 138 | - */ |
|
| 139 | - public function updateConfiguration(array $configuration): void |
|
| 140 | - { |
|
| 141 | - $this->config = array_merge($this->config, $configuration); |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - /** |
|
| 145 | - * Resolve the request. |
|
| 146 | - * |
|
| 147 | - * @param ?Update $update The custom to work with |
|
| 148 | - * @param array $config The configuration of the receiver |
|
| 149 | - * |
|
| 150 | - * @retrun void |
|
| 151 | - */ |
|
| 152 | - public function resolve(Update $update = null, array $config = []): void |
|
| 153 | - { |
|
| 154 | - $method = '__process'; |
|
| 155 | - if (!method_exists($this, $method)) { |
|
| 156 | - throw new \RuntimeException(sprintf('The method %s does not exist', $method)); |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - if (is_array($config)) $this->updateConfiguration($config); |
|
| 160 | - |
|
| 161 | - if (!empty($update)) $this->update = $update; |
|
| 162 | - else $this->update = Telegram::getUpdate() !== false ? Telegram::getUpdate() : null; |
|
| 163 | - |
|
| 164 | - if (empty($this->update)) { |
|
| 165 | - TelegramLog::error( |
|
| 166 | - 'The update is empty, the request is not processed' |
|
| 167 | - ); |
|
| 168 | - return; |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - putenv('TG_CURRENT_UPDATE=' . $this->update->getRawData(false)); |
|
| 172 | - |
|
| 173 | - $this->$method($update); |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - /** |
|
| 177 | - * This function will get update and spread it to the plugins |
|
| 178 | - * |
|
| 179 | - * @param Update $update |
|
| 180 | - * @param array<Plugin> $plugins |
|
| 181 | - * @return void |
|
| 182 | - */ |
|
| 183 | - private function spreadUpdateWith(Update $update, array $plugins): void |
|
| 184 | - { |
|
| 185 | - $this->isActive = true; |
|
| 186 | - |
|
| 187 | - foreach ($plugins as $plugin) { |
|
| 188 | - /** @var Plugin $plugin */ |
|
| 189 | - (new $plugin())->__execute($this, $update); |
|
| 190 | - if ($this->isActive === false) break; |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - $this->isActive = false; |
|
| 194 | - } |
|
| 195 | - |
|
| 196 | - /** |
|
| 197 | - * Kill the speeding process |
|
| 198 | - * |
|
| 199 | - * @return void |
|
| 200 | - */ |
|
| 201 | - public function kill(): void |
|
| 202 | - { |
|
| 203 | - $this->isActive = false; |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - /** |
|
| 207 | - * Use this method on the webhook class to get the updates |
|
| 208 | - * |
|
| 209 | - * @param Update $update |
|
| 210 | - * @return void |
|
| 211 | - */ |
|
| 212 | - abstract public function __process(Update $update): void; |
|
| 213 | - |
|
| 214 | - /** |
|
| 215 | - * put CrossData into the plugins |
|
| 216 | - * |
|
| 217 | - * @param string $key |
|
| 218 | - * @param mixed $value |
|
| 219 | - * @return void |
|
| 220 | - */ |
|
| 221 | - public function putCrossData(string $key, mixed $value): void |
|
| 222 | - { |
|
| 223 | - CrossData::put($key, $value); |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - /** |
|
| 227 | - * get CrossData from the plugins |
|
| 228 | - * |
|
| 229 | - * @param string $key |
|
| 230 | - * @return string|array|bool|null |
|
| 231 | - */ |
|
| 232 | - public function getCrossData(string $key): string|array|bool|null |
|
| 233 | - { |
|
| 234 | - return CrossData::get($key); |
|
| 235 | - } |
|
| 20 | + /** |
|
| 21 | + * @var ?Update |
|
| 22 | + */ |
|
| 23 | + protected ?Update $update; |
|
| 24 | + |
|
| 25 | + /** |
|
| 26 | + * @var array<Plugin> |
|
| 27 | + */ |
|
| 28 | + private array $plugins = []; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * @var bool |
|
| 32 | + */ |
|
| 33 | + private bool $isActive = false; |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * The default configuration of the webhook. |
|
| 37 | + * |
|
| 38 | + * @var array |
|
| 39 | + */ |
|
| 40 | + private array $config = [ |
|
| 41 | + 'autoload_env_file' => false, |
|
| 42 | + 'env_file_path' => null, |
|
| 43 | + ]; |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * Webhook constructor. |
|
| 47 | + * |
|
| 48 | + * @param string $api_key The API key of the bot. Leave it blank for autoload from .env file. |
|
| 49 | + */ |
|
| 50 | + public function __construct(string $api_key = '') |
|
| 51 | + { |
|
| 52 | + parent::__construct($api_key); |
|
| 53 | + |
|
| 54 | + if (!Telegram::validateToken(self::getApiKey())) { |
|
| 55 | + throw new InvalidBotTokenException(); |
|
| 56 | + } |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * Initialize the receiver. |
|
| 61 | + * |
|
| 62 | + * @return void |
|
| 63 | + */ |
|
| 64 | + public function init(): void |
|
| 65 | + { |
|
| 66 | + $this->config['env_file_path'] = $_SERVER['DOCUMENT_ROOT'] . '/.env'; |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * Add a plugin to the receiver |
|
| 71 | + * |
|
| 72 | + * @param array<Plugin> $plugins |
|
| 73 | + */ |
|
| 74 | + public function addPlugin(array $plugins): void |
|
| 75 | + { |
|
| 76 | + foreach ($plugins as $plugin) { |
|
| 77 | + if (!is_subclass_of($plugin, Plugin::class)) { |
|
| 78 | + throw new \RuntimeException( |
|
| 79 | + sprintf('The plugin %s must be an instance of %s', get_class($plugin), Plugin::class) |
|
| 80 | + ); |
|
| 81 | + } |
|
| 82 | + $this->plugins[] = $plugin; |
|
| 83 | + } |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * Match the update with the given plugins |
|
| 88 | + * |
|
| 89 | + * @param array<Plugin> $plugins |
|
| 90 | + * @param ?Update $update The update to match |
|
| 91 | + * @return void |
|
| 92 | + */ |
|
| 93 | + public function loadPluginsWith(array $plugins, Update $update = null): void |
|
| 94 | + { |
|
| 95 | + $update = $update ?? ($this->update ?? Telegram::getUpdate()); |
|
| 96 | + |
|
| 97 | + foreach ($plugins as $plugin) { |
|
| 98 | + if (!is_subclass_of($plugin, Plugin::class)) { |
|
| 99 | + throw new \InvalidArgumentException( |
|
| 100 | + sprintf('The plugin %s must be an instance of %s', get_class($plugin), Plugin::class) |
|
| 101 | + ); |
|
| 102 | + } |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + if ($update instanceof Update) { |
|
| 106 | + $this->spreadUpdateWith($update, $plugins); |
|
| 107 | + } |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + /** |
|
| 111 | + * Match the message to the plugins |
|
| 112 | + * |
|
| 113 | + * @param ?Update $update The update to match |
|
| 114 | + * @return void |
|
| 115 | + */ |
|
| 116 | + public function loadPlugins(Update $update = null): void |
|
| 117 | + { |
|
| 118 | + $update = $update ?? ($this->update ?? Telegram::getUpdate()); |
|
| 119 | + $this->loadPluginsWith($this->plugins, $update); |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * Load the environment's |
|
| 124 | + * |
|
| 125 | + * @param string $path |
|
| 126 | + * @retrun void |
|
| 127 | + */ |
|
| 128 | + public function loadFileOfEnv(string $path): void |
|
| 129 | + { |
|
| 130 | + DotEnv::load($path); |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + /** |
|
| 134 | + * Update the configuration |
|
| 135 | + * |
|
| 136 | + * @param array $configuration |
|
| 137 | + * @return void |
|
| 138 | + */ |
|
| 139 | + public function updateConfiguration(array $configuration): void |
|
| 140 | + { |
|
| 141 | + $this->config = array_merge($this->config, $configuration); |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + /** |
|
| 145 | + * Resolve the request. |
|
| 146 | + * |
|
| 147 | + * @param ?Update $update The custom to work with |
|
| 148 | + * @param array $config The configuration of the receiver |
|
| 149 | + * |
|
| 150 | + * @retrun void |
|
| 151 | + */ |
|
| 152 | + public function resolve(Update $update = null, array $config = []): void |
|
| 153 | + { |
|
| 154 | + $method = '__process'; |
|
| 155 | + if (!method_exists($this, $method)) { |
|
| 156 | + throw new \RuntimeException(sprintf('The method %s does not exist', $method)); |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + if (is_array($config)) $this->updateConfiguration($config); |
|
| 160 | + |
|
| 161 | + if (!empty($update)) $this->update = $update; |
|
| 162 | + else $this->update = Telegram::getUpdate() !== false ? Telegram::getUpdate() : null; |
|
| 163 | + |
|
| 164 | + if (empty($this->update)) { |
|
| 165 | + TelegramLog::error( |
|
| 166 | + 'The update is empty, the request is not processed' |
|
| 167 | + ); |
|
| 168 | + return; |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + putenv('TG_CURRENT_UPDATE=' . $this->update->getRawData(false)); |
|
| 172 | + |
|
| 173 | + $this->$method($update); |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + /** |
|
| 177 | + * This function will get update and spread it to the plugins |
|
| 178 | + * |
|
| 179 | + * @param Update $update |
|
| 180 | + * @param array<Plugin> $plugins |
|
| 181 | + * @return void |
|
| 182 | + */ |
|
| 183 | + private function spreadUpdateWith(Update $update, array $plugins): void |
|
| 184 | + { |
|
| 185 | + $this->isActive = true; |
|
| 186 | + |
|
| 187 | + foreach ($plugins as $plugin) { |
|
| 188 | + /** @var Plugin $plugin */ |
|
| 189 | + (new $plugin())->__execute($this, $update); |
|
| 190 | + if ($this->isActive === false) break; |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + $this->isActive = false; |
|
| 194 | + } |
|
| 195 | + |
|
| 196 | + /** |
|
| 197 | + * Kill the speeding process |
|
| 198 | + * |
|
| 199 | + * @return void |
|
| 200 | + */ |
|
| 201 | + public function kill(): void |
|
| 202 | + { |
|
| 203 | + $this->isActive = false; |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + /** |
|
| 207 | + * Use this method on the webhook class to get the updates |
|
| 208 | + * |
|
| 209 | + * @param Update $update |
|
| 210 | + * @return void |
|
| 211 | + */ |
|
| 212 | + abstract public function __process(Update $update): void; |
|
| 213 | + |
|
| 214 | + /** |
|
| 215 | + * put CrossData into the plugins |
|
| 216 | + * |
|
| 217 | + * @param string $key |
|
| 218 | + * @param mixed $value |
|
| 219 | + * @return void |
|
| 220 | + */ |
|
| 221 | + public function putCrossData(string $key, mixed $value): void |
|
| 222 | + { |
|
| 223 | + CrossData::put($key, $value); |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + /** |
|
| 227 | + * get CrossData from the plugins |
|
| 228 | + * |
|
| 229 | + * @param string $key |
|
| 230 | + * @return string|array|bool|null |
|
| 231 | + */ |
|
| 232 | + public function getCrossData(string $key): string|array|bool|null |
|
| 233 | + { |
|
| 234 | + return CrossData::get($key); |
|
| 235 | + } |
|
| 236 | 236 | |
| 237 | 237 | } |
| 238 | 238 | \ No newline at end of file |
@@ -35,49 +35,49 @@ |
||
| 35 | 35 | class InlineKeyboardButton extends KeyboardButton |
| 36 | 36 | { |
| 37 | 37 | |
| 38 | - /** |
|
| 39 | - * Creates instance of InlineKeyboardButton |
|
| 40 | - * |
|
| 41 | - * @param string $string |
|
| 42 | - * @return InlineKeyboardButton |
|
| 43 | - */ |
|
| 44 | - public static function make(string $string): InlineKeyboardButton |
|
| 45 | - { |
|
| 46 | - return new self($string); |
|
| 47 | - } |
|
| 38 | + /** |
|
| 39 | + * Creates instance of InlineKeyboardButton |
|
| 40 | + * |
|
| 41 | + * @param string $string |
|
| 42 | + * @return InlineKeyboardButton |
|
| 43 | + */ |
|
| 44 | + public static function make(string $string): InlineKeyboardButton |
|
| 45 | + { |
|
| 46 | + return new self($string); |
|
| 47 | + } |
|
| 48 | 48 | |
| 49 | - /** |
|
| 50 | - * @param string $data |
|
| 51 | - * @return $this |
|
| 52 | - */ |
|
| 53 | - public function CallbackData(string $data): InlineKeyboardButton |
|
| 54 | - { |
|
| 55 | - $this->raw_data['callback_data'] = $data; |
|
| 49 | + /** |
|
| 50 | + * @param string $data |
|
| 51 | + * @return $this |
|
| 52 | + */ |
|
| 53 | + public function CallbackData(string $data): InlineKeyboardButton |
|
| 54 | + { |
|
| 55 | + $this->raw_data['callback_data'] = $data; |
|
| 56 | 56 | |
| 57 | - return $this; |
|
| 58 | - } |
|
| 57 | + return $this; |
|
| 58 | + } |
|
| 59 | 59 | |
| 60 | - /** |
|
| 61 | - * @param string $data |
|
| 62 | - * @return $this |
|
| 63 | - */ |
|
| 64 | - public function Url(string $data): InlineKeyboardButton |
|
| 65 | - { |
|
| 66 | - $this->raw_data['url'] = $data; |
|
| 60 | + /** |
|
| 61 | + * @param string $data |
|
| 62 | + * @return $this |
|
| 63 | + */ |
|
| 64 | + public function Url(string $data): InlineKeyboardButton |
|
| 65 | + { |
|
| 66 | + $this->raw_data['url'] = $data; |
|
| 67 | 67 | |
| 68 | - return $this; |
|
| 69 | - } |
|
| 68 | + return $this; |
|
| 69 | + } |
|
| 70 | 70 | |
| 71 | - /** |
|
| 72 | - * @param string $url |
|
| 73 | - * @return $this |
|
| 74 | - */ |
|
| 75 | - public function WebApp(string $url): KeyboardButton |
|
| 76 | - { |
|
| 77 | - $this->raw_data['web_app'] = new WebAppInfo(['url' => $url]); |
|
| 71 | + /** |
|
| 72 | + * @param string $url |
|
| 73 | + * @return $this |
|
| 74 | + */ |
|
| 75 | + public function WebApp(string $url): KeyboardButton |
|
| 76 | + { |
|
| 77 | + $this->raw_data['web_app'] = new WebAppInfo(['url' => $url]); |
|
| 78 | 78 | |
| 79 | - return $this; |
|
| 80 | - } |
|
| 79 | + return $this; |
|
| 80 | + } |
|
| 81 | 81 | |
| 82 | 82 | /** |
| 83 | 83 | * Check if the passed data array could be an InlineKeyboardButton. |
@@ -12,37 +12,37 @@ |
||
| 12 | 12 | class CrossData |
| 13 | 13 | { |
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * get CrossData |
|
| 17 | - * |
|
| 18 | - * @param string $key |
|
| 19 | - * @return string|array|bool|null |
|
| 20 | - */ |
|
| 21 | - public static function get(string $key): string|array|bool|null |
|
| 22 | - { |
|
| 23 | - $data = json_decode(getenv('TG_CROSS_DATA'), true); |
|
| 24 | - $block = 'TG_CROSS_DATA' . $key; |
|
| 15 | + /** |
|
| 16 | + * get CrossData |
|
| 17 | + * |
|
| 18 | + * @param string $key |
|
| 19 | + * @return string|array|bool|null |
|
| 20 | + */ |
|
| 21 | + public static function get(string $key): string|array|bool|null |
|
| 22 | + { |
|
| 23 | + $data = json_decode(getenv('TG_CROSS_DATA'), true); |
|
| 24 | + $block = 'TG_CROSS_DATA' . $key; |
|
| 25 | 25 | |
| 26 | - if (!isset($data[$key])) { |
|
| 27 | - return null; |
|
| 28 | - } |
|
| 26 | + if (!isset($data[$key])) { |
|
| 27 | + return null; |
|
| 28 | + } |
|
| 29 | 29 | |
| 30 | - return getenv($data[$block]); |
|
| 31 | - } |
|
| 30 | + return getenv($data[$block]); |
|
| 31 | + } |
|
| 32 | 32 | |
| 33 | - /** |
|
| 34 | - * put CrossData |
|
| 35 | - * |
|
| 36 | - * @param string $key |
|
| 37 | - * @param mixed $value |
|
| 38 | - * @return void |
|
| 39 | - */ |
|
| 40 | - public static function put(string $key, mixed $value): void |
|
| 41 | - { |
|
| 42 | - $data = json_decode(getenv('TG_CROSS_DATA'), true); |
|
| 43 | - $data[$key] = 'TG_CROSS_DATA_' . $key; |
|
| 44 | - putenv($data[$key] . '=' . $value); |
|
| 45 | - putenv(json_encode($data)); |
|
| 46 | - } |
|
| 33 | + /** |
|
| 34 | + * put CrossData |
|
| 35 | + * |
|
| 36 | + * @param string $key |
|
| 37 | + * @param mixed $value |
|
| 38 | + * @return void |
|
| 39 | + */ |
|
| 40 | + public static function put(string $key, mixed $value): void |
|
| 41 | + { |
|
| 42 | + $data = json_decode(getenv('TG_CROSS_DATA'), true); |
|
| 43 | + $data[$key] = 'TG_CROSS_DATA_' . $key; |
|
| 44 | + putenv($data[$key] . '=' . $value); |
|
| 45 | + putenv(json_encode($data)); |
|
| 46 | + } |
|
| 47 | 47 | |
| 48 | 48 | } |
| 49 | 49 | \ No newline at end of file |
@@ -15,129 +15,129 @@ |
||
| 15 | 15 | class Plugin |
| 16 | 16 | { |
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * The Update types |
|
| 20 | - * |
|
| 21 | - * @var array |
|
| 22 | - */ |
|
| 23 | - protected array $update_types = [ |
|
| 24 | - 'Message', |
|
| 25 | - 'EditedMessage', |
|
| 26 | - 'ChannelPost', |
|
| 27 | - 'EditedChannelPost', |
|
| 28 | - 'InlineQuery', |
|
| 29 | - 'ChosenInlineResult', |
|
| 30 | - 'CallbackQuery', |
|
| 31 | - 'ShippingQuery', |
|
| 32 | - 'PreCheckoutQuery', |
|
| 33 | - ]; |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * @var WebhookHandler |
|
| 37 | - */ |
|
| 38 | - protected WebhookHandler $hook; |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * @var \Generator |
|
| 42 | - */ |
|
| 43 | - protected \Generator $returns; |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * @var bool |
|
| 47 | - */ |
|
| 48 | - protected bool $kill_on_yield = false; |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * Check for the exit of the plugin. |
|
| 52 | - * |
|
| 53 | - * @param \Generator $return |
|
| 54 | - * @return void |
|
| 55 | - */ |
|
| 56 | - public function __checkExit(\Generator $return): void |
|
| 57 | - { |
|
| 58 | - if ($return->valid()) { |
|
| 59 | - if ($return->current() !== null && $this->kill_on_yield === true) { |
|
| 60 | - $this->kill(); |
|
| 61 | - } |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - if ($return->valid()) { |
|
| 65 | - $return->next(); |
|
| 66 | - $this->__checkExit($return); |
|
| 67 | - } |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * Identify the update type. e.g. Message, EditedMessage, etc. |
|
| 72 | - * |
|
| 73 | - * @param Update $update |
|
| 74 | - * @return string|null |
|
| 75 | - */ |
|
| 76 | - public function __identify(Update $update): ?string |
|
| 77 | - { |
|
| 78 | - $type = $update->getUpdateType(); |
|
| 79 | - |
|
| 80 | - if ($type === null) { |
|
| 81 | - return null; |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - return str_replace('_', '', ucwords($type, '_')); |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * Execute the plugin. |
|
| 89 | - * |
|
| 90 | - * @param WebhookHandler $receiver |
|
| 91 | - * @param Update $update |
|
| 92 | - * @return void |
|
| 93 | - */ |
|
| 94 | - public function __execute(WebhookHandler $receiver, Update $update): void |
|
| 95 | - { |
|
| 96 | - $this->hook = $receiver; |
|
| 97 | - |
|
| 98 | - if (method_exists($this, 'onReceivedUpdate')) { |
|
| 99 | - $return = $this->onReceivedUpdate($update); |
|
| 100 | - $this->__checkExit($return); |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - $type = $this->__identify($update); |
|
| 104 | - if (method_exists($this, ($method = 'on' . $type)) && $type !== null) { |
|
| 105 | - $return = $this->$method($update); |
|
| 106 | - $this->__checkExit($return); |
|
| 107 | - } |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - /** |
|
| 111 | - * Kill the plugin. |
|
| 112 | - * |
|
| 113 | - * @return void |
|
| 114 | - */ |
|
| 115 | - public function kill(): void |
|
| 116 | - { |
|
| 117 | - $this->hook->kill(); |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - /** |
|
| 121 | - * put CrossData into the plugins |
|
| 122 | - * |
|
| 123 | - * @param string $key |
|
| 124 | - * @param mixed $value |
|
| 125 | - * @return void |
|
| 126 | - */ |
|
| 127 | - public function putCrossData(string $key, mixed $value): void |
|
| 128 | - { |
|
| 129 | - CrossData::put($key, $value); |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - /** |
|
| 133 | - * get CrossData from the plugins |
|
| 134 | - * |
|
| 135 | - * @param string $key |
|
| 136 | - * @return string|array|bool|null |
|
| 137 | - */ |
|
| 138 | - public function getCrossData(string $key): string|array|bool|null |
|
| 139 | - { |
|
| 140 | - return CrossData::get($key); |
|
| 141 | - } |
|
| 18 | + /** |
|
| 19 | + * The Update types |
|
| 20 | + * |
|
| 21 | + * @var array |
|
| 22 | + */ |
|
| 23 | + protected array $update_types = [ |
|
| 24 | + 'Message', |
|
| 25 | + 'EditedMessage', |
|
| 26 | + 'ChannelPost', |
|
| 27 | + 'EditedChannelPost', |
|
| 28 | + 'InlineQuery', |
|
| 29 | + 'ChosenInlineResult', |
|
| 30 | + 'CallbackQuery', |
|
| 31 | + 'ShippingQuery', |
|
| 32 | + 'PreCheckoutQuery', |
|
| 33 | + ]; |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * @var WebhookHandler |
|
| 37 | + */ |
|
| 38 | + protected WebhookHandler $hook; |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * @var \Generator |
|
| 42 | + */ |
|
| 43 | + protected \Generator $returns; |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * @var bool |
|
| 47 | + */ |
|
| 48 | + protected bool $kill_on_yield = false; |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * Check for the exit of the plugin. |
|
| 52 | + * |
|
| 53 | + * @param \Generator $return |
|
| 54 | + * @return void |
|
| 55 | + */ |
|
| 56 | + public function __checkExit(\Generator $return): void |
|
| 57 | + { |
|
| 58 | + if ($return->valid()) { |
|
| 59 | + if ($return->current() !== null && $this->kill_on_yield === true) { |
|
| 60 | + $this->kill(); |
|
| 61 | + } |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + if ($return->valid()) { |
|
| 65 | + $return->next(); |
|
| 66 | + $this->__checkExit($return); |
|
| 67 | + } |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * Identify the update type. e.g. Message, EditedMessage, etc. |
|
| 72 | + * |
|
| 73 | + * @param Update $update |
|
| 74 | + * @return string|null |
|
| 75 | + */ |
|
| 76 | + public function __identify(Update $update): ?string |
|
| 77 | + { |
|
| 78 | + $type = $update->getUpdateType(); |
|
| 79 | + |
|
| 80 | + if ($type === null) { |
|
| 81 | + return null; |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + return str_replace('_', '', ucwords($type, '_')); |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * Execute the plugin. |
|
| 89 | + * |
|
| 90 | + * @param WebhookHandler $receiver |
|
| 91 | + * @param Update $update |
|
| 92 | + * @return void |
|
| 93 | + */ |
|
| 94 | + public function __execute(WebhookHandler $receiver, Update $update): void |
|
| 95 | + { |
|
| 96 | + $this->hook = $receiver; |
|
| 97 | + |
|
| 98 | + if (method_exists($this, 'onReceivedUpdate')) { |
|
| 99 | + $return = $this->onReceivedUpdate($update); |
|
| 100 | + $this->__checkExit($return); |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + $type = $this->__identify($update); |
|
| 104 | + if (method_exists($this, ($method = 'on' . $type)) && $type !== null) { |
|
| 105 | + $return = $this->$method($update); |
|
| 106 | + $this->__checkExit($return); |
|
| 107 | + } |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + /** |
|
| 111 | + * Kill the plugin. |
|
| 112 | + * |
|
| 113 | + * @return void |
|
| 114 | + */ |
|
| 115 | + public function kill(): void |
|
| 116 | + { |
|
| 117 | + $this->hook->kill(); |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + /** |
|
| 121 | + * put CrossData into the plugins |
|
| 122 | + * |
|
| 123 | + * @param string $key |
|
| 124 | + * @param mixed $value |
|
| 125 | + * @return void |
|
| 126 | + */ |
|
| 127 | + public function putCrossData(string $key, mixed $value): void |
|
| 128 | + { |
|
| 129 | + CrossData::put($key, $value); |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + /** |
|
| 133 | + * get CrossData from the plugins |
|
| 134 | + * |
|
| 135 | + * @param string $key |
|
| 136 | + * @return string|array|bool|null |
|
| 137 | + */ |
|
| 138 | + public function getCrossData(string $key): string|array|bool|null |
|
| 139 | + { |
|
| 140 | + return CrossData::get($key); |
|
| 141 | + } |
|
| 142 | 142 | |
| 143 | 143 | } |
| 144 | 144 | \ No newline at end of file |
@@ -12,63 +12,63 @@ |
||
| 12 | 12 | class Common |
| 13 | 13 | { |
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * Validate the given string is JSON or not |
|
| 17 | - * |
|
| 18 | - * @param ?string $string |
|
| 19 | - * @return bool |
|
| 20 | - */ |
|
| 21 | - public static function isJson(?string $string): bool |
|
| 22 | - { |
|
| 23 | - if (!is_string($string)) { |
|
| 24 | - return false; |
|
| 25 | - } |
|
| 15 | + /** |
|
| 16 | + * Validate the given string is JSON or not |
|
| 17 | + * |
|
| 18 | + * @param ?string $string |
|
| 19 | + * @return bool |
|
| 20 | + */ |
|
| 21 | + public static function isJson(?string $string): bool |
|
| 22 | + { |
|
| 23 | + if (!is_string($string)) { |
|
| 24 | + return false; |
|
| 25 | + } |
|
| 26 | 26 | |
| 27 | - json_decode($string); |
|
| 27 | + json_decode($string); |
|
| 28 | 28 | |
| 29 | - return json_last_error() === JSON_ERROR_NONE; |
|
| 30 | - } |
|
| 29 | + return json_last_error() === JSON_ERROR_NONE; |
|
| 30 | + } |
|
| 31 | 31 | |
| 32 | - /** |
|
| 33 | - * Check string is a url encoded string or not |
|
| 34 | - * |
|
| 35 | - * @param ?string $string |
|
| 36 | - * @return bool |
|
| 37 | - */ |
|
| 38 | - public static function isUrlEncode(?string $string): bool |
|
| 39 | - { |
|
| 40 | - if (!is_string($string)) { |
|
| 41 | - return false; |
|
| 42 | - } |
|
| 32 | + /** |
|
| 33 | + * Check string is a url encoded string or not |
|
| 34 | + * |
|
| 35 | + * @param ?string $string |
|
| 36 | + * @return bool |
|
| 37 | + */ |
|
| 38 | + public static function isUrlEncode(?string $string): bool |
|
| 39 | + { |
|
| 40 | + if (!is_string($string)) { |
|
| 41 | + return false; |
|
| 42 | + } |
|
| 43 | 43 | |
| 44 | - return preg_match('/%[0-9A-F]{2}/i', $string); |
|
| 45 | - } |
|
| 44 | + return preg_match('/%[0-9A-F]{2}/i', $string); |
|
| 45 | + } |
|
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * Convert url encoded string to array |
|
| 49 | - * |
|
| 50 | - * @param string $string |
|
| 51 | - * @return array |
|
| 52 | - */ |
|
| 53 | - public static function urlDecode(string $string): array |
|
| 54 | - { |
|
| 55 | - $raw_data = explode('&', urldecode($string)); |
|
| 56 | - $data = []; |
|
| 47 | + /** |
|
| 48 | + * Convert url encoded string to array |
|
| 49 | + * |
|
| 50 | + * @param string $string |
|
| 51 | + * @return array |
|
| 52 | + */ |
|
| 53 | + public static function urlDecode(string $string): array |
|
| 54 | + { |
|
| 55 | + $raw_data = explode('&', urldecode($string)); |
|
| 56 | + $data = []; |
|
| 57 | 57 | |
| 58 | - foreach ($raw_data as $row) { |
|
| 59 | - [$key, $value] = explode('=', $row); |
|
| 58 | + foreach ($raw_data as $row) { |
|
| 59 | + [$key, $value] = explode('=', $row); |
|
| 60 | 60 | |
| 61 | - if (self::isUrlEncode($value)) { |
|
| 62 | - $value = urldecode($value); |
|
| 63 | - if (self::isJson($value)) { |
|
| 64 | - $value = json_decode($value, true); |
|
| 65 | - } |
|
| 66 | - } |
|
| 61 | + if (self::isUrlEncode($value)) { |
|
| 62 | + $value = urldecode($value); |
|
| 63 | + if (self::isJson($value)) { |
|
| 64 | + $value = json_decode($value, true); |
|
| 65 | + } |
|
| 66 | + } |
|
| 67 | 67 | |
| 68 | - $data[$key] = $value; |
|
| 69 | - } |
|
| 68 | + $data[$key] = $value; |
|
| 69 | + } |
|
| 70 | 70 | |
| 71 | - return $data; |
|
| 72 | - } |
|
| 71 | + return $data; |
|
| 72 | + } |
|
| 73 | 73 | |
| 74 | 74 | } |
| 75 | 75 | \ No newline at end of file |