Remove entity reflect method and replace it by saving the raw data array which...

Remove entity reflect method and replace it by saving the raw data array which will be used for returning JSON string.
parent 879ceb99
......@@ -148,7 +148,7 @@ class SendtochannelCommand extends AdminCommand
break;
}
$notes['last_message_id'] = $message->getMessageId();
$notes['message'] = $message->reflect();
$notes['message'] = $message->getRawData();
$notes['message_type'] = $type;
// no break
case 2:
......@@ -350,7 +350,7 @@ class SendtochannelCommand extends AdminCommand
$channels = (array)$this->getConfig('your_channel');
$first_channel = $channels[0];
$data['text'] = $this->publish(
new Message($message->reflect(), $this->telegram->getBotName()),
new Message($message->getRawData(), $this->telegram->getBotName()),
$first_channel
);
}
......
......@@ -18,39 +18,29 @@ use ReflectionObject;
* Class Entity
*
* This is the base class for all entities.
*
* @link https://core.telegram.org/bots/api#available-types
*
* @method array getRawData() Get the raw data passed to this entity
*/
abstract class Entity
{
/**
* @var string
*/
protected $bot_name;
/**
* Get bot name
*
* @return string
*/
public function getBotName()
{
return $this->bot_name;
}
/**
* Entity constructor.
*
* @todo Get rid of the $bot_name, it shouldn't be here!
*
* @param $data
* @param array $data
* @param string $bot_name
*
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public function __construct($data, $bot_name = '')
{
$data['raw_data'] = $data;
$data['bot_name'] = $bot_name;
$this->assignMemberVariables($data);
$this->validate();
$this->bot_name = $bot_name;
}
/**
......@@ -60,84 +50,7 @@ abstract class Entity
*/
public function toJson()
{
return json_encode($this->reflect($this));
}
/**
* Reflect
*
* @param null $object
*
* @return array
*/
public function reflect($object = null)
{
if ($object === null) {
$object = $this;
}
$reflection = new ReflectionObject($object);
$properties = $reflection->getProperties();
$fields = [];
foreach ($properties as $property) {
$name = $property->getName();
if ($name === 'bot_name') {
continue;
}
if (!$property->isPrivate()) {
$array_of_obj = false;
$array_of_array_obj = false;
if (is_array($object->$name)) {
$array_of_obj = true;
$array_of_array_obj = true;
foreach ($object->$name as $elm) {
if (!is_object($elm)) {
//echo $name . " not array of object \n";
$array_of_obj = false;
//break;
}
if (is_array($elm)) {
foreach ($elm as $more_net) {
if (!is_object($more_net)) {
$array_of_array_obj = false;
}
}
}
}
}
if (is_object($object->$name)) {
$fields[$name] = $this->reflect($object->$name);
} elseif ($array_of_obj) {
foreach ($object->$name as $elm) {
$fields[$name][] = $this->reflect($elm);
}
} elseif ($array_of_array_obj) {
foreach ($object->$name as $elm) {
$temp = null;
if (!is_array($elm) && !is_object($elm)) {
continue;
}
foreach ($elm as $obj) {
$temp[] = $this->reflect($obj);
}
$fields[$name][] = $temp;
}
} else {
$property->setAccessible(true);
$value = $property->getValue($object);
if (null === $value) {
continue;
}
$fields[$name] = $value;
}
}
}
return $fields;
return json_encode($this->getRawData());
}
/**
......
......@@ -40,7 +40,7 @@ class ServerResponse extends Entity
*/
public function __construct(array $data, $bot_name)
{
$this->raw_json = isset($data['raw_json']) ? $data['raw_json'] : null;
$this->raw_data = $data;
if (isset($data['ok'], $data['result'])) {
if (is_array($data['result'])) {
if ($data['ok'] && !$this->isAssoc($data['result']) && !isset($data['result'][0]['user'])) {
......
......@@ -313,14 +313,12 @@ class Request
self::ensureNonEmptyData($data);
$raw_json = self::execute($action, $data);
$response = json_decode($raw_json, true);
$response = json_decode(self::execute($action, $data), true);
if (null === $response) {
throw new TelegramException('Telegram returned an invalid response! Please review your bot name and API key.');
}
$response['raw_json'] = $raw_json;
return new ServerResponse($response, $bot_name);
}
......
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