Simplify Keyboard entities and tests to remove duplicate code.

Small optimisation for Chat entity.
parent 1cfa4779
......@@ -36,7 +36,7 @@ class Chat extends Entity
$id = $this->getId();
$type = $this->getType();
if (!$type && $id !== 0) {
if (!$type) {
$id > 0 && $this->type = 'private';
$id < 0 && $this->type = 'group';
}
......
......@@ -10,8 +10,6 @@
namespace Longman\TelegramBot\Entities;
use Longman\TelegramBot\Exception\TelegramException;
/**
* Class InlineKeyboard
*
......@@ -19,32 +17,4 @@ use Longman\TelegramBot\Exception\TelegramException;
*/
class InlineKeyboard extends Keyboard
{
/**
* {@inheritdoc}
*/
public function __construct($data = [])
{
$data = call_user_func_array([$this, 'createFromParams'], func_get_args());
parent::__construct($data);
}
/**
* {@inheritdoc}
*/
protected function validate()
{
$inline_keyboard = $this->getProperty('inline_keyboard');
if ($inline_keyboard !== null) {
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!');
}
}
}
}
}
......@@ -178,16 +178,17 @@ class Keyboard extends Entity
*/
protected function validate()
{
$keyboard = $this->getProperty('keyboard');
$keyboard_type = $this->getKeyboardType();
$keyboard = $this->getProperty($keyboard_type);
if ($keyboard !== null) {
if (!is_array($keyboard)) {
throw new TelegramException('Keyboard field is not an array!');
throw new TelegramException($keyboard_type . ' field is not an array!');
}
foreach ($keyboard as $item) {
if (!is_array($item)) {
throw new TelegramException('Keyboard subfield is not an array!');
throw new TelegramException($keyboard_type . ' subfield is not an array!');
}
}
}
......
......@@ -36,7 +36,7 @@ class InlineKeyboardTest extends TestCase
/**
* @expectedException \Longman\TelegramBot\Exception\TelegramException
* @expectedExceptionMessage Inline Keyboard field is not an array!
* @expectedExceptionMessage inline_keyboard field is not an array!
*/
public function testInlineKeyboardDataMalformedField()
{
......@@ -45,7 +45,7 @@ class InlineKeyboardTest extends TestCase
/**
* @expectedException \Longman\TelegramBot\Exception\TelegramException
* @expectedExceptionMessage Inline Keyboard subfield is not an array!
* @expectedExceptionMessage inline_keyboard subfield is not an array!
*/
public function testInlineKeyboardDataMalformedSubfield()
{
......
......@@ -25,7 +25,7 @@ class KeyboardTest extends TestCase
{
/**
* @expectedException \Longman\TelegramBot\Exception\TelegramException
* @expectedExceptionMessage Keyboard field is not an array!
* @expectedExceptionMessage keyboard field is not an array!
*/
public function testKeyboardDataMalformedField()
{
......@@ -34,7 +34,7 @@ class KeyboardTest extends TestCase
/**
* @expectedException \Longman\TelegramBot\Exception\TelegramException
* @expectedExceptionMessage Keyboard subfield is not an array!
* @expectedExceptionMessage keyboard subfield is not an array!
*/
public function testKeyboardDataMalformedSubfield()
{
......
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