Example #1
0
 public function testGetProperties()
 {
     // Username.
     $user = new User(['id' => 1, 'username' => 'name_phpunit']);
     self::assertEquals('name_phpunit', $user->getUsername());
     // First name.
     $user = new User(['id' => 1, 'first_name' => 'name_phpunit']);
     self::assertEquals('name_phpunit', $user->getFirstName());
     // Last name.
     $user = new User(['id' => 1, 'last_name' => 'name_phpunit']);
     self::assertEquals('name_phpunit', $user->getLastName());
 }
 public function tryMention(User $user)
 {
     if (!is_null($user->getUsername())) {
         return '@' . $user->getUsername();
     } else {
         return $user->getFirstName();
     }
 }
Example #3
0
 /**
  * Insert users and save their connection to chats
  *
  * @param  Entities\User $user
  * @param  string $date
  * @param  Entities\Chat $chat
  *
  * @return bool If the insert was successful
  * @throws TelegramException
  */
 public static function insertUser(User $user, $date, Chat $chat = null)
 {
     if (!self::isDbConnected()) {
         return false;
     }
     $user_id = $user->getId();
     $username = $user->getUsername();
     $first_name = $user->getFirstName();
     $last_name = $user->getLastName();
     try {
         $sth1 = self::$pdo->prepare('INSERT INTO `' . TB_USER . '`
             (
             `id`, `username`, `first_name`, `last_name`, `created_at`, `updated_at`
             )
             VALUES (
             :id, :username, :first_name, :last_name, :date, :date
             )
             ON DUPLICATE KEY UPDATE `username`=:username, `first_name`=:first_name,
             `last_name`=:last_name, `updated_at`=:date
             ');
         $sth1->bindParam(':id', $user_id, \PDO::PARAM_INT);
         $sth1->bindParam(':username', $username, \PDO::PARAM_STR, 255);
         $sth1->bindParam(':first_name', $first_name, \PDO::PARAM_STR, 255);
         $sth1->bindParam(':last_name', $last_name, \PDO::PARAM_STR, 255);
         $sth1->bindParam(':date', $date, \PDO::PARAM_STR);
         $status = $sth1->execute();
     } catch (\PDOException $e) {
         throw new TelegramException($e->getMessage());
     }
     //insert also the relationship to the chat into user_chat table
     if (!is_null($chat)) {
         $chat_id = $chat->getId();
         try {
             $sth3 = self::$pdo->prepare('INSERT IGNORE INTO `' . TB_USER_CHAT . '`
                 (
                 `user_id`, `chat_id`
                 )
                 VALUES (
                 :user_id, :chat_id
                 )');
             $sth3->bindParam(':user_id', $user_id, \PDO::PARAM_INT);
             $sth3->bindParam(':chat_id', $chat_id, \PDO::PARAM_INT);
             $status = $sth3->execute();
         } catch (\PDOException $e) {
             throw new TelegramException($e->getMessage());
         }
     }
     return $status;
 }