From c645d6a727d990089de9d4ae7e360fe4f22393ee Mon Sep 17 00:00:00 2001 From: netkas Date: Sun, 6 Oct 2024 18:59:59 -0400 Subject: [PATCH] Refactor sendRequest method to use method enums --- src/TgBotLib/Bot.php | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/TgBotLib/Bot.php b/src/TgBotLib/Bot.php index 02adbec..b38ac9b 100644 --- a/src/TgBotLib/Bot.php +++ b/src/TgBotLib/Bot.php @@ -9,6 +9,11 @@ namespace TgBotLib; + use InvalidArgumentException; + use TgBotLib\Enums\Methods; + use TgBotLib\Exceptions\TelegramException; + use TgBotLib\Interfaces\ObjectTypeInterface; + class Bot { private string $token; @@ -74,8 +79,22 @@ return $this->endpoint; } - public function sendRequest(string $method, array $parameters=[]): array + /** + * Sends a request by executing the specified method with the given parameters. + * + * @param string $method The name of the method to execute. + * @param array $parameters Optional parameters to pass to the method. + * @return mixed The result of the executed method. + * @throws TelegramException if the method execution fails. + */ + public function sendRequest(string $method, array $parameters=[]): mixed { + $method = Methods::tryFrom($method); + if($method === null) + { + throw new InvalidArgumentException('Invalid method name'); + } + return $method->execute($this, $parameters); } } \ No newline at end of file