Introduce helper commands for all Update Entity types straight from the command class.

This makes accessing the required Entity objects much easier.
parent 6d107528
...@@ -11,10 +11,27 @@ ...@@ -11,10 +11,27 @@
namespace Longman\TelegramBot\Commands; namespace Longman\TelegramBot\Commands;
use Longman\TelegramBot\DB; use Longman\TelegramBot\DB;
use Longman\TelegramBot\Entities\CallbackQuery;
use Longman\TelegramBot\Entities\ChosenInlineResult;
use Longman\TelegramBot\Entities\InlineQuery;
use Longman\TelegramBot\Entities\Message;
use Longman\TelegramBot\Entities\Update;
use Longman\TelegramBot\Request; use Longman\TelegramBot\Request;
use Longman\TelegramBot\Telegram; use Longman\TelegramBot\Telegram;
use Longman\TelegramBot\Entities\Update;
/**
* Class Command
*
* Base class for commands. It includes some helper methods that can fetch data directly from the Update object.
*
* @method Message getMessage() Optional. New incoming message of any kind — text, photo, sticker, etc.
* @method Message getEditedMessage() Optional. New version of a message that is known to the bot and was edited
* @method Message getChannelPost() Optional. New post in the channel, can be any kind — text, photo, sticker, etc.
* @method Message getEditedChannelPost() Optional. New version of a post in the channel that is known to the bot and was edited
* @method InlineQuery getInlineQuery() Optional. New incoming inline query
* @method ChosenInlineResult getChosenInlineResult() Optional. The result of an inline query that was chosen by a user and sent to their chat partner.
* @method CallbackQuery getCallbackQuery() Optional. New incoming callback query
*/
abstract class Command abstract class Command
{ {
/** /**
...@@ -31,13 +48,6 @@ abstract class Command ...@@ -31,13 +48,6 @@ abstract class Command
*/ */
protected $update; protected $update;
/**
* Message object
*
* @var \Longman\TelegramBot\Entities\Message
*/
protected $message;
/** /**
* Name * Name
* *
...@@ -117,8 +127,7 @@ abstract class Command ...@@ -117,8 +127,7 @@ abstract class Command
public function setUpdate(Update $update = null) public function setUpdate(Update $update = null)
{ {
if ($update !== null) { if ($update !== null) {
$this->update = $update; $this->update = $update;
$this->message = $this->update->getMessage();
} }
return $this; return $this;
...@@ -178,13 +187,21 @@ abstract class Command ...@@ -178,13 +187,21 @@ abstract class Command
} }
/** /**
* Get message object * Relay any non-existing function calls to Update object.
* *
* @return \Longman\TelegramBot\Entities\Message * This is purely a helper method to make requests from within execute() method easier.
*
* @param string $name
* @param array $arguments
*
* @return Command
*/ */
public function getMessage() public function __call($name, array $arguments)
{ {
return $this->message; if ($this->update === null) {
return null;
}
return call_user_func_array([$this->update, $name], $arguments);
} }
/** /**
......
...@@ -163,9 +163,7 @@ class CommandTest extends TestCase ...@@ -163,9 +163,7 @@ class CommandTest extends TestCase
$update = TestHelpers::getFakeUpdateObject(); $update = TestHelpers::getFakeUpdateObject();
$message = $update->getMessage(); $message = $update->getMessage();
$stub->setUpdate($update); $stub->setUpdate($update);
$this->assertAttributeEquals($update, 'update', $stub);
$this->assertEquals($update, $stub->getUpdate()); $this->assertEquals($update, $stub->getUpdate());
$this->assertAttributeEquals($message, 'message', $stub);
$this->assertEquals($message, $stub->getMessage()); $this->assertEquals($message, $stub->getMessage());
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment