1 | <?php |
||
26 | trait CommandHandler |
||
27 | { |
||
28 | /** |
||
29 | * \addtogroup Commands |
||
30 | * \brief Bots command and usage. |
||
31 | * @{ |
||
32 | */ |
||
33 | |||
34 | /** @internal |
||
35 | * \brief Contains all command used by the bot. */ |
||
36 | private $_commands = []; |
||
37 | |||
38 | /** |
||
39 | * @internal |
||
40 | * \brief Initialize commands to speed up processing. |
||
41 | * \details Get all command that the bot handle, and put them in priority. |
||
42 | */ |
||
43 | 3 | protected function initCommands() |
|
58 | |||
59 | /** |
||
60 | * @internal |
||
61 | * \brief Process updates prioritizing bot's commands over the general methods (e.g. BaseBot::processMessage()) |
||
62 | * @param array $update Update to process. |
||
63 | * @return bool True if this update trigger any command. |
||
64 | */ |
||
65 | 5 | protected function processCommands(array $update) : bool |
|
84 | |||
85 | /** |
||
86 | * \brief Add a command to the bot. |
||
87 | * @param BasicCommand $command Command to add. Must be an object that inherits BasicCommand class. |
||
88 | */ |
||
89 | 2 | public function addCommand(BasicCommand $command) |
|
93 | |||
94 | /** |
||
95 | * \brief Add various commands at once. |
||
96 | * @param ...BasicCommand $commands The commands to add. |
||
97 | */ |
||
98 | 1 | public function addCommands(BasicCommand ...$commands) |
|
104 | |||
105 | /** |
||
106 | * \brief Add a message command to the bot. |
||
107 | * @param string $command The command that will trigger the function (e.g. start). |
||
108 | * @param callable $script The function that will be triggered by a command. |
||
109 | */ |
||
110 | 1 | public function addMessageCommand(string $command, callable $script) |
|
114 | |||
115 | /** |
||
116 | * \brief Add a callback command to the bot. |
||
117 | * @param string $data The data that will trigger the function. |
||
118 | * @param callable $script The function that will be triggered by the data. |
||
119 | */ |
||
120 | public function addCallbackCommand(string $data, callable $script) |
||
124 | |||
125 | /** |
||
126 | * @internal |
||
127 | * \brief Sort an array based on <code>prior</code> index value. |
||
128 | * @param array $a First array. |
||
129 | * @param array $b Second array. |
||
130 | * @return int 1 If $a > $b, -1 if $a < $b, 0 otherwise. |
||
131 | */ |
||
132 | public static function sortingPrior($a, $b) |
||
148 | |||
149 | /** @} */ |
||
150 | } |
||
151 |
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: