1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TelegramBot\Util; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class Common |
7
|
|
|
* |
8
|
|
|
* @link https://github.com/telegram-bot-php/core |
9
|
|
|
* @author Shahrad Elahi (https://github.com/shahradelahi) |
10
|
|
|
* @license https://github.com/telegram-bot-php/core/blob/master/LICENSE (MIT License) |
11
|
|
|
*/ |
12
|
|
|
class Common |
13
|
|
|
{ |
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)) return false; |
24
|
|
|
json_decode($string); |
25
|
|
|
return json_last_error() === JSON_ERROR_NONE; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Debug mode |
30
|
|
|
* |
31
|
|
|
* @param ?int $admin_id Fill this or use setAdmin() |
32
|
|
|
* @return void |
33
|
|
|
*/ |
34
|
|
|
public static function setDebugMode(?int $admin_id = null): void |
35
|
|
|
{ |
36
|
|
|
error_reporting(E_ALL); |
37
|
|
|
ini_set('display_errors', 1); |
38
|
|
|
defined('DEBUG_MODE') or define('DEBUG_MODE', true); |
39
|
|
|
if ($admin_id) { |
|
|
|
|
40
|
|
|
defined('TG_ADMIN_ID') or define('TG_ADMIN_ID', $admin_id); |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Arrest with exception |
46
|
|
|
* |
47
|
|
|
* @param mixed $class The class |
48
|
|
|
* @param string $callback The method |
49
|
|
|
* @param mixed ...$args The arguments to pass to the callback |
50
|
|
|
* @return mixed |
51
|
|
|
*/ |
52
|
|
|
public static function arrest(mixed $class, string $callback, ...$args): mixed |
53
|
|
|
{ |
54
|
|
|
try { |
55
|
|
|
return call_user_func_array([$class, $callback], $args); |
56
|
|
|
} catch (\Throwable|\TypeError|\Exception $e) { |
57
|
|
|
if (defined('DEBUG_MODE') && DEBUG_MODE) { |
58
|
|
|
echo '<b>TelegramError:</b> ' . $e->getMessage() . PHP_EOL; |
59
|
|
|
} |
60
|
|
|
throw new \RuntimeException($e->getMessage(), $e->getCode(), $e); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Check string is a url encoded string or not |
66
|
|
|
* |
67
|
|
|
* @param string $string |
68
|
|
|
* @return bool |
69
|
|
|
*/ |
70
|
|
|
public static function isUrlEncode(string $string): bool |
71
|
|
|
{ |
72
|
|
|
return preg_match('/%[0-9A-F]{2}/i', $string); |
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Convert url encoded string to array |
77
|
|
|
* |
78
|
|
|
* @param string $string |
79
|
|
|
* @return array |
80
|
|
|
*/ |
81
|
|
|
public static function urlDecode(string $string): array |
82
|
|
|
{ |
83
|
|
|
$raw_data = explode('&', urldecode($string)); |
84
|
|
|
$data = []; |
85
|
|
|
|
86
|
|
|
foreach ($raw_data as $row) { |
87
|
|
|
[$key, $value] = explode('=', $row); |
88
|
|
|
|
89
|
|
|
if (self::isUrlEncode($value)) { |
90
|
|
|
$value = urldecode($value); |
91
|
|
|
if (self::isJson($value)) { |
92
|
|
|
$value = json_decode($value, true); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$data[$key] = $value; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $data; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
} |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: