Add initial rewrites of keyboard entities.

parent d30757a8
......@@ -12,26 +12,26 @@
namespace Longman\TelegramBot\Entities;
/**
* Class ForceReply
*
* @link https://core.telegram.org/bots/api#forcereply
*
* @method bool getForceReply() Shows reply interface to the user, as if they manually selected the bot‘s message and tapped 'Reply'
* @method bool getSelective() Optional. Use this parameter if you want to force reply from specific users only. Targets: 1) users that are @mentioned in the text of the Message object; 2) if the bot's message is a reply (has reply_to_message_id), sender of the original message.
*/
class ForceReply extends Entity
{
/**
* @var bool
*/
protected $force_reply;
/**
* @var bool|mixed
*/
protected $selective;
/**
* ForceReply constructor.
*
* @param array|null $data
* @param array $data
*
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public function __construct(array $data = null)
public function __construct(array $data = [])
{
$this->force_reply = true;
$this->selective = isset($data['selective']) ? $data['selective'] : false;
$data['force_reply'] = true;
parent::__construct($data);
}
}
......@@ -30,11 +30,13 @@ class InlineKeyboardButton extends Entity
protected function validate()
{
$num_params = 0;
foreach (['url', 'callback_data', 'switch_inline_query'] as $param) {
if (!empty($this->getProperty($param))) {
$num_params++;
}
}
if ($num_params !== 1) {
throw new TelegramException('You must use only one of these fields: url, callback_data, switch_inline_query!');
}
......
......@@ -16,35 +16,40 @@ use Longman\TelegramBot\Exception\TelegramException;
* Class InlineKeyboardMarkup
*
* @link https://core.telegram.org/bots/api#inlinekeyboardmarkup
*
* @method InlineKeyboardButton[][] getInlineKeyboard() Array of button rows, each represented by an Array of InlineKeyboardButton objects
*/
class InlineKeyboardMarkup extends Entity
{
/**
* @var array
* {@inheritdoc}
*/
protected $inline_keyboard;
protected function subEntities()
{
return [
'inline_keyboard' => InlineKeyboardButton::class,
];
}
/**
* InlineKeyboardMarkup constructor.
*
* @param array $data
* @throws \Longman\TelegramBot\Exception\TelegramException
* {@inheritdoc}
*/
public function __construct($data = array())
protected function validate()
{
if (isset($data['inline_keyboard'])) {
if (is_array($data['inline_keyboard'])) {
foreach ($data['inline_keyboard'] as $item) {
if (!is_array($item)) {
throw new TelegramException('Inline Keyboard subfield is not an array!');
}
}
$this->inline_keyboard = $data['inline_keyboard'];
} else {
throw new TelegramException('Inline Keyboard field is not an array!');
}
} else {
$inline_keyboard = $this->getProperty('inline_keyboard');
if ($inline_keyboard === null) {
throw new TelegramException('Inline Keyboard field is empty!');
}
if (!is_array($inline_keyboard)) {
throw new TelegramException('Inline Keyboard field is not an array!');
}
foreach ($inline_keyboard as $item) {
if (!is_array($item)) {
throw new TelegramException('Inline Keyboard subfield is not an array!');
}
}
}
}
......@@ -12,37 +12,24 @@ namespace Longman\TelegramBot\Entities;
use Longman\TelegramBot\Exception\TelegramException;
/**
* Class KeyboardButton
*
* @link https://core.telegram.org/bots/api#keyboardbutton
*
* @method string getText() Text of the button. If none of the optional fields are used, it will be sent to the bot as a message when the button is pressed
* @method bool getRequestContact() Optional. If True, the user's phone number will be sent as a contact when the button is pressed. Available in private chats only
* @method bool getRequestLocation() Optional. If True, the user's current location will be sent when the button is pressed. Available in private chats only
*/
class KeyboardButton extends Entity
{
/**
* @var mixed|null
*/
protected $text;
/**
* @var mixed|null
* {@inheritdoc}
*/
protected $request_contact;
/**
* @var mixed|null
*/
protected $request_location;
/**
* KeyboardButton constructor.
*
* @param array $data
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public function __construct(array $data = [])
protected function validate()
{
$this->text = isset($data['text']) ? $data['text'] : null;
if (empty($this->text)) {
throw new TelegramException('text is empty!');
if ($this->getRequestContact() && $this->getRequestLocation()) {
throw new TelegramException('You must use only one of these fields: request_contact, request_location!');
}
$this->request_contact = isset($data['request_contact']) ? $data['request_contact'] : null;
$this->request_location = isset($data['request_location']) ? $data['request_location'] : null;
}
}
......@@ -12,26 +12,26 @@
namespace Longman\TelegramBot\Entities;
/**
* Class ReplyKeyboardHide
*
* @link https://core.telegram.org/bots/api#replykeyboardhide
*
* @method bool getHideKeyboard() Requests clients to hide the custom keyboard
* @method bool getSelective() Optional. Use this parameter if you want to hide keyboard for specific users only. Targets: 1) users that are @mentioned in the text of the Message object; 2) if the bot's message is a reply (has reply_to_message_id), sender of the original message.
*/
class ReplyKeyboardHide extends Entity
{
/**
* @var bool
*/
protected $hide_keyboard;
/**
* @var bool
*/
protected $selective;
/**
* ReplyKeyboardHide constructor.
*
* @param array|null $data
* @param array $data
*
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public function __construct(array $data = null)
public function __construct(array $data = [])
{
$this->hide_keyboard = true;
$this->selective = isset($data['selective']) ? $data['selective'] : false;
$data['hide_keyboard'] = true;
parent::__construct($data);
}
}
......@@ -14,55 +14,48 @@ namespace Longman\TelegramBot\Entities;
use Longman\TelegramBot\Exception\TelegramException;
/**
* Class ReplyKeyboardMarkup
*
* @link https://core.telegram.org/bots/api#replykeyboardmarkup
*
* @method KeyboardButton[][] getKeyboard() Array of button rows, each represented by an Array of KeyboardButton objects
* @method bool getResizeKeyboard() Optional. Requests clients to resize the keyboard vertically for optimal fit (e.g., make the keyboard smaller if there are just two rows of buttons). Defaults to false, in which case the custom keyboard is always of the same height as the app's standard keyboard.
* @method bool getOneTimeKeyboard() Optional. Requests clients to hide the keyboard as soon as it's been used. The keyboard will still be available, but clients will automatically display the usual letter-keyboard in the chat – the user can press a special button in the input field to see the custom keyboard again. Defaults to false.
* @method bool getSelective() Optional. Use this parameter if you want to show the keyboard to specific users only. Targets: 1) users that are @mentioned in the text of the Message object; 2) if the bot's message is a reply (has reply_to_message_id), sender of the original message.
*/
class ReplyKeyboardMarkup extends Entity
{
/**
* @var array
* {@inheritdoc}
*/
protected $keyboard;
/**
* @var bool
*/
protected $resize_keyboard;
/**
* @var bool
*/
protected $one_time_keyboard;
/**
* @var bool
*/
protected $selective;
protected function subEntities()
{
return [
'keyboard' => KeyboardButton::class,
];
}
/**
* ReplyKeyboardMarkup constructor.
*
* @param array $data
*
* @throws \Longman\TelegramBot\Exception\TelegramException
* {@inheritdoc}
*/
public function __construct(array $data = [])
protected function validate()
{
if (!isset($data['keyboard'])) {
$keyboard = $this->getProperty('keyboard');
if ($keyboard === null) {
throw new TelegramException('Keyboard field is empty!');
}
if (!is_array($data['keyboard'])) {
if (!is_array($keyboard)) {
throw new TelegramException('Keyboard field is not an array!');
}
foreach ($data['keyboard'] as $item) {
foreach ($keyboard as $item) {
if (!is_array($item)) {
throw new TelegramException('Keyboard subfield is not an array!');
}
}
$this->keyboard = $data['keyboard'];
//Set the object members from the passed data params
foreach (['resize_keyboard', 'one_time_keyboard', 'selective'] as $param) {
$this->$param = isset($data[$param]) ? (bool)$data[$param] : false;
}
}
}
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