Skip to content

Commit

Permalink
Handle raw_data better in Entity class
Browse files Browse the repository at this point in the history
  • Loading branch information
noplanman committed May 7, 2023
1 parent f012a9c commit 5e12e1e
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 38 deletions.
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.

0 comments on commit 5e12e1e

Please sign in to comment.