Skip to content

Commit

Permalink
Merge pull request #1390 from alesinicio/develop
Browse files Browse the repository at this point in the history
Refactor: better handling of dynamic properties for Entities
  • Loading branch information
TiiFuchs committed May 7, 2023
2 parents de24a76 + 90d13e7 commit 37355b4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
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);
}
}

0 comments on commit 37355b4

Please sign in to comment.