Skip to content

Refactor: better handling of dynamic properties for Entities #1390

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
23 changes: 22 additions & 1 deletion src/Entities/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
* @method array getRawData() Get the raw data passed to this entity
* @method string getBotUsername() Return the bot name passed to this entity
*/
#[\AllowDynamicProperties]
abstract class Entity implements \JsonSerializable
{
public static $fixThumbnailRename = true;
private $parameters = [];

/**
* Entity constructor.
Expand All @@ -53,6 +53,27 @@ public function __construct(array $data, string $bot_username = '')
$this->validate();
}

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

/**
* Gets a dynamic parameter.
*
* @param string $name
* @return mixed|null
*/
public function __get(string $name) {
return $this->parameters[$name] ?? null;
}

/**
* Return the data that should be serialized for Telegram.
*
Expand Down
12 changes: 12 additions & 0 deletions tests/Unit/Entities/EntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,16 @@ public function testEscapeMarkdown(): void
self::assertEquals('\_\*\[\]\(\)\~\`\>\#\+\-\=\|\{\}\.\!', Entity::escapeMarkdownV2('_*[]()~`>#+-=|{}.!'));
self::assertEquals('\*mark\*\_down\_\~test\~', Entity::escapeMarkdownV2('*mark*_down_~test~'));
}
public function testSettingDynamicParameterWorks(): void {
$anonClass = new class([]) extends Entity {};

$anonClass->newParameter = 'test';

$this->assertEquals('test', $anonClass->newParameter);
}
public function testGettingUnknownDynamicParameterReturnsNull(): void {
$anonClass = new class([]) extends Entity {};

$this->assertEquals(null, $anonClass->unknownParameter);
}
}