Skip to content

Commit ac2bbc9

Browse files
committed
Implemented magic __tostring in Entity
1 parent 3cc7ae7 commit ac2bbc9

File tree

2 files changed

+42
-63
lines changed

2 files changed

+42
-63
lines changed

src/DB.php

Lines changed: 37 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,6 @@ public static function insertUser(User $user, $date, Chat $chat = null)
164164
}
165165

166166
$user_id = $user->getId();
167-
$username = $user->getUsername();
168-
$first_name = $user->getFirstName();
169-
$last_name = $user->getLastName();
170-
171-
$chat_id = $chat->getId();
172167

173168
try {
174169
//Users table
@@ -184,9 +179,9 @@ public static function insertUser(User $user, $date, Chat $chat = null)
184179
');
185180

186181
$sth1->bindParam(':id', $user_id, \PDO::PARAM_INT);
187-
$sth1->bindParam(':username', $username, \PDO::PARAM_STR, 255);
188-
$sth1->bindParam(':first_name', $first_name, \PDO::PARAM_STR, 255);
189-
$sth1->bindParam(':last_name', $last_name, \PDO::PARAM_STR, 255);
182+
$sth1->bindParam(':username', $user->getUsername(), \PDO::PARAM_STR, 255);
183+
$sth1->bindParam(':first_name', $user->getFirstName(), \PDO::PARAM_STR, 255);
184+
$sth1->bindParam(':last_name', $user->getLastName(), \PDO::PARAM_STR, 255);
190185
$sth1->bindParam(':date', $date, \PDO::PARAM_STR);
191186

192187
$status = $sth1->execute();
@@ -207,7 +202,7 @@ public static function insertUser(User $user, $date, Chat $chat = null)
207202
');
208203

209204
$sth3->bindParam(':user_id', $user_id, \PDO::PARAM_INT);
210-
$sth3->bindParam(':chat_id', $chat_id, \PDO::PARAM_INT);
205+
$sth3->bindParam(':chat_id', $chat->getId(), \PDO::PARAM_INT);
211206

212207
$status = $sth3->execute();
213208

@@ -230,36 +225,20 @@ public static function insertRequest(Update $update)
230225
return false;
231226
}
232227

233-
$update_id = $update->getUpdateId();
234228
$message = $update->getMessage();
229+
235230
$from = $message->getFrom();
236231
$chat = $message->getChat();
237232

238-
$user_id = $from->getId();
239233

240234
$chat_id = $chat->getId();
241-
$chat_title = $chat->getTitle();
242235

243236
$date = self::toTimestamp($message->getDate());
244-
$message_id = $message->getMessageId();
245237
$forward_from = $message->getForwardFrom();
246238
$forward_date = self::toTimestamp($message->getForwardDate());
247-
$reply_to_message = $message->getReplyToMessage();
248-
$text = $message->getText();
249-
$audio = $message->getAudio();
250-
$document = $message->getDocument();
251239
$photo = $message->getPhoto();
252-
$sticker = $message->getSticker();
253-
$video = $message->getVideo();
254-
$voice = $message->getVoice();
255-
$caption = $message->getCaption();
256-
$contact = $message->getContact();
257-
$location = $message->getLocation();
258240
$new_chat_participant = $message->getNewChatParticipant();
259241
$left_chat_participant = $message->getLeftChatParticipant();
260-
$new_chat_title = $message->getNewChatTitle();
261-
$delete_chat_photo = $message->getDeleteChatPhoto();
262-
$group_chat_created = $message->getGroupChatCreated();
263242

264243
//inser user and the relation with the chat
265244
self::insertUser($from, $date, $chat);
@@ -300,7 +279,7 @@ public static function insertRequest(Update $update)
300279

301280

302281
$sth2->bindParam(':id', $chat_id, \PDO::PARAM_INT);
303-
$sth2->bindParam(':title', $chat_title, \PDO::PARAM_STR, 255);
282+
$sth2->bindParam(':title',$chat->getTitle() , \PDO::PARAM_STR, 255);
304283
$sth2->bindParam(':date', $date, \PDO::PARAM_STR);
305284

306285
$status = $sth2->execute();
@@ -322,60 +301,55 @@ public static function insertRequest(Update $update)
322301
)');
323302

324303

