3030
3131class Telegram
3232{
33+ /**
34+ * Auth name for user commands
35+ */
36+ const AUTH_USER = 'User ' ;
37+ /**
38+ * Auth name tof system commands
39+ */
40+ const AUTH_SYSTEM = 'System ' ;
41+ /**
42+ * Auth name for admin commands
43+ */
44+ const AUTH_ADMIN = 'Admin ' ;
45+
3346 /**
3447 * Version
3548 *
@@ -86,9 +99,9 @@ class Telegram
8699 * @var array
87100 */
88101 protected $ commandsClasses = [
89- ' User ' => [],
90- ' Admin ' => [],
91- ' System ' => [],
102+ self :: AUTH_USER => [],
103+ self :: AUTH_ADMIN => [],
104+ self :: AUTH_SYSTEM => [],
92105 ];
93106
94107 /**
@@ -340,9 +353,9 @@ public function getCommandObject(string $command, string $filepath = ''): ?Comma
340353 return $ this ->commands_objects [$ command ];
341354 }
342355
343- $ which = [' System ' ];
344- $ this ->isAdmin () && $ which [] = ' Admin ' ;
345- $ which [] = ' User ' ;
356+ $ which = [self :: AUTH_SYSTEM ];
357+ $ this ->isAdmin () && $ which [] = self :: AUTH_ADMIN ;
358+ $ which [] = self :: AUTH_USER ;
346359
347360 foreach ($ which as $ auth )
348361 {
@@ -360,19 +373,19 @@ public function getCommandObject(string $command, string $filepath = ''): ?Comma
360373 $ command_obj = new $ command_class ($ this , $ this ->update );
361374
362375 switch ($ auth ) {
363- case ' System ' :
376+ case self :: AUTH_SYSTEM :
364377 if ($ command_obj instanceof SystemCommand) {
365378 return $ command_obj ;
366379 }
367380 break ;
368381
369- case ' Admin ' :
382+ case self :: AUTH_ADMIN :
370383 if ($ command_obj instanceof AdminCommand) {
371384 return $ command_obj ;
372385 }
373386 break ;
374387
375- case ' User ' :
388+ case self :: AUTH_USER :
376389 if ($ command_obj instanceof UserCommand) {
377390 return $ command_obj ;
378391 }
@@ -766,36 +779,48 @@ public function isDbEnabled(): bool
766779 /**
767780 * Add a single custom commands class
768781 *
769- * @param string $command Set command name
770782 * @param string $className Set full classname
771- * @param string $auth Set Auth for command. Default is User
772- *
773783 * @return Telegram
774784 */
775- public function addCommandsClass (string $ command , string $ className, $ auth = ' User ' ): Telegram
785+ public function addCommandsClass (string $ className ): Telegram
776786 {
777- if (!class_exists ($ className ))
787+ if (!$ className || ! class_exists ($ className ))
778788 {
779789 $ error = 'Command class name: " ' . $ className . '" does not exist. ' ;
780790 TelegramLog::error ($ error );
781791 throw new InvalidArgumentException ($ error );
782792 }
783- if (empty ($ command ))
784- {
785- $ error = 'Command Name " ' . $ command . '" not set. ' ;
786- TelegramLog::error ($ error );
787- throw new InvalidArgumentException ($ error );
788- }
789793
790794 if (!is_array ($ this ->commandsClasses ))
791795 {
792796 $ this ->commandsClasses = [];
793797 }
794798
795- $ command = mb_strtolower ($ command );
796- $ auth = $ this ->ucFirstUnicode ($ auth );
799+ if (!is_a ($ className , Command::class, true )) {
800+ $ error = 'Command class is not a base command class ' ;
801+ TelegramLog::error ($ error );
802+ throw new InvalidArgumentException ($ error );
803+ }
797804
798- $ this ->commandsClasses [$ auth ][$ command ] = $ className ;
805+ $ commandObject = new $ className ($ this );
806+
807+ $ command = $ commandObject ->getName ();
808+ $ auth = null ;
809+ switch (true ) {
810+ case $ commandObject ->isSystemCommand ():
811+ $ auth = self ::AUTH_SYSTEM ;
812+ break ;
813+ case $ commandObject ->isAdminCommand ():
814+ $ auth = self ::AUTH_ADMIN ;
815+ break ;
816+ case $ commandObject ->isUserCommand ():
817+ $ auth = self ::AUTH_USER ;
818+ break ;
819+ }
820+
821+ if ($ auth ) {
822+ $ this ->commandsClasses [$ auth ][$ command ] = $ className ;
823+ }
799824
800825 return $ this ;
801826 }
0 commit comments