1 | <?php |
||
27 | abstract class Entity |
||
28 | { |
||
29 | /** |
||
30 | * Entity constructor. |
||
31 | * |
||
32 | * @todo Get rid of the $bot_username, it shouldn't be here! |
||
33 | * |
||
34 | * @param array $data |
||
35 | * @param string $bot_username |
||
36 | * |
||
37 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
38 | */ |
||
39 | 73 | public function __construct($data, $bot_username = '') |
|
40 | { |
||
41 | // Make sure we're not raw_data inception-ing |
||
42 | 73 | if (array_key_exists('raw_data', $data)) { |
|
43 | 1 | if ($data['raw_data'] === null) { |
|
44 | 1 | unset($data['raw_data']); |
|
45 | } |
||
46 | } else { |
||
47 | 72 | $data['raw_data'] = $data; |
|
48 | } |
||
49 | |||
50 | 73 | $data['bot_username'] = $bot_username; |
|
51 | |||
52 | 73 | $this->assignMemberVariables($data); |
|
53 | 73 | $this->validate(); |
|
54 | 64 | } |
|
55 | |||
56 | /** |
||
57 | * Perform to json |
||
58 | * |
||
59 | * @return string |
||
60 | */ |
||
61 | 1 | public function toJson() |
|
65 | |||
66 | /** |
||
67 | * Perform to string |
||
68 | * |
||
69 | * @return string |
||
70 | */ |
||
71 | public function __toString() |
||
75 | |||
76 | /** |
||
77 | * Helper to set member variables |
||
78 | * |
||
79 | * @param array $data |
||
80 | */ |
||
81 | 73 | protected function assignMemberVariables(array $data) |
|
87 | |||
88 | /** |
||
89 | * Get the list of the properties that are themselves Entities |
||
90 | * |
||
91 | * @return array |
||
92 | */ |
||
93 | 40 | protected function subEntities() |
|
97 | |||
98 | /** |
||
99 | * Perform any special entity validation |
||
100 | * |
||
101 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
102 | */ |
||
103 | 46 | protected function validate() |
|
106 | |||
107 | /** |
||
108 | * Get a property from the current Entity |
||
109 | * |
||
110 | * @param mixed $property |
||
111 | * @param mixed $default |
||
112 | * |
||
113 | * @return mixed |
||
114 | */ |
||
115 | 66 | public function getProperty($property, $default = null) |
|
123 | |||
124 | /** |
||
125 | * Return the variable for the called getter or magically set properties dynamically. |
||
126 | * |
||
127 | * @param $method |
||
128 | * @param $args |
||
129 | * |
||
130 | * @return mixed|null |
||
131 | */ |
||
132 | 53 | public function __call($method, $args) |
|
162 | |||
163 | /** |
||
164 | * Return an array of nice objects from an array of object arrays |
||
165 | * |
||
166 | * This method is used to generate pretty object arrays |
||
167 | * mainly for PhotoSize and Entities object arrays. |
||
168 | * |
||
169 | * @param string $class |
||
170 | * @param string $property |
||
171 | * |
||
172 | * @return array |
||
173 | */ |
||
174 | 6 | protected function makePrettyObjectArray($class, $property) |
|
175 | { |
||
176 | 6 | $new_objects = []; |
|
177 | |||
178 | try { |
||
179 | 6 | if ($objects = $this->getProperty($property)) { |
|
180 | foreach ($objects as $object) { |
||
181 | if (!empty($object)) { |
||
182 | 6 | $new_objects[] = new $class($object); |
|
183 | } |
||
184 | } |
||
185 | } |
||
186 | } catch (Exception $e) { |
||
187 | $new_objects = []; |
||
188 | } |
||
189 | |||
190 | 6 | return $new_objects; |
|
191 | } |
||
192 | |||
193 | /** |
||
194 | * Escape markdown special characters |
||
195 | * |
||
196 | * @param string $string |
||
197 | * |
||
198 | * @return string |
||
199 | */ |
||
200 | 1 | public function escapeMarkdown($string) |
|
201 | { |
||
202 | 1 | return str_replace( |
|
203 | 1 | ['[', '`', '*', '_',], |
|
204 | 1 | ['\[', '\`', '\*', '\_',], |
|
205 | 1 | $string |
|
206 | ); |
||
207 | } |
||
208 | |||
209 | /** |
||
210 | * Try to mention the user |
||
211 | * |
||
212 | * Mention the user with the username otherwise print first and last name |
||
213 | * if the $escape_markdown argument is true special characters are escaped from the output |
||
214 | * |
||
215 | * @param bool $escape_markdown |
||
216 | * |
||
217 | * @return string|null |
||
218 | */ |
||
219 | 3 | public function tryMention($escape_markdown = false) |
|
245 | } |
||
246 |