Skip to content

Commit

Permalink
Merge pull request #1397 from php-telegram-bot/refactor/dynamic-prope…
Browse files Browse the repository at this point in the history
…rties

Refactoring after new handling of dynamic properties on Entity
  • Loading branch information
TiiFuchs committed May 7, 2023
2 parents 37355b4 + c384110 commit f012a9c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
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}");

This comment has been minimized.

Copy link
@noplanman

noplanman May 7, 2023

Member

@TiiFuchs I'm just testing this and wondering if it makes sense to throw here or just return null.

Internally for the getProperty() call we can just catch and ignore it, but I do wonder how backwards breaking this is for situations where users might try to get a property and expect null, which now breaks.

}

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
{

}

0 comments on commit f012a9c

Please sign in to comment.