@@ -10,57 +10,57 @@ |
||
| 10 | 10 | |
| 11 | 11 | class WebhookTest extends \PHPUnit\Framework\TestCase { |
| 12 | 12 | |
| 13 | - public function testAnonymousPlugins() { |
|
| 14 | - $plugin = new class($this) extends Plugin { |
|
| 13 | + public function testAnonymousPlugins() { |
|
| 14 | + $plugin = new class($this) extends Plugin { |
|
| 15 | 15 | |
| 16 | - private $testCase; |
|
| 16 | + private $testCase; |
|
| 17 | 17 | |
| 18 | - public function __construct(TestCase $testCase) { |
|
| 18 | + public function __construct(TestCase $testCase) { |
|
| 19 | 19 | $this->testCase = $testCase; |
| 20 | - } |
|
| 20 | + } |
|
| 21 | 21 | |
| 22 | - public function onMessage(int $update_id, Message $message): \Generator { |
|
| 22 | + public function onMessage(int $update_id, Message $message): \Generator { |
|
| 23 | 23 | $this->testCase->assertEquals(1, $update_id); |
| 24 | 24 | $this->testCase->assertEquals('Hello World!', $message->getText()); |
| 25 | 25 | yield; |
| 26 | - } |
|
| 26 | + } |
|
| 27 | 27 | |
| 28 | - }; |
|
| 28 | + }; |
|
| 29 | 29 | |
| 30 | - $message = DummyUpdate::message(); |
|
| 31 | - $message->set('text', 'Hello World!'); |
|
| 30 | + $message = DummyUpdate::message(); |
|
| 31 | + $message->set('text', 'Hello World!'); |
|
| 32 | 32 | |
| 33 | - (new UpdateHandler())->addPlugins($plugin)->resolve(new Update([ |
|
| 34 | - 'update_id' => 1, |
|
| 35 | - 'message' => $message->getRawData(), |
|
| 36 | - ])); |
|
| 37 | - } |
|
| 33 | + (new UpdateHandler())->addPlugins($plugin)->resolve(new Update([ |
|
| 34 | + 'update_id' => 1, |
|
| 35 | + 'message' => $message->getRawData(), |
|
| 36 | + ])); |
|
| 37 | + } |
|
| 38 | 38 | |
| 39 | - public function testFilterIncomingUpdates() { |
|
| 39 | + public function testFilterIncomingUpdates() { |
|
| 40 | 40 | |
| 41 | - $plugin = new class($this) extends Plugin { |
|
| 41 | + $plugin = new class($this) extends Plugin { |
|
| 42 | 42 | |
| 43 | - public function __construct(TestCase $testCase) { |
|
| 43 | + public function __construct(TestCase $testCase) { |
|
| 44 | 44 | $this->testCase = $testCase; |
| 45 | - } |
|
| 45 | + } |
|
| 46 | 46 | |
| 47 | - public function __process(Update $update): void { |
|
| 47 | + public function __process(Update $update): void { |
|
| 48 | 48 | $this->testCase->fail('This should not be called'); |
| 49 | - } |
|
| 50 | - }; |
|
| 49 | + } |
|
| 50 | + }; |
|
| 51 | 51 | |
| 52 | - $handler = (new UpdateHandler())->addPlugins($plugin); |
|
| 52 | + $handler = (new UpdateHandler())->addPlugins($plugin); |
|
| 53 | 53 | |
| 54 | - $handler->filterIncomingUpdates([ |
|
| 55 | - Update::TYPE_MESSAGE |
|
| 56 | - ]); |
|
| 54 | + $handler->filterIncomingUpdates([ |
|
| 55 | + Update::TYPE_MESSAGE |
|
| 56 | + ]); |
|
| 57 | 57 | |
| 58 | - $handler->resolve(new Update([ |
|
| 59 | - 'update_id' => 1, |
|
| 60 | - 'message' => DummyUpdate::message()->getRawData(), |
|
| 61 | - ])); |
|
| 58 | + $handler->resolve(new Update([ |
|
| 59 | + 'update_id' => 1, |
|
| 60 | + 'message' => DummyUpdate::message()->getRawData(), |
|
| 61 | + ])); |
|
| 62 | 62 | |
| 63 | - $this->assertTrue(true); |
|
| 64 | - } |
|
| 63 | + $this->assertTrue(true); |
|
| 64 | + } |
|
| 65 | 65 | |
| 66 | 66 | } |
| 67 | 67 | \ No newline at end of file |
@@ -9,114 +9,114 @@ |
||
| 9 | 9 | class DummyUpdate { |
| 10 | 10 | |
| 11 | 11 | |
| 12 | - private static function faker(): Generator { |
|
| 13 | - return Factory::create(); |
|
| 14 | - } |
|
| 12 | + private static function faker(): Generator { |
|
| 13 | + return Factory::create(); |
|
| 14 | + } |
|
| 15 | 15 | |
| 16 | - public static function message(): Message { |
|
| 17 | - return new Message([ |
|
| 18 | - 'message_id' => self::faker()->randomNumber(), |
|
| 19 | - 'from' => [ |
|
| 16 | + public static function message(): Message { |
|
| 17 | + return new Message([ |
|
| 18 | + 'message_id' => self::faker()->randomNumber(), |
|
| 19 | + 'from' => [ |
|
| 20 | 20 | 'id' => self::faker()->randomNumber(), |
| 21 | 21 | 'is_bot' => false, |
| 22 | 22 | 'first_name' => self::faker()->firstName(), |
| 23 | 23 | 'last_name' => self::faker()->lastName(), |
| 24 | 24 | 'username' => self::faker()->userName(), |
| 25 | 25 | 'language_code' => self::faker()->languageCode(), |
| 26 | - ], |
|
| 27 | - 'chat' => [ |
|
| 26 | + ], |
|
| 27 | + 'chat' => [ |
|
| 28 | 28 | 'id' => self::faker()->randomNumber(), |
| 29 | 29 | 'first_name' => self::faker()->firstName(), |
| 30 | 30 | 'last_name' => self::faker()->lastName(), |
| 31 | 31 | 'username' => self::faker()->userName(), |
| 32 | 32 | 'type' => 'private', |
| 33 | - ], |
|
| 34 | - 'date' => self::faker()->unixTime(), |
|
| 35 | - 'text' => self::faker()->sentence(), |
|
| 36 | - ]); |
|
| 37 | - } |
|
| 33 | + ], |
|
| 34 | + 'date' => self::faker()->unixTime(), |
|
| 35 | + 'text' => self::faker()->sentence(), |
|
| 36 | + ]); |
|
| 37 | + } |
|
| 38 | 38 | |
| 39 | - public static function messageWithEntities(): Message { |
|
| 40 | - return new Message([ |
|
| 41 | - 'message_id' => self::faker()->randomNumber(), |
|
| 42 | - 'from' => [ |
|
| 39 | + public static function messageWithEntities(): Message { |
|
| 40 | + return new Message([ |
|
| 41 | + 'message_id' => self::faker()->randomNumber(), |
|
| 42 | + 'from' => [ |
|
| 43 | 43 | 'id' => self::faker()->randomNumber(), |
| 44 | 44 | 'is_bot' => false, |
| 45 | 45 | 'first_name' => self::faker()->firstName(), |
| 46 | 46 | 'last_name' => self::faker()->lastName(), |
| 47 | 47 | 'username' => self::faker()->userName(), |
| 48 | 48 | 'language_code' => self::faker()->languageCode(), |
| 49 | - ], |
|
| 50 | - 'chat' => [ |
|
| 49 | + ], |
|
| 50 | + 'chat' => [ |
|
| 51 | 51 | 'id' => self::faker()->randomNumber(), |
| 52 | 52 | 'first_name' => self::faker()->firstName(), |
| 53 | 53 | 'last_name' => self::faker()->lastName(), |
| 54 | 54 | 'username' => self::faker()->userName(), |
| 55 | 55 | 'type' => 'private', |
| 56 | - ], |
|
| 57 | - 'date' => self::faker()->unixTime(), |
|
| 58 | - 'text' => self::faker()->sentence(), |
|
| 59 | - 'entities' => [ |
|
| 56 | + ], |
|
| 57 | + 'date' => self::faker()->unixTime(), |
|
| 58 | + 'text' => self::faker()->sentence(), |
|
| 59 | + 'entities' => [ |
|
| 60 | 60 | [ |
| 61 | - 'offset' => 0, |
|
| 62 | - 'length' => 1, |
|
| 63 | - 'type' => 'bold', |
|
| 61 | + 'offset' => 0, |
|
| 62 | + 'length' => 1, |
|
| 63 | + 'type' => 'bold', |
|
| 64 | 64 | ], |
| 65 | 65 | [ |
| 66 | - 'offset' => 2, |
|
| 67 | - 'length' => 3, |
|
| 68 | - 'type' => 'italic', |
|
| 66 | + 'offset' => 2, |
|
| 67 | + 'length' => 3, |
|
| 68 | + 'type' => 'italic', |
|
| 69 | 69 | ], |
| 70 | 70 | [ |
| 71 | - 'offset' => 6, |
|
| 72 | - 'length' => 7, |
|
| 73 | - 'type' => 'code', |
|
| 71 | + 'offset' => 6, |
|
| 72 | + 'length' => 7, |
|
| 73 | + 'type' => 'code', |
|
| 74 | + ] |
|
| 74 | 75 | ] |
| 75 | - ] |
|
| 76 | - ]); |
|
| 77 | - } |
|
| 76 | + ]); |
|
| 77 | + } |
|
| 78 | 78 | |
| 79 | - public static function messageWithReply(): Message { |
|
| 80 | - return new Message([ |
|
| 81 | - 'message_id' => self::faker()->randomNumber(), |
|
| 82 | - 'from' => [ |
|
| 79 | + public static function messageWithReply(): Message { |
|
| 80 | + return new Message([ |
|
| 81 | + 'message_id' => self::faker()->randomNumber(), |
|
| 82 | + 'from' => [ |
|
| 83 | 83 | 'id' => self::faker()->randomNumber(), |
| 84 | 84 | 'is_bot' => false, |
| 85 | 85 | 'first_name' => self::faker()->firstName(), |
| 86 | 86 | 'last_name' => self::faker()->lastName(), |
| 87 | 87 | 'username' => self::faker()->userName(), |
| 88 | 88 | 'language_code' => self::faker()->languageCode(), |
| 89 | - ], |
|
| 90 | - 'chat' => [ |
|
| 89 | + ], |
|
| 90 | + 'chat' => [ |
|
| 91 | 91 | 'id' => self::faker()->randomNumber(), |
| 92 | 92 | 'first_name' => self::faker()->firstName(), |
| 93 | 93 | 'last_name' => self::faker()->lastName(), |
| 94 | 94 | 'username' => self::faker()->userName(), |
| 95 | 95 | 'type' => 'private', |
| 96 | - ], |
|
| 97 | - 'date' => self::faker()->unixTime(), |
|
| 98 | - 'text' => self::faker()->sentence(), |
|
| 99 | - 'reply_to_message' => [ |
|
| 96 | + ], |
|
| 97 | + 'date' => self::faker()->unixTime(), |
|
| 98 | + 'text' => self::faker()->sentence(), |
|
| 99 | + 'reply_to_message' => [ |
|
| 100 | 100 | 'message_id' => self::faker()->randomNumber(), |
| 101 | 101 | 'from' => [ |
| 102 | - 'id' => self::faker()->randomNumber(), |
|
| 103 | - 'is_bot' => false, |
|
| 104 | - 'first_name' => self::faker()->firstName(), |
|
| 105 | - 'last_name' => self::faker()->lastName(), |
|
| 106 | - 'username' => self::faker()->userName(), |
|
| 107 | - 'language_code' => self::faker()->languageCode(), |
|
| 102 | + 'id' => self::faker()->randomNumber(), |
|
| 103 | + 'is_bot' => false, |
|
| 104 | + 'first_name' => self::faker()->firstName(), |
|
| 105 | + 'last_name' => self::faker()->lastName(), |
|
| 106 | + 'username' => self::faker()->userName(), |
|
| 107 | + 'language_code' => self::faker()->languageCode(), |
|
| 108 | 108 | ], |
| 109 | 109 | 'chat' => [ |
| 110 | - 'id' => self::faker()->randomNumber(), |
|
| 111 | - 'first_name' => self::faker()->firstName(), |
|
| 112 | - 'last_name' => self::faker()->lastName(), |
|
| 113 | - 'username' => self::faker()->userName(), |
|
| 114 | - 'type' => 'private', |
|
| 110 | + 'id' => self::faker()->randomNumber(), |
|
| 111 | + 'first_name' => self::faker()->firstName(), |
|
| 112 | + 'last_name' => self::faker()->lastName(), |
|
| 113 | + 'username' => self::faker()->userName(), |
|
| 114 | + 'type' => 'private', |
|
| 115 | 115 | ], |
| 116 | 116 | 'date' => self::faker()->unixTime(), |
| 117 | 117 | 'text' => self::faker()->sentence(), |
| 118 | - ] |
|
| 119 | - ]); |
|
| 120 | - } |
|
| 118 | + ] |
|
| 119 | + ]); |
|
| 120 | + } |
|
| 121 | 121 | |
| 122 | 122 | } |
| 123 | 123 | \ No newline at end of file |
@@ -17,237 +17,237 @@ |
||
| 17 | 17 | */ |
| 18 | 18 | class UpdateHandler extends Telegram implements HandlerInterface { |
| 19 | 19 | |
| 20 | - use HandlerTrait; |
|
| 21 | - |
|
| 22 | - /** |
|
| 23 | - * @var ?Update |
|
| 24 | - */ |
|
| 25 | - protected ?Update $update; |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * @var Plugin[] |
|
| 29 | - */ |
|
| 30 | - private array $plugins = []; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * @var bool |
|
| 34 | - */ |
|
| 35 | - private bool $active_spreader = false; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * The default configuration of the webhook. |
|
| 39 | - * |
|
| 40 | - * @var array |
|
| 41 | - */ |
|
| 42 | - private array $config = [ |
|
| 43 | - 'autoload_env_file' => false, |
|
| 44 | - 'env_file_path' => null, |
|
| 45 | - ]; |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * Filter incoming updates. |
|
| 49 | - * |
|
| 50 | - * @var array |
|
| 51 | - */ |
|
| 52 | - private array $filterIncomingUpdates = []; |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * Webhook constructor. |
|
| 56 | - * |
|
| 57 | - * @param string $api_token The API key of the bot. Leave it blank for autoload from .env file. |
|
| 58 | - */ |
|
| 59 | - public function __construct(string $api_token = '') { |
|
| 60 | - parent::__construct($api_token); |
|
| 61 | - |
|
| 62 | - if (!Telegram::validateToken(self::getApiToken())) { |
|
| 63 | - throw new InvalidBotTokenException(); |
|
| 64 | - } |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * Resolve the request on single plugin. |
|
| 69 | - * |
|
| 70 | - * @param Plugin $plugin The plugin to work with |
|
| 71 | - * @param ?Update $update The custom to work with |
|
| 72 | - * @param array $config The configuration of the receiver |
|
| 73 | - * @return void |
|
| 74 | - */ |
|
| 75 | - public static function resolveOn(Plugin $plugin, Update $update = null, array $config = []): void { |
|
| 76 | - // TODO: Implement resolveOn() method. |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - /** |
|
| 80 | - * Add plugins to the receiver |
|
| 81 | - * |
|
| 82 | - * @param Plugin[]|array $plugins |
|
| 83 | - * @retrun void |
|
| 84 | - */ |
|
| 85 | - public function addPlugins(Plugin|array $plugins): UpdateHandler { |
|
| 86 | - if (is_object($plugins)) { |
|
| 87 | - $plugins = [$plugins]; |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - foreach ($plugins as $plugin) { |
|
| 91 | - if (!is_subclass_of($plugin, Plugin::class)) { |
|
| 20 | + use HandlerTrait; |
|
| 21 | + |
|
| 22 | + /** |
|
| 23 | + * @var ?Update |
|
| 24 | + */ |
|
| 25 | + protected ?Update $update; |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * @var Plugin[] |
|
| 29 | + */ |
|
| 30 | + private array $plugins = []; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * @var bool |
|
| 34 | + */ |
|
| 35 | + private bool $active_spreader = false; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * The default configuration of the webhook. |
|
| 39 | + * |
|
| 40 | + * @var array |
|
| 41 | + */ |
|
| 42 | + private array $config = [ |
|
| 43 | + 'autoload_env_file' => false, |
|
| 44 | + 'env_file_path' => null, |
|
| 45 | + ]; |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * Filter incoming updates. |
|
| 49 | + * |
|
| 50 | + * @var array |
|
| 51 | + */ |
|
| 52 | + private array $filterIncomingUpdates = []; |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * Webhook constructor. |
|
| 56 | + * |
|
| 57 | + * @param string $api_token The API key of the bot. Leave it blank for autoload from .env file. |
|
| 58 | + */ |
|
| 59 | + public function __construct(string $api_token = '') { |
|
| 60 | + parent::__construct($api_token); |
|
| 61 | + |
|
| 62 | + if (!Telegram::validateToken(self::getApiToken())) { |
|
| 63 | + throw new InvalidBotTokenException(); |
|
| 64 | + } |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * Resolve the request on single plugin. |
|
| 69 | + * |
|
| 70 | + * @param Plugin $plugin The plugin to work with |
|
| 71 | + * @param ?Update $update The custom to work with |
|
| 72 | + * @param array $config The configuration of the receiver |
|
| 73 | + * @return void |
|
| 74 | + */ |
|
| 75 | + public static function resolveOn(Plugin $plugin, Update $update = null, array $config = []): void { |
|
| 76 | + // TODO: Implement resolveOn() method. |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + /** |
|
| 80 | + * Add plugins to the receiver |
|
| 81 | + * |
|
| 82 | + * @param Plugin[]|array $plugins |
|
| 83 | + * @retrun void |
|
| 84 | + */ |
|
| 85 | + public function addPlugins(Plugin|array $plugins): UpdateHandler { |
|
| 86 | + if (is_object($plugins)) { |
|
| 87 | + $plugins = [$plugins]; |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + foreach ($plugins as $plugin) { |
|
| 91 | + if (!is_subclass_of($plugin, Plugin::class)) { |
|
| 92 | 92 | throw new \RuntimeException( |
| 93 | - sprintf('The plugin %s must be an instance of %s', get_class($plugin), Plugin::class) |
|
| 93 | + sprintf('The plugin %s must be an instance of %s', get_class($plugin), Plugin::class) |
|
| 94 | 94 | ); |
| 95 | - } |
|
| 95 | + } |
|
| 96 | 96 | |
| 97 | - $reflection = Toolkit::reflectionClass($plugin); |
|
| 98 | - $this->plugins[] = [ |
|
| 97 | + $reflection = Toolkit::reflectionClass($plugin); |
|
| 98 | + $this->plugins[] = [ |
|
| 99 | 99 | 'class' => $plugin, |
| 100 | 100 | 'initialized' => is_object($plugin), |
| 101 | - ]; |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - return $this; |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * Updates the filter for incoming updates. |
|
| 109 | - * |
|
| 110 | - * @param array $filter |
|
| 111 | - * @return void |
|
| 112 | - */ |
|
| 113 | - public function filterIncomingUpdates(array $filter): void { |
|
| 114 | - $this->filterIncomingUpdates = $filter; |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - /** |
|
| 118 | - * Do not process updates that match the filter. |
|
| 119 | - * |
|
| 120 | - * @param Update $update |
|
| 121 | - * @return bool |
|
| 122 | - */ |
|
| 123 | - private function isFiltered(Update $update): bool { |
|
| 124 | - if (empty($this->filterIncomingUpdates)) { |
|
| 125 | - return false; |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - foreach ($this->filterIncomingUpdates as $type => $value) { |
|
| 129 | - if (is_int($type)) { |
|
| 101 | + ]; |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + return $this; |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * Updates the filter for incoming updates. |
|
| 109 | + * |
|
| 110 | + * @param array $filter |
|
| 111 | + * @return void |
|
| 112 | + */ |
|
| 113 | + public function filterIncomingUpdates(array $filter): void { |
|
| 114 | + $this->filterIncomingUpdates = $filter; |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + /** |
|
| 118 | + * Do not process updates that match the filter. |
|
| 119 | + * |
|
| 120 | + * @param Update $update |
|
| 121 | + * @return bool |
|
| 122 | + */ |
|
| 123 | + private function isFiltered(Update $update): bool { |
|
| 124 | + if (empty($this->filterIncomingUpdates)) { |
|
| 125 | + return false; |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + foreach ($this->filterIncomingUpdates as $type => $value) { |
|
| 129 | + if (is_int($type)) { |
|
| 130 | 130 | if ($update->getUpdateType() === $value) { |
| 131 | - return true; |
|
| 131 | + return true; |
|
| 132 | 132 | } |
| 133 | - } elseif (is_string($type)) { |
|
| 133 | + } elseif (is_string($type)) { |
|
| 134 | 134 | if ($update->getUpdateType() === $type) { |
| 135 | - if (is_callable($value)) { |
|
| 136 | - return $value($update); |
|
| 137 | - } elseif (is_bool($value)) { |
|
| 138 | - return $value; |
|
| 139 | - } |
|
| 140 | - throw new \InvalidArgumentException('The value of the filter must be a callable or a boolean'); |
|
| 135 | + if (is_callable($value)) { |
|
| 136 | + return $value($update); |
|
| 137 | + } elseif (is_bool($value)) { |
|
| 138 | + return $value; |
|
| 139 | + } |
|
| 140 | + throw new \InvalidArgumentException('The value of the filter must be a callable or a boolean'); |
|
| 141 | + } |
|
| 141 | 142 | } |
| 142 | - } |
|
| 143 | - throw new \InvalidArgumentException('Invalid filter'); |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - return false; |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - /** |
|
| 150 | - * Resolve the request. |
|
| 151 | - * |
|
| 152 | - * @param ?Update $update The custom to work with |
|
| 153 | - * @param array $config The configuration of the receiver |
|
| 154 | - * |
|
| 155 | - * @retrun void |
|
| 156 | - */ |
|
| 157 | - public function resolve(Update|null $update = null, array $config = []): void { |
|
| 158 | - $this->update = $update ?? Telegram::getUpdate(); |
|
| 159 | - |
|
| 160 | - if (empty($this->update)) { |
|
| 161 | - TelegramLog::error('The update is empty, the request is not processed'); |
|
| 162 | - return; |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - if ($this->isFiltered($this->update)) { |
|
| 166 | - TelegramLog::notice('The update is filtered, the request is not processed'); |
|
| 167 | - return; |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - if (!method_exists($this, '__process')) { |
|
| 171 | - throw new \RuntimeException('The method __process does not exist'); |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - if (is_array($config)) { |
|
| 175 | - $this->updateConfiguration($config); |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - putenv('TG_CURRENT_UPDATE=' . $this->update->getRawData(false)); |
|
| 179 | - |
|
| 180 | - $this->__process($this->update); |
|
| 181 | - $this->loadPlugins($this->plugins); |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - /** |
|
| 185 | - * Update the configuration |
|
| 186 | - * |
|
| 187 | - * @param array $configuration |
|
| 188 | - * @return void |
|
| 189 | - */ |
|
| 190 | - public function updateConfiguration(array $configuration): void { |
|
| 191 | - $this->config = array_merge($this->config, $configuration); |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - /** |
|
| 195 | - * Match the update with the given plugins |
|
| 196 | - * |
|
| 197 | - * @param Plugin[]|array $plugins |
|
| 198 | - * @return void |
|
| 199 | - */ |
|
| 200 | - private function loadPlugins(array $plugins): void { |
|
| 201 | - $update = $update ?? ($this->update ?? Telegram::getUpdate()); |
|
| 202 | - |
|
| 203 | - foreach ($plugins as $plugin) { |
|
| 204 | - if (!is_subclass_of($plugin['class'], Plugin::class)) { |
|
| 143 | + throw new \InvalidArgumentException('Invalid filter'); |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + return false; |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + /** |
|
| 150 | + * Resolve the request. |
|
| 151 | + * |
|
| 152 | + * @param ?Update $update The custom to work with |
|
| 153 | + * @param array $config The configuration of the receiver |
|
| 154 | + * |
|
| 155 | + * @retrun void |
|
| 156 | + */ |
|
| 157 | + public function resolve(Update|null $update = null, array $config = []): void { |
|
| 158 | + $this->update = $update ?? Telegram::getUpdate(); |
|
| 159 | + |
|
| 160 | + if (empty($this->update)) { |
|
| 161 | + TelegramLog::error('The update is empty, the request is not processed'); |
|
| 162 | + return; |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + if ($this->isFiltered($this->update)) { |
|
| 166 | + TelegramLog::notice('The update is filtered, the request is not processed'); |
|
| 167 | + return; |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + if (!method_exists($this, '__process')) { |
|
| 171 | + throw new \RuntimeException('The method __process does not exist'); |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + if (is_array($config)) { |
|
| 175 | + $this->updateConfiguration($config); |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + putenv('TG_CURRENT_UPDATE=' . $this->update->getRawData(false)); |
|
| 179 | + |
|
| 180 | + $this->__process($this->update); |
|
| 181 | + $this->loadPlugins($this->plugins); |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + /** |
|
| 185 | + * Update the configuration |
|
| 186 | + * |
|
| 187 | + * @param array $configuration |
|
| 188 | + * @return void |
|
| 189 | + */ |
|
| 190 | + public function updateConfiguration(array $configuration): void { |
|
| 191 | + $this->config = array_merge($this->config, $configuration); |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + /** |
|
| 195 | + * Match the update with the given plugins |
|
| 196 | + * |
|
| 197 | + * @param Plugin[]|array $plugins |
|
| 198 | + * @return void |
|
| 199 | + */ |
|
| 200 | + private function loadPlugins(array $plugins): void { |
|
| 201 | + $update = $update ?? ($this->update ?? Telegram::getUpdate()); |
|
| 202 | + |
|
| 203 | + foreach ($plugins as $plugin) { |
|
| 204 | + if (!is_subclass_of($plugin['class'], Plugin::class)) { |
|
| 205 | 205 | throw new \InvalidArgumentException(sprintf( |
| 206 | - 'The plugin %s must be an instance of %s', |
|
| 207 | - get_class($plugin['class']), Plugin::class |
|
| 206 | + 'The plugin %s must be an instance of %s', |
|
| 207 | + get_class($plugin['class']), Plugin::class |
|
| 208 | 208 | )); |
| 209 | - } |
|
| 210 | - } |
|
| 209 | + } |
|
| 210 | + } |
|
| 211 | 211 | |
| 212 | - if (!$update instanceof Update) { |
|
| 213 | - throw new \InvalidArgumentException(sprintf( |
|
| 212 | + if (!$update instanceof Update) { |
|
| 213 | + throw new \InvalidArgumentException(sprintf( |
|
| 214 | 214 | 'The update must be an instance of %s. %s given', |
| 215 | 215 | Update::class, gettype($update) |
| 216 | - )); |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - $this->spreadUpdateWith($update, $plugins); |
|
| 220 | - } |
|
| 221 | - |
|
| 222 | - /** |
|
| 223 | - * This function will get update and spread it to the plugins |
|
| 224 | - * |
|
| 225 | - * @param Update $update |
|
| 226 | - * @param array<Plugin> $plugins |
|
| 227 | - * @return void |
|
| 228 | - */ |
|
| 229 | - private function spreadUpdateWith(Update $update, array $plugins): void { |
|
| 230 | - $this->active_spreader = true; |
|
| 231 | - |
|
| 232 | - foreach ($plugins as $plugin) { |
|
| 233 | - if ($plugin['initialized'] === false) { |
|
| 216 | + )); |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + $this->spreadUpdateWith($update, $plugins); |
|
| 220 | + } |
|
| 221 | + |
|
| 222 | + /** |
|
| 223 | + * This function will get update and spread it to the plugins |
|
| 224 | + * |
|
| 225 | + * @param Update $update |
|
| 226 | + * @param array<Plugin> $plugins |
|
| 227 | + * @return void |
|
| 228 | + */ |
|
| 229 | + private function spreadUpdateWith(Update $update, array $plugins): void { |
|
| 230 | + $this->active_spreader = true; |
|
| 231 | + |
|
| 232 | + foreach ($plugins as $plugin) { |
|
| 233 | + if ($plugin['initialized'] === false) { |
|
| 234 | 234 | $plugin['class'] = new $plugin['class'](); |
| 235 | - } |
|
| 236 | - |
|
| 237 | - $plugin['class']->__execute($this, $update); |
|
| 238 | - if ($this->active_spreader === false) break; |
|
| 239 | - } |
|
| 240 | - |
|
| 241 | - $this->active_spreader = false; |
|
| 242 | - } |
|
| 243 | - |
|
| 244 | - /** |
|
| 245 | - * Stop the spreader process |
|
| 246 | - * |
|
| 247 | - * @return void |
|
| 248 | - */ |
|
| 249 | - public function stop(): void { |
|
| 250 | - $this->active_spreader = false; |
|
| 251 | - } |
|
| 235 | + } |
|
| 236 | + |
|
| 237 | + $plugin['class']->__execute($this, $update); |
|
| 238 | + if ($this->active_spreader === false) break; |
|
| 239 | + } |
|
| 240 | + |
|
| 241 | + $this->active_spreader = false; |
|
| 242 | + } |
|
| 243 | + |
|
| 244 | + /** |
|
| 245 | + * Stop the spreader process |
|
| 246 | + * |
|
| 247 | + * @return void |
|
| 248 | + */ |
|
| 249 | + public function stop(): void { |
|
| 250 | + $this->active_spreader = false; |
|
| 251 | + } |
|
| 252 | 252 | |
| 253 | 253 | } |
| 254 | 254 | \ No newline at end of file |
@@ -235,7 +235,9 @@ |
||
| 235 | 235 | } |
| 236 | 236 | |
| 237 | 237 | $plugin['class']->__execute($this, $update); |
| 238 | - if ($this->active_spreader === false) break; |
|
| 238 | + if ($this->active_spreader === false) { |
|
| 239 | + break; |
|
| 240 | + } |
|
| 239 | 241 | } |
| 240 | 242 | |
| 241 | 243 | $this->active_spreader = false; |
@@ -16,98 +16,98 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | trait PluginTrait { |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * @var UpdateHandler |
|
| 21 | - */ |
|
| 22 | - protected UpdateHandler $hook; |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * This property is used to kill the plugin when you yield a Response object. |
|
| 26 | - * |
|
| 27 | - * @var bool |
|
| 28 | - */ |
|
| 29 | - protected bool $KILL_ON_YIELD = true; |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * @var \Generator |
|
| 33 | - */ |
|
| 34 | - private \Generator $returns; |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * Execute the plugin. |
|
| 38 | - * |
|
| 39 | - * @param UpdateHandler $receiver |
|
| 40 | - * @param Update $update |
|
| 41 | - * @return void |
|
| 42 | - */ |
|
| 43 | - public function __execute(UpdateHandler $receiver, Update $update): void { |
|
| 44 | - $this->hook = $receiver; |
|
| 45 | - |
|
| 46 | - if (method_exists($this, '__process')) { |
|
| 47 | - $this->__process($update); |
|
| 48 | - } |
|
| 49 | - |
|
| 50 | - if (method_exists($this, 'onReceivedUpdate')) { |
|
| 51 | - $return = $this->onUpdate($update); |
|
| 52 | - $this->__checkExit($return); |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - $type = $this->__identify($update); |
|
| 56 | - if (method_exists($this, ($method = 'on' . $type)) && $type !== null) { |
|
| 57 | - $this->__checkExit($this->__callEvent($method, $update)); |
|
| 58 | - } |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * Check for the exit of the plugin. |
|
| 63 | - * |
|
| 64 | - * @param \Generator $return |
|
| 65 | - * @return void |
|
| 66 | - */ |
|
| 67 | - private function __checkExit(\Generator $return): void { |
|
| 68 | - if ($return->valid()) { |
|
| 69 | - if ($return->current() !== null && $this->KILL_ON_YIELD === true) { |
|
| 19 | + /** |
|
| 20 | + * @var UpdateHandler |
|
| 21 | + */ |
|
| 22 | + protected UpdateHandler $hook; |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * This property is used to kill the plugin when you yield a Response object. |
|
| 26 | + * |
|
| 27 | + * @var bool |
|
| 28 | + */ |
|
| 29 | + protected bool $KILL_ON_YIELD = true; |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * @var \Generator |
|
| 33 | + */ |
|
| 34 | + private \Generator $returns; |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * Execute the plugin. |
|
| 38 | + * |
|
| 39 | + * @param UpdateHandler $receiver |
|
| 40 | + * @param Update $update |
|
| 41 | + * @return void |
|
| 42 | + */ |
|
| 43 | + public function __execute(UpdateHandler $receiver, Update $update): void { |
|
| 44 | + $this->hook = $receiver; |
|
| 45 | + |
|
| 46 | + if (method_exists($this, '__process')) { |
|
| 47 | + $this->__process($update); |
|
| 48 | + } |
|
| 49 | + |
|
| 50 | + if (method_exists($this, 'onReceivedUpdate')) { |
|
| 51 | + $return = $this->onUpdate($update); |
|
| 52 | + $this->__checkExit($return); |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + $type = $this->__identify($update); |
|
| 56 | + if (method_exists($this, ($method = 'on' . $type)) && $type !== null) { |
|
| 57 | + $this->__checkExit($this->__callEvent($method, $update)); |
|
| 58 | + } |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * Check for the exit of the plugin. |
|
| 63 | + * |
|
| 64 | + * @param \Generator $return |
|
| 65 | + * @return void |
|
| 66 | + */ |
|
| 67 | + private function __checkExit(\Generator $return): void { |
|
| 68 | + if ($return->valid()) { |
|
| 69 | + if ($return->current() !== null && $this->KILL_ON_YIELD === true) { |
|
| 70 | 70 | if ($return->current() instanceof Response) { |
| 71 | - $this->stop(); |
|
| 71 | + $this->stop(); |
|
| 72 | 72 | } |
| 73 | - } |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - if ($return->valid()) { |
|
| 77 | - $return->next(); |
|
| 78 | - $this->__checkExit($return); |
|
| 79 | - } |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * Identify the update type. e.g. Message, EditedMessage, etc. |
|
| 84 | - * |
|
| 85 | - * @param Update $update |
|
| 86 | - * @return string|null |
|
| 87 | - */ |
|
| 88 | - private function __identify(Update $update): ?string { |
|
| 89 | - $type = $update->getUpdateType(); |
|
| 90 | - |
|
| 91 | - if ($type === null) { |
|
| 92 | - return null; |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - return str_replace('_', '', ucwords($type, '_')); |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * Pass data to the method. |
|
| 100 | - * |
|
| 101 | - * @param string $method The method name. |
|
| 102 | - * @param Update $update The update object. |
|
| 103 | - * @return \Generator |
|
| 104 | - */ |
|
| 105 | - private function __callEvent(string $method, Update $update): \Generator { |
|
| 106 | - $upperName = 'get' . ucfirst(substr($method, 2)); |
|
| 107 | - return match ($method) { |
|
| 108 | - 'onWebAppData' => $this->onWebAppData($update->getWebAppData()), |
|
| 109 | - default => $this->$method($update->getUpdateId(), $update->$upperName()), |
|
| 110 | - }; |
|
| 111 | - } |
|
| 73 | + } |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + if ($return->valid()) { |
|
| 77 | + $return->next(); |
|
| 78 | + $this->__checkExit($return); |
|
| 79 | + } |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * Identify the update type. e.g. Message, EditedMessage, etc. |
|
| 84 | + * |
|
| 85 | + * @param Update $update |
|
| 86 | + * @return string|null |
|
| 87 | + */ |
|
| 88 | + private function __identify(Update $update): ?string { |
|
| 89 | + $type = $update->getUpdateType(); |
|
| 90 | + |
|
| 91 | + if ($type === null) { |
|
| 92 | + return null; |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + return str_replace('_', '', ucwords($type, '_')); |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * Pass data to the method. |
|
| 100 | + * |
|
| 101 | + * @param string $method The method name. |
|
| 102 | + * @param Update $update The update object. |
|
| 103 | + * @return \Generator |
|
| 104 | + */ |
|
| 105 | + private function __callEvent(string $method, Update $update): \Generator { |
|
| 106 | + $upperName = 'get' . ucfirst(substr($method, 2)); |
|
| 107 | + return match ($method) { |
|
| 108 | + 'onWebAppData' => $this->onWebAppData($update->getWebAppData()), |
|
| 109 | + default => $this->$method($update->getUpdateId(), $update->$upperName()), |
|
| 110 | + }; |
|
| 111 | + } |
|
| 112 | 112 | |
| 113 | 113 | } |
| 114 | 114 | \ No newline at end of file |
@@ -11,164 +11,164 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | abstract class Entity { |
| 13 | 13 | |
| 14 | - /** |
|
| 15 | - * @var array The raw data passed to this entity |
|
| 16 | - */ |
|
| 17 | - protected array $raw_data = []; |
|
| 18 | - |
|
| 19 | - /** |
|
| 20 | - * Entity constructor. |
|
| 21 | - * |
|
| 22 | - * @param ?array $data The raw data passed to this entity |
|
| 23 | - */ |
|
| 24 | - public function __construct(?array $data) { |
|
| 25 | - if (!empty($data)) { |
|
| 26 | - $this->assignMemberVariables(($this->raw_data = $data)); |
|
| 27 | - $this->validate(); |
|
| 28 | - } |
|
| 29 | - } |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * Helper to set member variables |
|
| 33 | - * |
|
| 34 | - * @param array $data |
|
| 35 | - * @return void |
|
| 36 | - */ |
|
| 37 | - protected function assignMemberVariables(array $data): void { |
|
| 38 | - foreach ($data as $key => $value) { |
|
| 39 | - $this->$key = $value; |
|
| 40 | - } |
|
| 41 | - } |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * Perform any special entity validation |
|
| 45 | - * |
|
| 46 | - * @return void |
|
| 47 | - */ |
|
| 48 | - protected function validate(): void { |
|
| 49 | - // Do nothing by default |
|
| 50 | - } |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * Escape markdown (v1) special characters |
|
| 54 | - * |
|
| 55 | - * @see https://core.telegram.org/bots/api#markdown-style |
|
| 56 | - * |
|
| 57 | - * @param string $string |
|
| 58 | - * |
|
| 59 | - * @return string |
|
| 60 | - */ |
|
| 61 | - public static function escapeMarkdown(string $string): string { |
|
| 62 | - return str_replace( |
|
| 63 | - ['[', '`', '*', '_',], |
|
| 64 | - ['\[', '\`', '\*', '\_',], |
|
| 65 | - $string |
|
| 66 | - ); |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * Escape markdown (v2) special characters |
|
| 71 | - * |
|
| 72 | - * @see https://core.telegram.org/bots/api#markdownv2-style |
|
| 73 | - * |
|
| 74 | - * @param string $string |
|
| 75 | - * |
|
| 76 | - * @return string |
|
| 77 | - */ |
|
| 78 | - public static function escapeMarkdownV2(string $string): string { |
|
| 79 | - return str_replace( |
|
| 80 | - ['_', '*', '[', ']', '(', ')', '~', '`', '>', '#', '+', '-', '=', '|', '{', '}', '.', '!'], |
|
| 81 | - ['\_', '\*', '\[', '\]', '\(', '\)', '\~', '\`', '\>', '\#', '\+', '\-', '\=', '\|', '\{', '\}', '\.', '\!'], |
|
| 82 | - $string |
|
| 83 | - ); |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * Get the raw data passed to this entity |
|
| 88 | - * |
|
| 89 | - * @param bool $associated |
|
| 90 | - * @return array|string |
|
| 91 | - */ |
|
| 92 | - public function getRawData(bool $associated = true): array|string { |
|
| 93 | - return $associated ? $this->raw_data : json_encode($this->raw_data); |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * Get a property from the current Entity |
|
| 98 | - * |
|
| 99 | - * @param string $property |
|
| 100 | - * @param mixed $default |
|
| 101 | - * |
|
| 102 | - * @return mixed |
|
| 103 | - */ |
|
| 104 | - public function getProperty(string $property, mixed $default = null): mixed { |
|
| 105 | - return $this->raw_data[$property] ?? $default; |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - /** |
|
| 109 | - * Get the list of the properties that are themselves Entities |
|
| 110 | - * |
|
| 111 | - * @return array |
|
| 112 | - */ |
|
| 113 | - protected function subEntities(): array { |
|
| 114 | - return []; |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - /** |
|
| 118 | - * Get the key value from the raw data passed to this entity |
|
| 119 | - * |
|
| 120 | - * @param string $key |
|
| 121 | - * @return mixed The value can be {Entity, array, string, int, float, bool, null} |
|
| 122 | - */ |
|
| 123 | - public function get(string $key): mixed { |
|
| 124 | - $property = $this->getProperty($key); |
|
| 125 | - $sub_entities = $this->subEntities() ?? []; |
|
| 126 | - |
|
| 127 | - if (isset($sub_entities[$key])) { |
|
| 128 | - $class_name = $sub_entities[$key]; |
|
| 129 | - return Factory::resolveEntityClass($class_name, $property); |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - return $property ?? null; |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - /** |
|
| 136 | - * Rewrite the entity values with the new data |
|
| 137 | - * |
|
| 138 | - * @param string $key |
|
| 139 | - * @param mixed $value |
|
| 140 | - * @return void |
|
| 141 | - */ |
|
| 142 | - public function set(string $key, mixed $value): void { |
|
| 143 | - $this->raw_data[$key] = $value; |
|
| 144 | - |
|
| 145 | - if (property_exists($this, $key)) { |
|
| 146 | - $this->{$key} = $value; |
|
| 147 | - } |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - /** |
|
| 151 | - * @param string $name The name of the property |
|
| 152 | - * @param array $arguments The arguments passed to the method |
|
| 153 | - * @return mixed |
|
| 154 | - */ |
|
| 155 | - public function __call(string $name, array $arguments): mixed { |
|
| 156 | - if (method_exists($this, $name)) { |
|
| 157 | - return $this->{$name}(...$arguments); |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - if (str_starts_with($name, 'get')) { |
|
| 161 | - $property_name = strtolower(ltrim(preg_replace('/[A-Z]/', '_$0', substr($name, 3)), '_')); |
|
| 162 | - return $this->get($property_name); |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - if (str_starts_with($name, 'set')) { |
|
| 166 | - $property_name = strtolower(ltrim(preg_replace('/[A-Z]/', '_$0', substr($name, 3)), '_')); |
|
| 167 | - $this->set($property_name, $arguments[0]); |
|
| 168 | - return $this; |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - throw new \BadMethodCallException(`Method ${name} does not exist`); |
|
| 172 | - } |
|
| 14 | + /** |
|
| 15 | + * @var array The raw data passed to this entity |
|
| 16 | + */ |
|
| 17 | + protected array $raw_data = []; |
|
| 18 | + |
|
| 19 | + /** |
|
| 20 | + * Entity constructor. |
|
| 21 | + * |
|
| 22 | + * @param ?array $data The raw data passed to this entity |
|
| 23 | + */ |
|
| 24 | + public function __construct(?array $data) { |
|
| 25 | + if (!empty($data)) { |
|
| 26 | + $this->assignMemberVariables(($this->raw_data = $data)); |
|
| 27 | + $this->validate(); |
|
| 28 | + } |
|
| 29 | + } |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * Helper to set member variables |
|
| 33 | + * |
|
| 34 | + * @param array $data |
|
| 35 | + * @return void |
|
| 36 | + */ |
|
| 37 | + protected function assignMemberVariables(array $data): void { |
|
| 38 | + foreach ($data as $key => $value) { |
|
| 39 | + $this->$key = $value; |
|
| 40 | + } |
|
| 41 | + } |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * Perform any special entity validation |
|
| 45 | + * |
|
| 46 | + * @return void |
|
| 47 | + */ |
|
| 48 | + protected function validate(): void { |
|
| 49 | + // Do nothing by default |
|
| 50 | + } |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * Escape markdown (v1) special characters |
|
| 54 | + * |
|
| 55 | + * @see https://core.telegram.org/bots/api#markdown-style |
|
| 56 | + * |
|
| 57 | + * @param string $string |
|
| 58 | + * |
|
| 59 | + * @return string |
|
| 60 | + */ |
|
| 61 | + public static function escapeMarkdown(string $string): string { |
|
| 62 | + return str_replace( |
|
| 63 | + ['[', '`', '*', '_',], |
|
| 64 | + ['\[', '\`', '\*', '\_',], |
|
| 65 | + $string |
|
| 66 | + ); |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * Escape markdown (v2) special characters |
|
| 71 | + * |
|
| 72 | + * @see https://core.telegram.org/bots/api#markdownv2-style |
|
| 73 | + * |
|
| 74 | + * @param string $string |
|
| 75 | + * |
|
| 76 | + * @return string |
|
| 77 | + */ |
|
| 78 | + public static function escapeMarkdownV2(string $string): string { |
|
| 79 | + return str_replace( |
|
| 80 | + ['_', '*', '[', ']', '(', ')', '~', '`', '>', '#', '+', '-', '=', '|', '{', '}', '.', '!'], |
|
| 81 | + ['\_', '\*', '\[', '\]', '\(', '\)', '\~', '\`', '\>', '\#', '\+', '\-', '\=', '\|', '\{', '\}', '\.', '\!'], |
|
| 82 | + $string |
|
| 83 | + ); |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * Get the raw data passed to this entity |
|
| 88 | + * |
|
| 89 | + * @param bool $associated |
|
| 90 | + * @return array|string |
|
| 91 | + */ |
|
| 92 | + public function getRawData(bool $associated = true): array|string { |
|
| 93 | + return $associated ? $this->raw_data : json_encode($this->raw_data); |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * Get a property from the current Entity |
|
| 98 | + * |
|
| 99 | + * @param string $property |
|
| 100 | + * @param mixed $default |
|
| 101 | + * |
|
| 102 | + * @return mixed |
|
| 103 | + */ |
|
| 104 | + public function getProperty(string $property, mixed $default = null): mixed { |
|
| 105 | + return $this->raw_data[$property] ?? $default; |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + /** |
|
| 109 | + * Get the list of the properties that are themselves Entities |
|
| 110 | + * |
|
| 111 | + * @return array |
|
| 112 | + */ |
|
| 113 | + protected function subEntities(): array { |
|
| 114 | + return []; |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + /** |
|
| 118 | + * Get the key value from the raw data passed to this entity |
|
| 119 | + * |
|
| 120 | + * @param string $key |
|
| 121 | + * @return mixed The value can be {Entity, array, string, int, float, bool, null} |
|
| 122 | + */ |
|
| 123 | + public function get(string $key): mixed { |
|
| 124 | + $property = $this->getProperty($key); |
|
| 125 | + $sub_entities = $this->subEntities() ?? []; |
|
| 126 | + |
|
| 127 | + if (isset($sub_entities[$key])) { |
|
| 128 | + $class_name = $sub_entities[$key]; |
|
| 129 | + return Factory::resolveEntityClass($class_name, $property); |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + return $property ?? null; |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + /** |
|
| 136 | + * Rewrite the entity values with the new data |
|
| 137 | + * |
|
| 138 | + * @param string $key |
|
| 139 | + * @param mixed $value |
|
| 140 | + * @return void |
|
| 141 | + */ |
|
| 142 | + public function set(string $key, mixed $value): void { |
|
| 143 | + $this->raw_data[$key] = $value; |
|
| 144 | + |
|
| 145 | + if (property_exists($this, $key)) { |
|
| 146 | + $this->{$key} = $value; |
|
| 147 | + } |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + /** |
|
| 151 | + * @param string $name The name of the property |
|
| 152 | + * @param array $arguments The arguments passed to the method |
|
| 153 | + * @return mixed |
|
| 154 | + */ |
|
| 155 | + public function __call(string $name, array $arguments): mixed { |
|
| 156 | + if (method_exists($this, $name)) { |
|
| 157 | + return $this->{$name}(...$arguments); |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + if (str_starts_with($name, 'get')) { |
|
| 161 | + $property_name = strtolower(ltrim(preg_replace('/[A-Z]/', '_$0', substr($name, 3)), '_')); |
|
| 162 | + return $this->get($property_name); |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + if (str_starts_with($name, 'set')) { |
|
| 166 | + $property_name = strtolower(ltrim(preg_replace('/[A-Z]/', '_$0', substr($name, 3)), '_')); |
|
| 167 | + $this->set($property_name, $arguments[0]); |
|
| 168 | + return $this; |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + throw new \BadMethodCallException(`Method ${name} does not exist`); |
|
| 172 | + } |
|
| 173 | 173 | |
| 174 | 174 | } |
| 175 | 175 | \ No newline at end of file |
@@ -60,8 +60,8 @@ |
||
| 60 | 60 | */ |
| 61 | 61 | public static function escapeMarkdown(string $string): string { |
| 62 | 62 | return str_replace( |
| 63 | - ['[', '`', '*', '_',], |
|
| 64 | - ['\[', '\`', '\*', '\_',], |
|
| 63 | + ['[', '`', '*', '_', ], |
|
| 64 | + ['\[', '\`', '\*', '\_', ], |
|
| 65 | 65 | $string |
| 66 | 66 | ); |
| 67 | 67 | } |