Commit cf301e8f authored by MBoretto's avatar MBoretto

Object correctly exported with recursion

parent c166bb21
......@@ -127,7 +127,7 @@ class DB
}
$sth = self::$pdo->prepare($query);
$sth->bindParam(':limit', $limit, \PDO::PARAM_STR);
$sth->bindParam(':limit', $limit, \PDO::PARAM_INT);
$sth->execute();
$results = $sth->fetchAll(\PDO::FETCH_ASSOC);
......@@ -239,11 +239,6 @@ class DB
$new_chat_photo = $message->getNewChatPhoto();
$left_chat_participant = $message->getLeftChatParticipant();
try {
//chats table
$sth2 = self::$pdo->prepare('INSERT INTO `'.TB_CHATS.'`
......
......@@ -21,27 +21,46 @@ class Entity
return $this->bot_name;
}
public function toJSON()
{
$fields = $this->reflect($this);
$json = json_encode($fields);
return $json;
}
public function toJSON()
public function reflect($object)
{
$reflection = new \ReflectionObject($this);
$reflection = new \ReflectionObject($object);
$properties = $reflection->getProperties();
$fields = array();
$fields = [];
foreach ($properties as $property) {
$name = $property->getName();
$property->setAccessible(true);
$value = $property->getValue($this);
$fields[$name] = $value;
}
$json = json_encode($fields);
if ($name == 'bot_name') {
continue;
}
return $json;
if (!$property->isPrivate()) {
if (is_object($object->$name)) {
$fields[$name] = $this->reflect($object->$name);
} else {
$property->setAccessible(true);
$value = $property->getValue($object);
if (is_null($value)) {
continue;
}
$fields[$name] = $value;
}
}
}
return $fields;
}
public function __toString()
{
return $this->toJSON();
......
......@@ -61,13 +61,26 @@ class Message extends Entity
protected $group_chat_created;
protected $command;
private $command;
private $type;
public function __construct(array $data, $bot_name)
{
$this->reply_to_message = isset($data['reply_to_message']) ? $data['reply_to_message'] : null;
if (!empty($this->reply_to_message)) {
$this->reply_to_message = new ReplyToMessage($this->reply_to_message, $bot_name);
}
$this->init($data, $bot_name);
}
//Common init to Message and ReplyToMessage
protected function init(array & $data, & $bot_name)
{
$this->bot_name = $bot_name;
$this->type = 'text';
$this->message_id = isset($data['message_id']) ? $data['message_id'] : null;
......@@ -81,17 +94,17 @@ class Message extends Entity
}
$this->from = new User($this->from);
$this->date = isset($data['date']) ? $data['date'] : null;
if (empty($this->date)) {
throw new TelegramException('date is empty!');
}
$this->chat = isset($data['chat']) ? $data['chat'] : null;
if (empty($this->chat)) {
throw new TelegramException('chat is empty!');
}
$this->chat = new Chat($this->chat);
$this->date = isset($data['date']) ? $data['date'] : null;
if (empty($this->date)) {
throw new TelegramException('date is empty!');
}
$this->forward_from = isset($data['forward_from']) ? $data['forward_from'] : null;
if (!empty($this->forward_from)) {
$this->forward_from = new User($this->forward_from);
......@@ -99,11 +112,6 @@ class Message extends Entity
$this->forward_date = isset($data['forward_date']) ? $data['forward_date'] : null;
$this->reply_to_message = isset($data['reply_to_message']) ? $data['reply_to_message'] : null;
if (!empty($this->reply_to_message)) {
$this->reply_to_message = new Message($this->reply_to_message, $this->bot_name);
}
$this->text = isset($data['text']) ? $data['text'] : null;
$command = $this->getCommand();
if (!empty($command)) {
......@@ -193,6 +201,7 @@ class Message extends Entity
if ($this->group_chat_created) {
$this->type = 'group_chat_created';
}
}
//return the entire command like /echo or /echo@bot1 if specified
......
<?php
/*
* This file is part of the TelegramBot package.
*
* (c) Avtandil Kikabidze aka LONGMAN <akalongman@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Longman\TelegramBot\Entities;
use Longman\TelegramBot\Exception\TelegramException;
class ReplyToMessage extends Message
{
public function __construct(array $data, $bot_name)
{
//As explained in the documentation
//Reply to message can't contain other reply to message entities
$reply_to_message = null;
$this->init($data, $bot_name);
}
}
<?php
/*
* This file is part of the TelegramBot package.
*
* (c) Avtandil Kikabidze aka LONGMAN <akalongman@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tests\Unit;
use \Longman\TelegramBot\Entities\Update;
use \Longman\TelegramBot\Entities\ReplyToMessage;
/**
* @package TelegramTest
* @author Avtandil Kikabidze <akalongman@gmail.com>
* @copyright Avtandil Kikabidze <akalongman@gmail.com>
* @license http://opensource.org/licenses/mit-license.php The MIT License (MIT)
* @link http://www.github.com/akalongman/php-telegram-bot
*/
class ReplyToMessageTest extends TestCase
{
/**
* @var \Longman\TelegramBot\Telegram
*/
private $reply_to_message;
private $message;
/**
* @test
*/
public function testChatType()
{
$json = '
{"update_id":137809335,
"message":{"message_id":4479,"from":{"id":123,"first_name":"John","username":"MJohn"},"chat":{"id":-123,"title":"MyChat","type":"group"},"date":1449092987,"reply_to_message":{"message_id":11,"from":{"id":121,"first_name":"Myname","username":"mybot"},"chat":{"id":-123,"title":"MyChat","type":"group"},"date":1449092984,"text":"type some text"},"text":"some text"}}
';
$struct = json_decode($json, true);
$update = new Update($struct,'mybot');
$this->message = $update->getMessage();
$this->reply_to_message = $this->message->getReplyToMessage();
$this->assertNull($this->reply_to_message->getReplyToMessage());
}
}
<?php
/*
* This file is part of the TelegramBot package.
*
* (c) Avtandil Kikabidze aka LONGMAN <akalongman@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tests\Unit;
use \Longman\TelegramBot\Entities\Update;
/**
* @package TelegramTest
* @author Avtandil Kikabidze <akalongman@gmail.com>
* @copyright Avtandil Kikabidze <akalongman@gmail.com>
* @license http://opensource.org/licenses/mit-license.php The MIT License (MIT)
* @link http://www.github.com/akalongman/php-telegram-bot
*/
class UpdateTest extends TestCase
{
/**
* @var \Longman\TelegramBot\Telegram
*/
private $update;
/**
* @test
*/
public function testUpdateCast()
{
$json = '
{"update_id":137809336,
"message":{"message_id":4479,"from":{"id":123,"first_name":"John","username":"MJohn"},"chat":{"id":-123,"title":"MyChat","type":"group"},"date":1449092987,"reply_to_message":{"message_id":11,"from":{"id":121,"first_name":"Myname","username":"mybot"},"chat":{"id":-123,"title":"MyChat","type":"group"},"date":1449092984,"text":"type some text"},"text":"some text"}}
';
$struct = json_decode($json, true);
$update = new Update($struct, 'mybot');
$array_string_after = json_decode($update->toJSON(), true);
$this->assertEquals($struct, $array_string_after);
}
}
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