1 | <?php |
||
5 | trait DatabaseHandler { |
||
6 | /** |
||
7 | * \brief Open a database connection using PDO. |
||
8 | * \details Provides a simpler way to initialize a database connection |
||
9 | * and create a PDO instance. |
||
10 | * @param $params Parameters for initialize connection. |
||
11 | * @return True on success. |
||
12 | */ |
||
13 | public function connect($params) { |
||
27 | |||
28 | protected function mergeWithDefaults($params) { |
||
32 | |||
33 | /** \brief Returns a string that can passed to PDO in order to open connection. */ |
||
34 | protected function stringify($params) : string { |
||
54 | |||
55 | /** |
||
56 | * \addtogroup Users-handle Users handling |
||
57 | * \brief Handle bot users on the database. |
||
58 | * @{ |
||
59 | */ |
||
60 | |||
61 | /** \brief Table contaning bot users data in the sql database. */ |
||
62 | public $user_table = '"User"'; |
||
63 | |||
64 | /** \brief Name of the column that represents the user id in the sql database */ |
||
65 | public $id_column = 'chat_id'; |
||
66 | |||
67 | /** \brief Add a user to the database. |
||
68 | * \details Add a user to the database in Bot::$user_table table and Bot::$id_column column using Bot::$pdo connection. |
||
69 | * @param $chat_id chat_id of the user to add. |
||
70 | * @return True on success. |
||
71 | */ |
||
72 | public function addUser($chat_id) : bool { |
||
110 | |||
111 | /** |
||
112 | * \brief Broadcast a message to all user registred on the database. |
||
113 | * \details Send a message to all users subscribed, change Bot::$user_table and Bot::$id_column to match your database structure is. |
||
114 | * This method requires Bot::$pdo connection set. |
||
115 | * All parameters are the same as CoreBot::sendMessage. |
||
116 | * Because a limitation of Telegram Bot API the bot will have a delay after 20 messages sent in different chats. |
||
117 | * @see CoreBot::sendMessage |
||
118 | */ |
||
119 | public function broadcastMessage($text, string $reply_markup = null, string $parse_mode = 'HTML', bool $disable_web_preview = true, bool $disable_notification = false) { |
||
164 | |||
165 | /** @} */ |
||
166 | |||
167 | } |
||
168 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: