1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TelegramBot; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class Entity |
7
|
|
|
* |
8
|
|
|
* This is the base class for all entities. |
9
|
|
|
* |
10
|
|
|
* @link https://core.telegram.org/bots/api#available-types |
11
|
|
|
*/ |
12
|
|
|
abstract class Entity |
13
|
|
|
{ |
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
|
|
|
{ |
27
|
|
|
if (array_key_exists('raw_data', $data)) { |
28
|
|
|
$this->raw_data = $data['raw_data']; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
$this->assignMemberVariables(($this->raw_data = $data)); |
32
|
|
|
$this->validate(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Get the raw data passed to this entity |
37
|
|
|
* |
38
|
|
|
* @param bool $associated |
39
|
|
|
* @return array|string |
40
|
|
|
*/ |
41
|
|
|
public function getRawData(bool $associated = true): array|string |
42
|
|
|
{ |
43
|
|
|
return $associated ? $this->raw_data : json_encode($this->raw_data); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Helper to set member variables |
48
|
|
|
* |
49
|
|
|
* @param array $data |
50
|
|
|
* @return void |
51
|
|
|
*/ |
52
|
|
|
protected function assignMemberVariables(array $data): void |
53
|
|
|
{ |
54
|
|
|
foreach ($data as $key => $value) { |
55
|
|
|
$this->$key = $value; |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Get a property from the current Entity |
61
|
|
|
* |
62
|
|
|
* @param string $property |
63
|
|
|
* @param mixed $default |
64
|
|
|
* |
65
|
|
|
* @return mixed |
66
|
|
|
*/ |
67
|
|
|
public function getProperty(string $property, mixed $default = null): mixed |
68
|
|
|
{ |
69
|
|
|
return $this->raw_data[$property] ?? $default; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Get the list of the properties that are themselves Entities |
74
|
|
|
* |
75
|
|
|
* @return array |
76
|
|
|
*/ |
77
|
|
|
protected function subEntities(): array |
78
|
|
|
{ |
79
|
|
|
return []; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Perform any special entity validation |
84
|
|
|
* |
85
|
|
|
* @return void |
86
|
|
|
*/ |
87
|
|
|
protected function validate(): void |
88
|
|
|
{ |
89
|
|
|
// Do nothing by default |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param string $name The name of the property |
94
|
|
|
* @param array $arguments The arguments passed to the method |
95
|
|
|
* @return mixed |
96
|
|
|
*/ |
97
|
|
|
public function __call(string $name, array $arguments): mixed |
98
|
|
|
{ |
99
|
|
|
if (method_exists($this, $name)) { |
100
|
|
|
return $this->{$name}(...$arguments); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
if (str_starts_with($name, 'get')) { |
104
|
|
|
$property_name = strtolower(ltrim(preg_replace('/[A-Z]/', '_$0', substr($name, 3)), '_')); |
105
|
|
|
|
106
|
|
|
$property = $this->getProperty($property_name); |
107
|
|
|
$sub_entities = $this->subEntities() ?? []; |
108
|
|
|
|
109
|
|
|
if (isset($sub_entities[$property_name])) { |
110
|
|
|
$class_name = $sub_entities[$property_name]; |
111
|
|
|
return Factory::resolveEntityClass($class_name, $property); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return $property ?? null; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
if (str_starts_with($name, 'set')) { |
118
|
|
|
$property_name = strtolower(ltrim(preg_replace('/[A-Z]/', '_$0', substr($name, 3)), '_')); |
119
|
|
|
|
120
|
|
|
if (property_exists($this, $property_name)) { |
121
|
|
|
$this->{$property_name} = $arguments[0]; |
122
|
|
|
$this->raw_data[$property_name] = $arguments[0]; |
123
|
|
|
|
124
|
|
|
return $this; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
throw new \BadMethodCallException("Method '$name' does not exist"); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Escape markdown (v1) special characters |
134
|
|
|
* |
135
|
|
|
* @see https://core.telegram.org/bots/api#markdown-style |
136
|
|
|
* |
137
|
|
|
* @param string $string |
138
|
|
|
* |
139
|
|
|
* @return string |
140
|
|
|
*/ |
141
|
|
|
public static function escapeMarkdown(string $string): string |
142
|
|
|
{ |
143
|
|
|
return str_replace( |
144
|
|
|
['[', '`', '*', '_',], |
145
|
|
|
['\[', '\`', '\*', '\_',], |
146
|
|
|
$string |
147
|
|
|
); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Escape markdown (v2) special characters |
152
|
|
|
* |
153
|
|
|
* @see https://core.telegram.org/bots/api#markdownv2-style |
154
|
|
|
* |
155
|
|
|
* @param string $string |
156
|
|
|
* |
157
|
|
|
* @return string |
158
|
|
|
*/ |
159
|
|
|
public static function escapeMarkdownV2(string $string): string |
160
|
|
|
{ |
161
|
|
|
return str_replace( |
162
|
|
|
['_', '*', '[', ']', '(', ')', '~', '`', '>', '#', '+', '-', '=', '|', '{', '}', '.', '!'], |
163
|
|
|
['\_', '\*', '\[', '\]', '\(', '\)', '\~', '\`', '\>', '\#', '\+', '\-', '\=', '\|', '\{', '\}', '\.', '\!'], |
164
|
|
|
$string |
165
|
|
|
); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
} |