A few code optimisations.

parent 98722812
......@@ -35,7 +35,7 @@ class KeyboardButton extends Entity
* @param array $data
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public function __construct($data = array())
public function __construct(array $data = [])
{
$this->text = isset($data['text']) ? $data['text'] : null;
if (empty($this->text)) {
......
......@@ -40,27 +40,29 @@ class ReplyKeyboardMarkup extends Entity
* ReplyKeyboardMarkup constructor.
*
* @param array $data
*
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public function __construct($data = array())
public function __construct(array $data = [])
{
if (isset($data['keyboard'])) {
if (is_array($data['keyboard'])) {
if (!isset($data['keyboard'])) {
throw new TelegramException('Keyboard field is empty!');
}
if (!is_array($data['keyboard'])) {
throw new TelegramException('Keyboard field is not an array!');
}
foreach ($data['keyboard'] as $item) {
if (!is_array($item)) {
throw new TelegramException('Keyboard subfield is not an array!');
}
}
$this->keyboard = $data['keyboard'];
} else {
throw new TelegramException('Keyboard field is not an array!');
}
} else {
throw new TelegramException('Keyboard field is empty!');
}
$this->resize_keyboard = isset($data['resize_keyboard']) ? $data['resize_keyboard'] : false;
$this->one_time_keyboard = isset($data['one_time_keyboard']) ? $data['one_time_keyboard'] : false;
$this->selective = isset($data['selective']) ? $data['selective'] : false;
//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;
}
}
}
......@@ -35,10 +35,12 @@ class ServerResponse extends Entity
*
* @param array $data
* @param $bot_name
*
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public function __construct(array $data, $bot_name)
{
if (isset($data['ok']) && isset($data['result'])) {
if (isset($data['ok'], $data['result'])) {
if (is_array($data['result'])) {
if ($data['ok'] && !$this->isAssoc($data['result']) && !isset($data['result'][0]['user'])) {
//Get Update
......@@ -49,7 +51,7 @@ class ServerResponse extends Entity
//Response from getChatAdministrators
$this->result = [];
foreach ($data['result'] as $user) {
array_push($this->result, new ChatMember($user));
$this->result[] = new ChatMember($user);
}
} elseif ($data['ok'] && $this->isAssoc($data['result'])) {
if (isset($data['result']['total_count'])) {
......@@ -82,12 +84,7 @@ class ServerResponse extends Entity
$this->ok = $data['ok'];
$this->result = true;
$this->error_code = null;
if (isset($data['description'])) {
$this->description = $data['description'];
} else {
$this->description = '';
}
$this->description = isset($data['description']) ? $data['description'] : '';
} elseif (is_numeric($data['result'])) {
//Response from getChatMembersCount
$this->result = $data['result'];
......@@ -101,24 +98,9 @@ class ServerResponse extends Entity
} else {
//webHook not set
$this->ok = false;
if (isset($data['result'])) {
$this->result = $data['result'];
} else {
$this->result = null;
}
if (isset($data['error_code'])) {
$this->error_code = $data['error_code'];
} else {
$this->error_code = null;
}
if (isset($data['description'])) {
$this->description = $data['description'];
} else {
$this->description = null;
}
$this->result = isset($data['result']) ? $data['result'] : null;
$this->error_code = isset($data['error_code']) ? $data['error_code'] : null;
$this->description = isset($data['description']) ? $data['description'] : null;
//throw new TelegramException('ok(variable) is not set!');
}
......
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