Skip to content

Refactoring after new handling of dynamic properties on Entity #1397

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

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 15 additions & 12 deletions src/Entities/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

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

/**
* Class Entity
Expand All @@ -27,7 +28,8 @@
abstract class Entity implements \JsonSerializable
{
public static $fixThumbnailRename = true;
private $parameters = [];

private $fields = [];

/**
* Entity constructor.
Expand Down Expand Up @@ -60,8 +62,9 @@ public function __construct(array $data, string $bot_username = '')
* @param $value
* @return void
*/
public function __set(string $name, $value) : void {
$this->parameters[$name] = $value;
public function __set(string $name, $value) : void
{
$this->fields[$name] = $value;
}

/**
Expand All @@ -70,8 +73,14 @@ public function __set(string $name, $value) : void {
* @param string $name
* @return mixed|null
*/
public function __get(string $name) {
return $this->parameters[$name] ?? 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];
}

/**
Expand All @@ -81,13 +90,7 @@ public function __get(string $name) {
*/
public function jsonSerialize(): array
{
$data = get_object_vars($this);

// Delete unnecessary data
unset($data['raw_data']);
unset($data['bot_username']);

return $data;
return $this->fields;
}

/**
Expand Down
8 changes: 8 additions & 0 deletions src/Exception/UndefinedPropertyException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Longman\TelegramBot\Exception;

class UndefinedPropertyException extends \LogicException
{

}