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 ...@@ -148,7 +148,7 @@ class SendtochannelCommand extends AdminCommand
break; break;
} }
$notes['last_message_id'] = $message->getMessageId(); $notes['last_message_id'] = $message->getMessageId();
$notes['message'] = $message->reflect(); $notes['message'] = $message->getRawData();
$notes['message_type'] = $type; $notes['message_type'] = $type;
// no break // no break
case 2: case 2:
...@@ -350,7 +350,7 @@ class SendtochannelCommand extends AdminCommand ...@@ -350,7 +350,7 @@ class SendtochannelCommand extends AdminCommand
$channels = (array)$this->getConfig('your_channel'); $channels = (array)$this->getConfig('your_channel');
$first_channel = $channels[0]; $first_channel = $channels[0];
$data['text'] = $this->publish( $data['text'] = $this->publish(
new Message($message->reflect(), $this->telegram->getBotName()), new Message($message->getRawData(), $this->telegram->getBotName()),
$first_channel $first_channel
); );
} }
......
...@@ -18,39 +18,29 @@ use ReflectionObject; ...@@ -18,39 +18,29 @@ use ReflectionObject;
* Class Entity * Class Entity
* *
* This is the base class for all entities. * 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 abstract class Entity
{ {
/**
* @var string
*/
protected $bot_name;
/**
* Get bot name
*
* @return string
*/
public function getBotName()
{
return $this->bot_name;
}
/** /**
* Entity constructor. * Entity constructor.
* *
* @todo Get rid of the $bot_name, it shouldn't be here! * @todo Get rid of the $bot_name, it shouldn't be here!
* *
* @param $data * @param array $data
* @param string $bot_name * @param string $bot_name
* *
* @throws \Longman\TelegramBot\Exception\TelegramException * @throws \Longman\TelegramBot\Exception\TelegramException
*/ */
public function __construct($data, $bot_name = '') public function __construct($data, $bot_name = '')
{ {
$data['raw_data'] = $data;
$data['bot_name'] = $bot_name;
$this->assignMemberVariables($data); $this->assignMemberVariables($data);
$this->validate(); $this->validate();
$this->bot_name = $bot_name;
} }
/** /**
...@@ -60,84 +50,7 @@ abstract class Entity ...@@ -60,84 +50,7 @@ abstract class Entity
*/ */
public function toJson() public function toJson()
{ {
return json_encode($this->reflect($this)); return json_encode($this->getRawData());
}
/**
* 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;
} }
/** /**
......
...@@ -40,7 +40,7 @@ class ServerResponse extends Entity ...@@ -40,7 +40,7 @@ class ServerResponse extends Entity
*/ */
public function __construct(array $data, $bot_name) 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 (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'])) {
......
...@@ -313,14 +313,12 @@ class Request ...@@ -313,14 +313,12 @@ class Request
self::ensureNonEmptyData($data); self::ensureNonEmptyData($data);
$raw_json = self::execute($action, $data); $response = json_decode(self::execute($action, $data), true);
$response = json_decode($raw_json, true);
if (null === $response) { if (null === $response) {
throw new TelegramException('Telegram returned an invalid response! Please review your bot name and API key.'); 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); 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