325-
$sth->bindParam(':update_id', $update_id, \PDO::PARAM_INT);
326-
$sth->bindParam(':message_id', $message_id, \PDO::PARAM_INT);
327-
$sth->bindParam(':user_id', $user_id, \PDO::PARAM_INT);
304+
$sth->bindParam(':update_id', $update->getUpdateId(), \PDO::PARAM_INT);
305+
$sth->bindParam(':message_id', $message->getMessageId(), \PDO::PARAM_INT);
306+
$sth->bindParam(':user_id', $from->getId(), \PDO::PARAM_INT);
328307
$sth->bindParam(':date', $date, \PDO::PARAM_STR);
329308
$sth->bindParam(':chat_id', $chat_id, \PDO::PARAM_STR);
330-
331309
$sth->bindParam(':forward_from', $forward_from, \PDO::PARAM_STR);
332-
333310
$sth->bindParam(':forward_date', $forward_date, \PDO::PARAM_STR);
334-
335-
$reply_to_message = is_object($reply_to_message) ? $reply_to_message->toJSON(): '';
336-
$sth->bindParam(':reply_to_message', $reply_to_message, \PDO::PARAM_STR);
337-
$sth->bindParam(':text', $text, \PDO::PARAM_STR);
338-
339-
$audio = is_object($audio) ? $audio->toJSON(): '';
340-
$sth->bindParam(':audio', $audio, \PDO::PARAM_STR);
341-
$document = is_object($document) ? $document->toJSON(): '';
342-
$sth->bindParam(':document', $document, \PDO::PARAM_STR);
343-
344-
//TODO what about the Entities array of photosize?
311+
$sth->bindParam(':reply_to_message', $message->getReplyToMessage(), \PDO::PARAM_STR);
312+
$sth->bindParam(':text', $message->getText(), \PDO::PARAM_STR);
313+
$sth->bindParam(':audio', $message->getAudio(), \PDO::PARAM_STR);
314+
$sth->bindParam(':document', $message->getDocument(), \PDO::PARAM_STR);
315+
//Try with to magic shoud work
345316
$var = [];
346317
if (is_array($photo)) {
347318
foreach ($photo as $elm) {
348-
$var[] = json_decode($elm->toJSON(), true);
319+
$var[] = json_decode($elm, true);
349320
}
350321

351322
$photo = json_encode($var);
352323
} else {
353-
$forward_from = '';
324+
$photo = '';
354325
}
355326

356327
$sth->bindParam(':photo', $photo, \PDO::PARAM_STR);
357-
358-
$sticker = is_object($sticker) ? $sticker->toJSON(): '';
359-
$sth->bindParam(':sticker', $sticker, \PDO::PARAM_STR);
360-
$video = is_object($video) ? $video->toJSON(): '';
361-
$sth->bindParam(':video', $video, \PDO::PARAM_STR);
362-
$voice = is_object($voice) ? $voice->toJSON(): '';
363-
$sth->bindParam(':voice', $voice, \PDO::PARAM_STR);
364-
$sth->bindParam(':caption', $caption, \PDO::PARAM_STR);
365-
$contact = is_object($contact) ? $contact->toJSON(): '';
366-
$sth->bindParam(':contact', $contact, \PDO::PARAM_STR);
367-
$location = is_object($location) ? $location->toJSON(): '';
368-
$sth->bindParam(':location', $location, \PDO::PARAM_STR);
369-
328+
$sth->bindParam(':sticker', $message->getSticker(), \PDO::PARAM_STR);
329+
$sth->bindParam(':video', $message->getVideo(), \PDO::PARAM_STR);
330+
$sth->bindParam(':voice', $message->getVoice(), \PDO::PARAM_STR);
331+
$sth->bindParam(':caption', $message->getCaption(), \PDO::PARAM_STR);
332+
$sth->bindParam(':contact', $message->getContact(), \PDO::PARAM_STR);
333+
$sth->bindParam(':location', $message->getLocation(), \PDO::PARAM_STR);
370334
$sth->bindParam(':new_chat_participant', $new_chat_paticipant, \PDO::PARAM_STR);
371335
$sth->bindParam(':left_chat_participant', $left_chat_paticipant, \PDO::PARAM_STR);
372-
373-
$sth->bindParam(':new_chat_title', $new_chat_title, \PDO::PARAM_STR);
336+
$sth->bindParam(':new_chat_title', $message->getNewChatTitle(), \PDO::PARAM_STR);
374337
//Array of Photosize
375-
$sth->bindParam(':new_chat_photo', $new_chat_photo, \PDO::PARAM_STR);
376338

377-
$sth->bindParam(':delete_chat_photo', $delete_chat_photo, \PDO::PARAM_STR);
378-
$sth->bindParam(':group_chat_created', $group_chat_created, \PDO::PARAM_STR);
339+
$var = [];
340+
if (is_array($new_chat_photo)) {
341+
foreach ($new_chat_photo as $elm) {
342+
$var[] = json_decode($elm, true);
343+
}
344+
345+
$new_chat_photo = json_encode($var);
346+
} else {
347+
$new_chat_photo = '';
348+
}
349+
350+
$sth->bindParam(':new_chat_photo', $new_chat_photo, \PDO::PARAM_STR);
351+
$sth->bindParam(':delete_chat_photo', $message->getDeleteChatPhoto(), \PDO::PARAM_STR);
352+
$sth->bindParam(':group_chat_created', $message->getGroupChatCreated(), \PDO::PARAM_STR);
379353

380354
$status = $sth->execute();
381355

src/Entities/Entity.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,9 @@ public function toJSON()
4141

4242
return $json;
4343
}
44+
45+
public function __toString()
46+
{
47+
return $this->toJSON();
48+
}
4449
}

0 commit comments

Comments
 (0)