Skip to content

Commit cf301e8

Browse files
committed
Object correctly exported with recursion
1 parent c166bb2 commit cf301e8

File tree

6 files changed

+171
-26
lines changed

6 files changed

+171
-26
lines changed

src/DB.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public static function selectMessages($limit = null)
127127
}
128128

129129
$sth = self::$pdo->prepare($query);
130-
$sth->bindParam(':limit', $limit, \PDO::PARAM_STR);
130+
$sth->bindParam(':limit', $limit, \PDO::PARAM_INT);
131131
$sth->execute();
132132
$results = $sth->fetchAll(\PDO::FETCH_ASSOC);
133133

@@ -239,11 +239,6 @@ public static function insertRequest(Update $update)
239239
$new_chat_photo = $message->getNewChatPhoto();
240240
$left_chat_participant = $message->getLeftChatParticipant();
241241

242-
243-
244-
245-
246-
247242
try {
248243
//chats table
249244
$sth2 = self::$pdo->prepare('INSERT INTO `'.TB_CHATS.'`

src/Entities/Entity.php

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,46 @@ public function getBotName()
2121
return $this->bot_name;
2222
}
2323

24+
public function toJSON()
25+
{
26+
$fields = $this->reflect($this);
27+
$json = json_encode($fields);
2428

29+
return $json;
30+
}
2531

26-
public function toJSON()
32+
33+
public function reflect($object)
2734
{
28-
$reflection = new \ReflectionObject($this);
35+
$reflection = new \ReflectionObject($object);
2936
$properties = $reflection->getProperties();
3037

31-
$fields = array();
38+
$fields = [];
3239

3340
foreach ($properties as $property) {
3441
$name = $property->getName();
35-
$property->setAccessible(true);
36-
$value = $property->getValue($this);
37-
$fields[$name] = $value;
38-
}
3942

40-
$json = json_encode($fields);
43+
if ($name == 'bot_name') {
44+
continue;
45+
}
4146

42-
return $json;
47+
if (!$property->isPrivate()) {
48+
if (is_object($object->$name)) {
49+
$fields[$name] = $this->reflect($object->$name);
50+
} else {
51+
$property->setAccessible(true);
52+
$value = $property->getValue($object);
53+
if (is_null($value)) {
54+
continue;
55+
}
56+
$fields[$name] = $value;
57+
}
58+
}
59+
}
60+
return $fields;
4361
}
4462

