Refactor sendRequest method to use method enums

This commit is contained in:
netkas 2024-10-06 18:59:59 -04:00
parent 5703ce9987
commit c645d6a727

View file

@ -9,6 +9,11 @@
namespace TgBotLib; namespace TgBotLib;
use InvalidArgumentException;
use TgBotLib\Enums\Methods;
use TgBotLib\Exceptions\TelegramException;
use TgBotLib\Interfaces\ObjectTypeInterface;
class Bot class Bot
{ {
private string $token; private string $token;
@ -74,8 +79,22 @@
return $this->endpoint; 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);
} }
} }