Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle raw_data better in Entity class #1399

Merged
merged 1 commit into from
May 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 10 additions & 20 deletions src/Entities/Entity.php
Expand Up @@ -13,7 +13,6 @@

use Longman\TelegramBot\Entities\InlineQuery\InlineEntity;
use Longman\TelegramBot\Entities\InputMedia\InputMedia;
use Longman\TelegramBot\Exception\UndefinedPropertyException;

/**
* Class Entity
Expand All @@ -29,6 +28,9 @@ abstract class Entity implements \JsonSerializable
{
public static $fixThumbnailRename = true;

public $bot_username = '';
public $raw_data = [];

private $fields = [];

/**
Expand All @@ -41,46 +43,34 @@ abstract class Entity implements \JsonSerializable
*/
public function __construct(array $data, string $bot_username = '')
{
//Make sure we're not raw_data inception-ing
if (array_key_exists('raw_data', $data)) {
if ($data['raw_data'] === null) {
unset($data['raw_data']);
}
} else {
$data['raw_data'] = $data;
}
$this->bot_username = $bot_username;
$this->raw_data = $data;

$data['bot_username'] = $bot_username;
$this->assignMemberVariables($data);
$this->validate();
}

/**
* Dynamically sets a parameter.
* Dynamically set a field.
*
* @param string $name
* @param $value
* @param mixed $value
* @return void
*/
public function __set(string $name, $value) : void
public function __set(string $name, mixed $value): void
{
$this->fields[$name] = $value;
}

/**
* Gets a dynamic parameter.
* Gets a dynamic field.
*
* @param string $name
* @return mixed|null
*/
public function __get(string $name)
{
if (! isset($this->fields[$name])) {
$class = static::class;
throw new UndefinedPropertyException("Undefined property: {$class}::\${$name}");
}

return $this->fields[$name];
return $this->fields[$name] ?? null;
}

/**
Expand Down
10 changes: 0 additions & 10 deletions src/Entities/ServerResponse.php
Expand Up @@ -37,10 +37,6 @@ class ServerResponse extends Entity
*/
public function __construct(array $data, string $bot_username = '')
{
// Make sure we don't double-save the raw_data
unset($data['raw_data']);
$data['raw_data'] = $data;

$is_ok = (bool) ($data['ok'] ?? false);
$result = $data['result'] ?? null;

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

// We don't need to save the raw_data of the response object!
$result['raw_data'] = null;

return Factory::resolveEntityClass($object_class, $result, $bot_username);
}

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

foreach ($results as $result) {
// We don't need to save the raw_data of the response object!
$result['raw_data'] = null;

$objects[] = Factory::resolveEntityClass($object_class, $result, $bot_username);
}

Expand Down
8 changes: 0 additions & 8 deletions src/Exception/UndefinedPropertyException.php

This file was deleted.