1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the TelegramBot package. |
4
|
|
|
* |
5
|
|
|
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Longman\TelegramBot\Entities; |
12
|
|
|
|
13
|
|
|
use Exception; |
14
|
|
|
use Longman\TelegramBot\Entities\InlineQuery\InlineEntity; |
15
|
|
|
use ReflectionObject; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class Entity |
19
|
|
|
* |
20
|
|
|
* This is the base class for all entities. |
21
|
|
|
* |
22
|
|
|
* @link https://core.telegram.org/bots/api#available-types |
23
|
|
|
* |
24
|
|
|
* @method array getRawData() Get the raw data passed to this entity |
25
|
|
|
*/ |
26
|
|
|
abstract class Entity |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* Entity constructor. |
30
|
|
|
* |
31
|
|
|
* @todo Get rid of the $bot_name, it shouldn't be here! |
32
|
|
|
* |
33
|
|
|
* @param array $data |
34
|
|
|
* @param string $bot_name |
35
|
|
|
* |
36
|
|
|
* @throws \Longman\TelegramBot\Exception\TelegramException |
37
|
|
|
*/ |
38
|
78 |
|
public function __construct($data, $bot_name = '') |
39
|
|
|
{ |
40
|
|
|
//Make sure we're not raw_data inception-ing |
41
|
78 |
|
if (array_key_exists('raw_data', $data)) { |
42
|
14 |
|
if ($data['raw_data'] === null) { |
43
|
14 |
|
unset($data['raw_data']); |
44
|
|
|
} |
45
|
|
|
} else { |
46
|
72 |
|
$data['raw_data'] = $data; |
47
|
|
|
} |
48
|
|
|
|
49
|
78 |
|
$data['bot_name'] = $bot_name; |
50
|
78 |
|
$this->assignMemberVariables($data); |
51
|
78 |
|
$this->validate(); |
52
|
69 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Perform to json |
56
|
|
|
* |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
1 |
|
public function toJson() |
60
|
|
|
{ |
61
|
1 |
|
return json_encode($this->getRawData()); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Perform to string |
66
|
|
|
* |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
|
|
public function __toString() |
70
|
|
|
{ |
71
|
|
|
return $this->toJson(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Helper to set member variables |
76
|
|
|
* |
77
|
|
|
* @param array $data |
78
|
|
|
*/ |
79
|
78 |
|
protected function assignMemberVariables(array $data) |
80
|
|
|
{ |
81
|
78 |
|
foreach ($data as $key => $value) { |
82
|
78 |
|
$this->$key = $value; |
83
|
|
|
} |
84
|
78 |
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Get the list of the properties that are themselves Entities |
88
|
|
|
* |
89
|
|
|
* @return array |
90
|
|
|
*/ |
91
|
53 |
|
protected function subEntities() |
92
|
|
|
{ |
93
|
53 |
|
return []; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Perform any special entity validation |
98
|
|
|
* |
99
|
|
|
* @throws \Longman\TelegramBot\Exception\TelegramException |
100
|
|
|
*/ |
101
|
51 |
|
protected function validate() |
102
|
|
|
{ |
103
|
51 |
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Get a property from the current Entity |
107
|
|
|
* |
108
|
|
|
* @param mixed $property |
109
|
|
|
* @param mixed $default |
110
|
|
|
* |
111
|
|
|
* @return mixed |
112
|
|
|
*/ |
113
|
73 |
|
public function getProperty($property, $default = null) |
114
|
|
|
{ |
115
|
73 |
|
if (isset($this->$property)) { |
116
|
68 |
|
return $this->$property; |
117
|
|
|
} |
118
|
|
|
|
119
|
43 |
|
return $default; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Return the variable for the called getter or magically set properties dynamically. |
124
|
|
|
* |
125
|
|
|
* @param $method |
126
|
|
|
* @param $args |
127
|
|
|
* |
128
|
|
|
* @return mixed|null |
129
|
|
|
*/ |
130
|
59 |
|
public function __call($method, $args) |
131
|
|
|
{ |
132
|
|
|
//Convert method to snake_case (which is the name of the property) |
133
|
59 |
|
$property_name = ltrim(strtolower(preg_replace('/[A-Z]/', '_$0', substr($method, 3))), '_'); |
134
|
|
|
|
135
|
59 |
|
$action = substr($method, 0, 3); |
136
|
59 |
|
if ($action === 'get') { |
137
|
59 |
|
$property = $this->getProperty($property_name); |
138
|
|
|
|
139
|
59 |
|
if ($property !== null) { |
140
|
|
|
//Get all sub-Entities of the current Entity |
141
|
56 |
|
$sub_entities = $this->subEntities(); |
142
|
|
|
|
143
|
56 |
|
if (isset($sub_entities[$property_name])) { |
144
|
15 |
|
return new $sub_entities[$property_name]($property); |
145
|
|
|
} |
146
|
|
|
|
147
|
58 |
|
return $property; |
148
|
|
|
} |
149
|
3 |
|
} elseif ($action === 'set') { |
150
|
|
|
// Limit setters to specific classes. |
151
|
3 |
|
if ($this instanceof InlineEntity || $this instanceof Keyboard || $this instanceof KeyboardButton) { |
152
|
3 |
|
$this->$property_name = $args[0]; |
153
|
|
|
|
154
|
3 |
|
return $this; |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
158
|
29 |
|
return null; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Return an array of nice objects from an array of object arrays |
163
|
|
|
* |
164
|
|
|
* This method is used to generate pretty object arrays |
165
|
|
|
* mainly for PhotoSize and Entities object arrays. |
166
|
|
|
* |
167
|
|
|
* @param string $class |
168
|
|
|
* @param string $property |
169
|
|
|
* |
170
|
|
|
* @return array |
171
|
|
|
*/ |
172
|
6 |
|
protected function makePrettyObjectArray($class, $property) |
173
|
|
|
{ |
174
|
6 |
|
$new_objects = []; |
175
|
|
|
|
176
|
|
|
try { |
177
|
6 |
|
if ($objects = $this->getProperty($property)) { |
178
|
|
|
foreach ($objects as $object) { |
179
|
|
|
if (!empty($object)) { |
180
|
6 |
|
$new_objects[] = new $class($object); |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
} catch (Exception $e) { |
185
|
|
|
$new_objects = []; |
186
|
|
|
} |
187
|
|
|
|
188
|
6 |
|
return $new_objects; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Escape markdown special characters |
193
|
|
|
* |
194
|
|
|
* @param string $string |
195
|
|
|
* |
196
|
|
|
* @return string |
197
|
|
|
*/ |
198
|
1 |
|
public function escapeMarkdown($string) |
199
|
|
|
{ |
200
|
1 |
|
return str_replace( |
201
|
1 |
|
['[', '`', '*', '_',], |
202
|
1 |
|
['\[', '\`', '\*', '\_',], |
203
|
|
|
$string |
204
|
|
|
); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Try to mention the user |
209
|
|
|
* |
210
|
|
|
* Mention the user with the username otherwise print first and last name |
211
|
|
|
* if the $escape_markdown argument is true special characters are escaped from the output |
212
|
|
|
* |
213
|
|
|
* @param bool $escape_markdown |
214
|
|
|
* |
215
|
|
|
* @return string|null |
216
|
|
|
*/ |
217
|
3 |
|
public function tryMention($escape_markdown = false) |
218
|
|
|
{ |
219
|
|
|
//TryMention only makes sense for the User and Chat entity. |
220
|
3 |
|
if (!($this instanceof User || $this instanceof Chat)) { |
221
|
|
|
return null; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
//Try with the username first... |
225
|
3 |
|
$name = $this->getProperty('username'); |
226
|
3 |
|
$is_username = $name !== null; |
227
|
|
|
|
228
|
3 |
|
if ($name === null) { |
229
|
|
|
//...otherwise try with the names. |
230
|
3 |
|
$name = $this->getProperty('first_name'); |
231
|
3 |
|
$last_name = $this->getProperty('last_name'); |
232
|
3 |
|
if ($last_name !== null) { |
233
|
3 |
|
$name .= ' ' . $last_name; |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
|
237
|
3 |
|
if ($escape_markdown) { |
238
|
1 |
|
$name = $this->escapeMarkdown($name); |
239
|
|
|
} |
240
|
|
|
|
241
|
3 |
|
return ($is_username ? '@' : '') . $name; |
242
|
|
|
} |
243
|
|
|
} |
244
|
|
|
|