63+
4564
public function __toString()
4665
{
4766
return $this->toJSON();

src/Entities/Message.php

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,26 @@ class Message extends Entity
6161

6262
protected $group_chat_created;
6363

64-
protected $command;
64+
private $command;
6565

6666
private $type;
6767

6868
public function __construct(array $data, $bot_name)
69+
{
70+
71+
$this->reply_to_message = isset($data['reply_to_message']) ? $data['reply_to_message'] : null;
72+
if (!empty($this->reply_to_message)) {
73+
$this->reply_to_message = new ReplyToMessage($this->reply_to_message, $bot_name);
74+
}
75+
76+
$this->init($data, $bot_name);
77+
}
78+
79+
//Common init to Message and ReplyToMessage
80+
protected function init(array & $data, & $bot_name)
6981
{
7082
$this->bot_name = $bot_name;
83+
7184
$this->type = 'text';
7285

7386
$this->message_id = isset($data['message_id']) ? $data['message_id'] : null;
@@ -81,29 +94,24 @@ public function __construct(array $data, $bot_name)
8194
}
8295
$this->from = new User($this->from);
8396

84-
$this->date = isset($data['date']) ? $data['date'] : null;
85-
if (empty($this->date)) {
86-
throw new TelegramException('date is empty!');
87-
}
88-
8997
$this->chat = isset($data['chat']) ? $data['chat'] : null;
9098
if (empty($this->chat)) {
9199
throw new TelegramException('chat is empty!');
92100
}
93101
$this->chat = new Chat($this->chat);
94102

103+
$this->date = isset($data['date']) ? $data['date'] : null;
104+
if (empty($this->date)) {
105+
throw new TelegramException('date is empty!');
106+
}
107+
95108
$this->forward_from = isset($data['forward_from']) ? $data['forward_from'] : null;
96109
if (!empty($this->forward_from)) {
97110
$this->forward_from = new User($this->forward_from);
98111
}
99112

100113
$this->forward_date = isset($data['forward_date']) ? $data['forward_date'] : null;
101114

102-
$this->reply_to_message = isset($data['reply_to_message']) ? $data['reply_to_message'] : null;
103-
if (!empty($this->reply_to_message)) {
104-
$this->reply_to_message = new Message($this->reply_to_message, $this->bot_name);
105-
}
106-
107115
$this->text = isset($data['text']) ? $data['text'] : null;
108116
$command = $this->getCommand();
109117
if (!empty($command)) {
@@ -193,6 +201,7 @@ public function __construct(array $data, $bot_name)
193201
if ($this->group_chat_created) {
194202
$this->type = 'group_chat_created';
195203
}
204+
196205
}
197206

198207
//return the entire command like /echo or /echo@bot1 if specified

src/Entities/ReplyToMessage.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the TelegramBot package.
5+
*
6+
* (c) Avtandil Kikabidze aka LONGMAN <akalongman@gmail.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Longman\TelegramBot\Entities;
13+
14+
use Longman\TelegramBot\Exception\TelegramException;
15+
16+
class ReplyToMessage extends Message
17+
{
18+
19+
public function __construct(array $data, $bot_name)
20+
{
21+
22+
//As explained in the documentation
23+
//Reply to message can't contain other reply to message entities
24+
$reply_to_message = null;
25+
26+
$this->init($data, $bot_name);
27+
}
28+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
/*
3+
* This file is part of the TelegramBot package.
4+
*
5+
* (c) Avtandil Kikabidze aka LONGMAN <akalongman@gmail.com>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace Tests\Unit;
11+
12+
use \Longman\TelegramBot\Entities\Update;
13+
use \Longman\TelegramBot\Entities\ReplyToMessage;
14+
15+
/**
16+
* @package TelegramTest
17+
* @author Avtandil Kikabidze <akalongman@gmail.com>
18+
* @copyright Avtandil Kikabidze <akalongman@gmail.com>
19+
* @license http://opensource.org/licenses/mit-license.php The MIT License (MIT)
20+
* @link http://www.github.com/akalongman/php-telegram-bot
21+
*/
22+
class ReplyToMessageTest extends TestCase
23+
{
24+
/**
25+
* @var \Longman\TelegramBot\Telegram
26+
*/
27+
private $reply_to_message;
28+
private $message;
29+
30+
/**
31+
* @test
32+
*/
33+
34+
public function testChatType()
35+
{
36+
$json = '
37+
{"update_id":137809335,
38+
"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"}}
39+
';
40+
$struct = json_decode($json, true);
41+
$update = new Update($struct,'mybot');
42+
43+
$this->message = $update->getMessage();
44+
$this->reply_to_message = $this->message->getReplyToMessage();
45+
46+
$this->assertNull($this->reply_to_message->getReplyToMessage());
47+
48+
}
49+
}

tests/Unit/Entities/UpdateTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
/*
3+
* This file is part of the TelegramBot package.
4+
*
5+
* (c) Avtandil Kikabidze aka LONGMAN <akalongman@gmail.com>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace Tests\Unit;
11+
12+
use \Longman\TelegramBot\Entities\Update;
13+
14+
/**
15+
* @package TelegramTest
16+
* @author Avtandil Kikabidze <akalongman@gmail.com>
17+
* @copyright Avtandil Kikabidze <akalongman@gmail.com>
18+
* @license http://opensource.org/licenses/mit-license.php The MIT License (MIT)
19+
* @link http://www.github.com/akalongman/php-telegram-bot
20+
*/
21+
class UpdateTest extends TestCase
22+
{
23+
/**
24+
* @var \Longman\TelegramBot\Telegram
25+
*/
26+
private $update;
27+
28+
/**
29+
* @test
30+
*/
31+
32+
public function testUpdateCast()
33+
{
34+
$json = '
35+
{"update_id":137809336,
36+
"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"}}
37+
';
38+
$struct = json_decode($json, true);
39+
$update = new Update($struct, 'mybot');
40+
41+
$array_string_after = json_decode($update->toJSON(), true);
42+
$this->assertEquals($struct, $array_string_after);
43+
44+
}
45+
}

0 commit comments

Comments
 (0)