A few code optimisations.

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