88
99namespace Longman \TelegramBot \Entities ;
1010
11+ use Longman \TelegramBot \Entities \Games \GameHighScore ;
12+ use Longman \TelegramBot \Request ;
13+
1114/**
1215 * Class ServerResponse
1316 *
@@ -103,32 +106,27 @@ public function printError($return = false)
103106 * @param string $bot_username
104107 *
105108 * @return \Longman\TelegramBot\Entities\Chat|\Longman\TelegramBot\Entities\ChatMember|\Longman\TelegramBot\Entities\File|\Longman\TelegramBot\Entities\Message|\Longman\TelegramBot\Entities\User|\Longman\TelegramBot\Entities\UserProfilePhotos|\Longman\TelegramBot\Entities\WebhookInfo
106- * @throws \Longman\TelegramBot\Exception\TelegramException
107109 */
108- private function createResultObject ($ result , $ bot_username )
110+ private function createResultObject (array $ result , $ bot_username )
109111 {
112+ $ action = Request::getCurrentAction ();
113+
110114 // We don't need to save the raw_data of the response object!
111115 $ result ['raw_data ' ] = null ;
112116
113117 $ result_object_types = [
114- 'total_count ' => ' UserProfilePhotos ' , //Response from getUserProfilePhotos
115- 'stickers ' => ' StickerSet ' , //Response from getStickerSet
116- 'file_id ' => ' File ' , //Response from getFile
117- 'title ' => ' Chat ' , //Response from getChat
118- 'username ' => ' User ' , //Response from getMe
119- 'user ' => ' ChatMember ' , //Response from getChatMember
120- 'url ' => ' WebhookInfo ' , //Response from getWebhookInfo
118+ 'getChat ' => Chat::class,
119+ 'getChatMember ' => ChatMember::class,
120+ 'getFile ' => File::class,
121+ 'getMe ' => User::class,
122+ 'getStickerSet ' => StickerSet::class,
123+ 'getUserProfilePhotos ' => UserProfilePhotos::class,
124+ 'getWebhookInfo ' => WebhookInfo::class,
121125 ];
122- foreach ($ result_object_types as $ type => $ object_class ) {
123- if (isset ($ result [$ type ])) {
124- $ object_class = __NAMESPACE__ . '\\' . $ object_class ;
125126
126- return new $ object_class ($ result );
127- }
128- }
127+ $ object_class = array_key_exists ($ action , $ result_object_types ) ? $ result_object_types [$ action ] : Message::class;
129128
130- //Response from sendMessage
131- return new Message ($ result , $ bot_username );
129+ return new $ object_class ($ result , $ bot_username );
132130 }
133131
134132 /**
@@ -137,28 +135,26 @@ private function createResultObject($result, $bot_username)
137135 * @param array $result
138136 * @param string $bot_username
139137 *
140- * @return null|\Longman\TelegramBot\Entities\ChatMember[]|\Longman\TelegramBot\Entities\Update[]
141- * @throws \Longman\TelegramBot\Exception\TelegramException
138+ * @return \Longman\TelegramBot\Entities\ChatMember[]|\Longman\TelegramBot\Entities\Games\GameHighScore[]|\Longman\TelegramBot\Entities\Message[]|\Longman\TelegramBot\Entities\Update[]
142139 */
143- private function createResultObjects ($ result , $ bot_username )
140+ private function createResultObjects (array $ result , $ bot_username )
144141 {
145142 $ results = [];
146- if (isset ($ result [0 ]['user ' ])) {
147- //Response from getChatAdministrators
148- foreach ($ result as $ user ) {
149- // We don't need to save the raw_data of the response object!
150- $ user ['raw_data ' ] = null ;
143+ $ action = Request::getCurrentAction ();
151144
152- $ results [] = new ChatMember ($ user );
153- }
154- } else {
155- //Get Update
156- foreach ($ result as $ update ) {
157- // We don't need to save the raw_data of the response object!
158- $ update ['raw_data ' ] = null ;
145+ $ result_object_types = [
146+ 'getChatAdministrators ' => ChatMember::class,
147+ 'getGameHighScores ' => GameHighScore::class,
148+ 'sendMediaGroup ' => Message::class,
149+ ];
159150
160- $ results [] = new Update ($ update , $ bot_username );
161- }
151+ $ object_class = array_key_exists ($ action , $ result_object_types ) ? $ result_object_types [$ action ] : Update::class;
152+
153+ foreach ($ result as $ data ) {
154+ // We don't need to save the raw_data of the response object!
155+ $ data ['raw_data ' ] = null ;
156+
157+ $ results [] = new $ object_class ($ data , $ bot_username );
162158 }
163159
164160 return $ results ;
0 commit comments