Use new Entity schema for ServerResponse and save raw_data in Entities.

parent 6009277b
...@@ -37,7 +37,15 @@ abstract class Entity ...@@ -37,7 +37,15 @@ abstract class Entity
*/ */
public function __construct($data, $bot_name = '') public function __construct($data, $bot_name = '')
{ {
$data['raw_data'] = $data; //Make sure we're not raw_data inception-ing
if (array_key_exists('raw_data', $data)) {
if ($data['raw_data'] === null) {
unset($data['raw_data']);
}
} else {
$data['raw_data'] = $data;
}
$data['bot_name'] = $bot_name; $data['bot_name'] = $bot_name;
$this->assignMemberVariables($data); $this->assignMemberVariables($data);
$this->validate(); $this->validate();
......
...@@ -8,117 +8,74 @@ ...@@ -8,117 +8,74 @@
namespace Longman\TelegramBot\Entities; namespace Longman\TelegramBot\Entities;
/**
* Class ServerResponse
*
* @method bool getOk() If the request was successful
* @method mixed getResult() The result of the query
* @method int getErrorCode() Error code of the unsuccessful request
* @method string getDescription() Human-readable description of the result / unsuccessful request
*
* @method_todo ResponseParameters getParameters() Field which can help to automatically handle the error
*/
class ServerResponse extends Entity class ServerResponse extends Entity
{ {
/**
* @var bool
*/
protected $ok;
/**
* @var null
*/
protected $result;
/**
* @var null
*/
protected $error_code;
/**
* @var null
*/
protected $description;
/** /**
* ServerResponse constructor. * ServerResponse constructor.
* *
* @param array $data * @param array $data
* @param $bot_name * @param string $bot_name
* *
* @throws \Longman\TelegramBot\Exception\TelegramException * @throws \Longman\TelegramBot\Exception\TelegramException
*/ */
public function __construct(array $data, $bot_name) public function __construct(array $data, $bot_name)
{ {
$this->raw_data = $data; // Make sure we don't double-save the raw_data.
if (isset($data['ok'], $data['result'])) { unset($data['raw_data']);
if (is_array($data['result'])) { $data['raw_data'] = $data;
if ($data['ok'] && !$this->isAssoc($data['result']) && !isset($data['result'][0]['user'])) {
//Get Update $is_ok = isset($data['ok']) ? (bool)$data['ok'] : false;
foreach ($data['result'] as $update) { $result = isset($data['result']) ? $data['result'] : null;
$this->result[] = new Update($update, $bot_name);
} if ($is_ok && $result !== null) {
} elseif ($data['ok'] && !$this->isAssoc($data['result']) && isset($data['result'][0]['user'])) { $data['ok'] = true;
//Response from getChatAdministrators
$this->result = []; if (is_array($result)) {
foreach ($data['result'] as $user) { if ($this->isAssoc($result)) {
$this->result[] = new ChatMember($user); $data['result'] = $this->createResultObject($result, $bot_name);
} } else {
} elseif ($data['ok'] && $this->isAssoc($data['result'])) { $data['result'] = $this->createResultObjects($result, $bot_name);
if (isset($data['result']['total_count'])) {
//Response from getUserProfilePhotos
$this->result = new UserProfilePhotos($data['result']);
} elseif (isset($data['result']['file_id'])) {
//Response from getFile
$this->result = new File($data['result']);
} elseif (isset($data['result']['username'])) {
//Response from getMe
$this->result = new User($data['result']);
} elseif (isset($data['result']['id'])) {
//Response from getChat
$this->result = new Chat($data['result']);
} elseif (isset($data['result']['user'])) {
//Response from getChatMember
$this->result = new ChatMember($data['result']);
} elseif (isset($data['result']['has_custom_certificate'])) {
//Response from getWebhookInfo
$this->result = new WebhookInfo($data['result']);
} else {
//Response from sendMessage
$this->result = new Message($data['result'], $bot_name);
}
} }
$this->ok = $data['ok']; unset($data['error_code'], $data['description']);
$this->error_code = null;
$this->description = null;
} else { } else {
if ($data['ok'] && $data['result'] === true) { $data['result'] = $result;
if ($result === true) {
//Response from setWebhook set //Response from setWebhook set
$this->ok = $data['ok']; unset($data['error_code']);
$this->result = true; } elseif (!is_numeric($result)) { //Not response from getChatMembersCount
$this->error_code = null; $data['ok'] = false;
$this->description = isset($data['description']) ? $data['description'] : ''; unset($data['result']);
} elseif (is_numeric($data['result'])) {
//Response from getChatMembersCount
$this->result = $data['result'];
} else {
$this->ok = false;
$this->result = null;
$this->error_code = $data['error_code'];
$this->description = $data['description'];
} }
} }
} else { } else {
//webHook not set //webHook not set
$this->ok = false; $data['ok'] = false;
$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!');
} }
parent::__construct($data, $bot_name);
} }
/** /**
* Check if array is associative * Check if array is associative
* *
* @param array $array * @param array $array
*
* @return bool * @return bool
*/ */
protected function isAssoc(array $array) protected function isAssoc(array $array)
{ {
return (bool) count(array_filter(array_keys($array), 'is_string')); return (bool)count(array_filter(array_keys($array), 'is_string'));
} }
/** /**
...@@ -128,46 +85,83 @@ class ServerResponse extends Entity ...@@ -128,46 +85,83 @@ class ServerResponse extends Entity
*/ */
public function isOk() public function isOk()
{ {
return $this->ok; return (bool)$this->getOk();
} }
/** /**
* Get result * Print error
* *
* @return string * @return string
*/ */
public function getResult() public function printError()
{ {
return $this->result; return 'Error N: ' . $this->getErrorCode() . ' Description: ' . $this->getDescription();
} }
/** /**
* Get error code * Create and return the object of the received result
* *
* @return string * @param array $result
*/ * @param string $bot_name
public function getErrorCode()
{
return $this->error_code;
}
/**
* Get description
* *
* @return null * @return \Longman\TelegramBot\Entities\Chat|\Longman\TelegramBot\Entities\ChatMember|\Longman\TelegramBot\Entities\File|\Longman\TelegramBot\Entities\Message|\Longman\TelegramBot\Entities\User|\Longman\TelegramBot\Entities\UserProfilePhotos|\Longman\TelegramBot\Entities\WebhookInfo
* @throws \Longman\TelegramBot\Exception\TelegramException
*/ */
public function getDescription() private function createResultObject($result, $bot_name)
{ {
return $this->description; // We don't need to save the raw_data of the response object!
$result['raw_data'] = null;
$result_object_types = [
'total_count' => 'UserProfilePhotos', //Response from getUserProfilePhotos
'file_id' => 'File', //Response from getFile
'username' => 'User', //Response from getMe
'id' => 'Chat', //Response from getChat
'user' => 'ChatMember', //Response from getChatMember
'url' => 'WebhookInfo', //Response from getWebhookInfo
];
foreach ($result_object_types as $type => $object_class) {
if (isset($result[$type])) {
$object_class = __NAMESPACE__ . '\\' . $object_class;
return new $object_class($result);
}
}
//Response from sendMessage
return new Message($result, $bot_name);
} }
/** /**
* Print error * Create and return the objects array of the received result
* *
* @return string * @param array $result
* @param string $bot_name
*
* @return null|\Longman\TelegramBot\Entities\ChatMember[]|\Longman\TelegramBot\Entities\Update[]
* @throws \Longman\TelegramBot\Exception\TelegramException
*/ */
public function printError() private function createResultObjects($result, $bot_name)
{ {
return 'Error N: ' . $this->getErrorCode() . ' Description: ' . $this->getDescription(); $results = [];
if (isset($result[0]['user'])) {
//Response from getChatAdministrators
foreach ($result as $user) {
// We don't need to save the raw_data of the response object!
$user['raw_data'] = null;
$results[] = new ChatMember($user);
}
} else {
//Get Update
foreach ($result as $update) {
// We don't need to save the raw_data of the response object!
$update['raw_data'] = null;
$results[] = new Update($update, $bot_name);
}
}
return $results;
} }
} }
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