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