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() |
|
44 | { |
||
45 | 3 | $commands_temp = $this->_commands; |
|
46 | 3 | $this->_commands = []; |
|
47 | |||
48 | // Iterate over each |
||
49 | 3 | foreach ($commands_temp as $command) { |
|
50 | 1 | $this->_commands[$command::$type][] = $command; |
|
51 | } |
||
52 | |||
53 | 3 | foreach ($this->_commands as $index => $array) { |
|
54 | // Sort them by priority |
||
55 | 1 | uasort($this->_commands[$index], 'PhpBotFramework\Commands\CommandHandler::sortingPrior'); |
|
56 | } |
||
57 | 3 | } |
|
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 | 3 | protected function processCommands(array $update) : bool |
|
66 | { |
||
67 | // For each command active (checked by initCommands()) |
||
68 | 3 | foreach ($this->_commands as $entity => $commands) { |
|
69 | 1 | foreach ($commands as $index => $command) { |
|
70 | // If the update type is right and the update triggered a command |
||
71 | 1 | if (isset($update[$entity]) && $command->checkCommand($update[$entity])) { |
|
72 | 1 | $entity = new $command::$object_class($update[$entity]); |
|
73 | 1 | $this->_chat_id = $entity->getChatID(); |
|
|
|||
74 | 1 | $command->getScript()($this, $entity); |
|
75 | // Return the id as we already processed this update |
||
76 | 1 | return true; |
|
77 | } |
||
78 | } |
||
79 | } |
||
80 | |||
81 | // Return -1 because this update didn't trigger any command. |
||
82 | 2 | return false; |
|
83 | } |
||
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 | 1 | public function addCommand(BasicCommand $command) |
|
90 | { |
||
91 | 1 | $this->_commands[] = $command; |
|
92 | 1 | } |
|
93 | |||
94 | /** |
||
95 | * @internal |
||
96 | * \brief Sort an array based on <code>prior</code> index value. |
||
97 | * @param array $a First array. |
||
98 | * @param array $b Second array. |
||
99 | * @return int 1 If $a > $b, -1 if $a < $b, 0 otherwise. |
||
100 | */ |
||
101 | public static function sortingPrior($a, $b) |
||
117 | |||
118 | /** @} */ |
||
119 | } |
||
120 |
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: