Change interpretation of keyboard parameters, so that each parameter...

Change interpretation of keyboard parameters, so that each parameter represents a button on it's own row.
parent 8dba19fe
...@@ -51,7 +51,7 @@ class Keyboard extends Entity ...@@ -51,7 +51,7 @@ class Keyboard extends Entity
/** /**
* Get the proper keyboard button class for this keyboard. * Get the proper keyboard button class for this keyboard.
* *
* @return mixed * @return KeyboardButton|InlineKeyboardButton
*/ */
public function getKeyboardButtonClass() public function getKeyboardButtonClass()
{ {
...@@ -75,19 +75,20 @@ class Keyboard extends Entity ...@@ -75,19 +75,20 @@ class Keyboard extends Entity
*/ */
protected function createFromParams() protected function createFromParams()
{ {
/** @var KeyboardButton|InlineKeyboardButton $button_class */
$button_class = $this->getKeyboardButtonClass(); $button_class = $this->getKeyboardButtonClass();
$keyboard_type = $this->getKeyboardType(); $keyboard_type = $this->getKeyboardType();
// If the inline_keyboard isn't set directly, try to create one from the arguments.
$args = func_get_args(); $args = func_get_args();
$data = reset($args);
// Force button parameters into a single row. // Force button parameters into individual rows.
if (!is_array($data)) { foreach ($args as &$arg) {
$args = [$args]; !is_array($arg) && $arg = [$arg];
} }
if (!array_key_exists($keyboard_type, (array) $data)) { unset($arg);
$data = reset($args);
if (!array_key_exists($keyboard_type, $data)) {
$new_keyboard = []; $new_keyboard = [];
foreach ($args as $row) { foreach ($args as $row) {
if (is_array($row)) { if (is_array($row)) {
......
...@@ -33,6 +33,15 @@ class KeyboardTest extends TestCase ...@@ -33,6 +33,15 @@ class KeyboardTest extends TestCase
public function testKeyboardSingleButtonMultipleRows() public function testKeyboardSingleButtonMultipleRows()
{ {
$keyboard = (new Keyboard(
'Button Text 1',
'Button Text 2',
'Button Text 3'
))->getProperty('keyboard');
self::assertSame('Button Text 1', $keyboard[0][0]->getText());
self::assertSame('Button Text 2', $keyboard[1][0]->getText());
self::assertSame('Button Text 3', $keyboard[2][0]->getText());
$keyboard = (new Keyboard( $keyboard = (new Keyboard(
['Button Text 1'], ['Button Text 1'],
['Button Text 2'], ['Button Text 2'],
...@@ -45,39 +54,22 @@ class KeyboardTest extends TestCase ...@@ -45,39 +54,22 @@ class KeyboardTest extends TestCase
public function testKeyboardMultipleButtonsSingleRow() public function testKeyboardMultipleButtonsSingleRow()
{ {
/** @var KeyboardButton $keyboard_button_1 */
/** @var KeyboardButton $keyboard_button_2 */
$keyboard = (new Keyboard('Button Text 1', 'Button Text 2'))->getProperty('keyboard');
list($keyboard_button_1, $keyboard_button_2) = $keyboard[0]; // Row 1.
self::assertSame('Button Text 1', $keyboard_button_1->getText());
self::assertSame('Button Text 2', $keyboard_button_2->getText());
$keyboard = (new Keyboard(['Button Text 3', 'Button Text 4']))->getProperty('keyboard'); $keyboard = (new Keyboard(['Button Text 3', 'Button Text 4']))->getProperty('keyboard');
list($keyboard_button_1, $keyboard_button_2) = $keyboard[0]; // Row 1. self::assertSame('Button Text 3', $keyboard[0][0]->getText());
self::assertSame('Button Text 3', $keyboard_button_1->getText()); self::assertSame('Button Text 4', $keyboard[0][1]->getText());
self::assertSame('Button Text 4', $keyboard_button_2->getText());
} }
public function testKeyboardMultipleButtonsMultipleRows() public function testKeyboardMultipleButtonsMultipleRows()
{ {
/** @var KeyboardButton $keyboard_button_1 */
/** @var KeyboardButton $keyboard_button_2 */
/** @var KeyboardButton $keyboard_button_3 */
/** @var KeyboardButton $keyboard_button_4 */
$keyboard = (new Keyboard( $keyboard = (new Keyboard(
['Button Text 1', 'Button Text 2'], ['Button Text 1', 'Button Text 2'],
['Button Text 3', 'Button Text 4'] ['Button Text 3', 'Button Text 4']
))->getProperty('keyboard'); ))->getProperty('keyboard');
list($keyboard_button_1, $keyboard_button_2) = $keyboard[0]; // Row 1. self::assertSame('Button Text 1', $keyboard[0][0]->getText());
list($keyboard_button_3, $keyboard_button_4) = $keyboard[1]; // Row 2. self::assertSame('Button Text 2', $keyboard[0][1]->getText());
self::assertSame('Button Text 3', $keyboard[1][0]->getText());
self::assertSame('Button Text 1', $keyboard_button_1->getText()); self::assertSame('Button Text 4', $keyboard[1][1]->getText());
self::assertSame('Button Text 2', $keyboard_button_2->getText());
self::assertSame('Button Text 3', $keyboard_button_3->getText());
self::assertSame('Button Text 4', $keyboard_button_4->getText());
} }
public function testKeyboardWithButtonObjects() public function testKeyboardWithButtonObjects()
...@@ -92,7 +84,7 @@ class KeyboardTest extends TestCase ...@@ -92,7 +84,7 @@ class KeyboardTest extends TestCase
new KeyboardButton('Button Text 3') new KeyboardButton('Button Text 3')
))->getProperty('keyboard'); ))->getProperty('keyboard');
self::assertSame('Button Text 2', $keyboard[0][0]->getText()); self::assertSame('Button Text 2', $keyboard[0][0]->getText());
self::assertSame('Button Text 3', $keyboard[0][1]->getText()); self::assertSame('Button Text 3', $keyboard[1][0]->getText());
$keyboard = (new Keyboard( $keyboard = (new Keyboard(
[new KeyboardButton('Button Text 4')], [new KeyboardButton('Button Text 4')],
...@@ -102,30 +94,4 @@ class KeyboardTest extends TestCase ...@@ -102,30 +94,4 @@ class KeyboardTest extends TestCase
self::assertSame('Button Text 5', $keyboard[1][0]->getText()); self::assertSame('Button Text 5', $keyboard[1][0]->getText());
self::assertSame('Button Text 6', $keyboard[1][1]->getText()); self::assertSame('Button Text 6', $keyboard[1][1]->getText());
} }
/*public function testKeyboardWithData()
{
$keyboard = (new Keyboard(
['Button Text 1', 'Button Text 2'],
['Button Text 3', 'Button Text 4']
))->getProperty('keyboard');
}*/
/**
* @expectedException \Longman\TelegramBot\Exception\TelegramException
* @expectedExceptionMessage You must use only one of these fields: request_contact, request_location!
*/
/*public function testKeyboardTooManyParametersFail()
{
new Keyboard(['text' => 'message', 'request_contact' => true, 'request_location' => true]);
}*/
/*public function testKeyboardSuccess()
{
new Keyboard(['text' => 'message']);
new Keyboard(['text' => 'message', 'request_contact' => true]);
new Keyboard(['text' => 'message', 'request_location' => true]);
}*/
} }
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