1 | <?php |
||
19 | class Conversation |
||
20 | { |
||
21 | /** |
||
22 | * All information fetched from the database |
||
23 | * |
||
24 | * @var array|null |
||
25 | */ |
||
26 | protected $conversation; |
||
27 | |||
28 | /** |
||
29 | * Notes stored inside the conversation |
||
30 | * |
||
31 | * @var mixed |
||
32 | */ |
||
33 | protected $protected_notes; |
||
34 | |||
35 | /** |
||
36 | * Notes to be stored |
||
37 | * |
||
38 | * @var mixed |
||
39 | */ |
||
40 | public $notes; |
||
41 | |||
42 | /** |
||
43 | * Telegram user id |
||
44 | * |
||
45 | * @var int |
||
46 | */ |
||
47 | protected $user_id; |
||
48 | |||
49 | /** |
||
50 | * Telegram chat id |
||
51 | * |
||
52 | * @var int |
||
53 | */ |
||
54 | protected $chat_id; |
||
55 | |||
56 | /** |
||
57 | * Command to be executed if the conversation is active |
||
58 | * |
||
59 | * @var string |
||
60 | */ |
||
61 | protected $command; |
||
62 | |||
63 | /** |
||
64 | * Conversation contructor to initialize a new conversation |
||
65 | * |
||
66 | * @param int $user_id |
||
67 | * @param int $chat_id |
||
68 | * @param string $command |
||
69 | * |
||
70 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
71 | */ |
||
72 | 3 | public function __construct($user_id, $chat_id, $command = null) |
|
84 | |||
85 | /** |
||
86 | * Clear all conversation variables. |
||
87 | * |
||
88 | * @return bool Always return true, to allow this method in an if statement. |
||
89 | */ |
||
90 | protected function clear() |
||
91 | { |
||
92 | $this->conversation = null; |
||
93 | $this->protected_notes = null; |
||
94 | $this->notes = null; |
||
95 | |||
96 | return true; |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * Load the conversation from the database |
||
101 | * |
||
102 | * @return bool |
||
103 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
104 | */ |
||
105 | 3 | protected function load() |
|
106 | { |
||
107 | //Select an active conversation |
||
108 | 3 | $conversation = ConversationDB::selectConversation($this->user_id, $this->chat_id, 1); |
|
|
|||
109 | 3 | if (isset($conversation[0])) { |
|
110 | //Pick only the first element |
||
111 | $this->conversation = $conversation[0]; |
||
112 | |||
113 | //Load the command from the conversation if it hasn't been passed |
||
114 | $this->command = $this->command ?: $this->conversation['command']; |
||
115 | |||
116 | if ($this->command !== $this->conversation['command']) { |
||
117 | $this->cancel(); |
||
118 | return false; |
||
119 | } |
||
120 | |||
121 | //Load the conversation notes |
||
122 | $this->protected_notes = json_decode($this->conversation['notes'], true); |
||
123 | $this->notes = $this->protected_notes; |
||
124 | } |
||
125 | |||
126 | 3 | return $this->exists(); |
|
127 | } |
||
128 | |||
129 | /** |
||
130 | * Check if the conversation already exists |
||
131 | * |
||
132 | * @return bool |
||
133 | */ |
||
134 | 3 | public function exists() |
|
138 | |||
139 | /** |
||
140 | * Start a new conversation if the current command doesn't have one yet |
||
141 | * |
||
142 | * @return bool |
||
143 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
144 | */ |
||
145 | 1 | protected function start() |
|
146 | { |
||
147 | 1 | if ($this->command |
|
148 | 1 | && !$this->exists() |
|
149 | 1 | && ConversationDB::insertConversation( |
|
150 | 1 | $this->user_id, |
|
151 | 1 | $this->chat_id, |
|
152 | 1 | $this->command |
|
153 | ) |
||
154 | ) { |
||
155 | return $this->load(); |
||
156 | } |
||
157 | |||
158 | return false; |
||
159 | } |
||
160 | |||
161 | /** |
||
162 | * Delete the current conversation |
||
163 | * |
||
164 | * Currently the Conversation is not deleted but just set to 'stopped' |
||
165 | * |
||
166 | * @return bool |
||
167 | */ |
||
168 | public function stop() |
||
169 | { |
||
170 | return ($this->updateStatus('stopped') && $this->clear()); |
||
171 | } |
||
172 | |||
173 | /** |
||
174 | * Cancel the current conversation |
||
175 | * |
||
176 | * @return bool |
||
177 | */ |
||
178 | public function cancel() |
||
179 | { |
||
180 | return ($this->updateStatus('cancelled') && $this->clear()); |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * Update the status of the current conversation |
||
185 | * |
||
186 | * @param string $status |
||
187 | * |
||
188 | * @return bool |
||
189 | */ |
||
190 | protected function updateStatus($status) |
||
191 | { |
||
192 | if ($this->exists()) { |
||
193 | $fields = ['status' => $status]; |
||
194 | $where = [ |
||
195 | 'id' => $this->conversation['id'], |
||
196 | 'status' => 'active', |
||
197 | 'user_id' => $this->user_id, |
||
198 | 'chat_id' => $this->chat_id, |
||
199 | ]; |
||
200 | if (ConversationDB::updateConversation($fields, $where)) { |
||
201 | return true; |
||
202 | } |
||
203 | } |
||
204 | |||
205 | return false; |
||
206 | } |
||
207 | |||
208 | /** |
||
209 | * Store the array/variable in the database with json_encode() function |
||
210 | * |
||
211 | * @return bool |
||
212 | */ |
||
213 | public function update() |
||
214 | { |
||
215 | if ($this->exists()) { |
||
216 | $fields = ['notes' => json_encode($this->notes)]; |
||
217 | //I can update a conversation whatever the state is |
||
218 | $where = ['id' => $this->conversation['id']]; |
||
219 | if (ConversationDB::updateConversation($fields, $where)) { |
||
220 | return true; |
||
221 | } |
||
222 | } |
||
223 | |||
224 | return false; |
||
225 | } |
||
226 | |||
227 | /** |
||
228 | * Retrieve the command to execute from the conversation |
||
229 | * |
||
230 | * @return string|null |
||
231 | */ |
||
232 | 1 | public function getCommand() |
|
236 | } |
||
237 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: