Skip to content

Commit 7e419ed

Browse files
authored
Merge pull request #1399 from php-telegram-bot/improve-entity-data-handling
Handle raw_data better in Entity class
2 parents f012a9c + 5e12e1e commit 7e419ed

File tree

3 files changed

+10
-38
lines changed

3 files changed

+10
-38
lines changed

src/Entities/Entity.php

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
use Longman\TelegramBot\Entities\InlineQuery\InlineEntity;
1515
use Longman\TelegramBot\Entities\InputMedia\InputMedia;
16-
use Longman\TelegramBot\Exception\UndefinedPropertyException;
1716

1817
/**
1918
* Class Entity
@@ -29,6 +28,9 @@ abstract class Entity implements \JsonSerializable
2928
{
3029
public static $fixThumbnailRename = true;
3130

31+
public $bot_username = '';
32+
public $raw_data = [];
33+
3234
private $fields = [];
3335

3436
/**
@@ -41,46 +43,34 @@ abstract class Entity implements \JsonSerializable
4143
*/
4244
public function __construct(array $data, string $bot_username = '')
4345
{
44-
//Make sure we're not raw_data inception-ing
45-
if (array_key_exists('raw_data', $data)) {
46-
if ($data['raw_data'] === null) {
47-
unset($data['raw_data']);
48-
}
49-
} else {
50-
$data['raw_data'] = $data;
51-
}
46+
$this->bot_username = $bot_username;
47+
$this->raw_data = $data;
5248

53-
$data['bot_username'] = $bot_username;
5449
$this->assignMemberVariables($data);
5550
$this->validate();
5651
}
5752

5853
/**
59-
* Dynamically sets a parameter.
54+
* Dynamically set a field.
6055
*
6156
* @param string $name
62-
* @param $value
57+
* @param mixed $value
6358
* @return void
6459
*/
65-
public function __set(string $name, $value) : void
60+
public function __set(string $name, mixed $value): void
6661
{
6762
$this->fields[$name] = $value;
6863
}
6964

7065
/**
71-
* Gets a dynamic parameter.
66+
* Gets a dynamic field.
7267
*
7368
* @param string $name
7469
* @return mixed|null
7570
*/
7671
public function __get(string $name)
7772
{
78-
if (! isset($this->fields[$name])) {
79-
$class = static::class;
80-
throw new UndefinedPropertyException("Undefined property: {$class}::\${$name}");
81-
}
82-
83-
return $this->fields[$name];
73+
return $this->fields[$name] ?? null;
8474
}
8575

8676
/**

src/Entities/ServerResponse.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,6 @@ class ServerResponse extends Entity
3737
*/
3838
public function __construct(array $data, string $bot_username = '')
3939
{
40-
// Make sure we don't double-save the raw_data
41-
unset($data['raw_data']);
42-
$data['raw_data'] = $data;
43-
4440
$is_ok = (bool) ($data['ok'] ?? false);
4541
$result = $data['result'] ?? null;
4642

@@ -131,9 +127,6 @@ private function createResultObject(array $result, string $bot_username): Entity
131127
$action = Request::getCurrentAction();
132128
$object_class = $result_object_types[$action] ?? Message::class;
133129

134-
// We don't need to save the raw_data of the response object!
135-
$result['raw_data'] = null;
136-
137130
return Factory::resolveEntityClass($object_class, $result, $bot_username);
138131
}
139132

@@ -163,9 +156,6 @@ private function createResultObjects(array $results, string $bot_username): arra
163156
$objects = [];
164157

165158
foreach ($results as $result) {
166-
// We don't need to save the raw_data of the response object!
167-
$result['raw_data'] = null;
168-
169159
$objects[] = Factory::resolveEntityClass($object_class, $result, $bot_username);
170160
}
171161

src/Exception/UndefinedPropertyException.php

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)