Skip to content

Commit 6538ddd

Browse files
committed
Redesign config setup array and add array-dot notation for retrieving values.
1 parent 09e93c6 commit 6538ddd

File tree

4 files changed

+141
-66
lines changed

4 files changed

+141
-66
lines changed

src/BotManager.php

Lines changed: 47 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -241,23 +241,59 @@ private function handleOutput(string $output): self
241241
*/
242242
public function setBotExtras(): self
243243
{
244-
$telegram_extras = [
244+
$this->setBotExtrasTelegram();
245+
$this->setBotExtrasRequest();
246+
247+
return $this;
248+
}
249+
250+
/**
251+
* Set extra bot parameters for Telegram object.
252+
*
253+
* @return \NPM\TelegramBotManager\BotManager
254+
* @throws \Longman\TelegramBot\Exception\TelegramException
255+
*/
256+
protected function setBotExtrasTelegram(): self
257+
{
258+
$simple_extras = [
245259
'admins' => 'enableAdmins',
246260
'mysql' => 'enableMySql',
247-
'botan_token' => 'enableBotan',
248-
'commands_paths' => 'addCommandsPaths',
261+
'commands.paths' => 'addCommandsPaths',
249262
'custom_input' => 'setCustomInput',
250-
'download_path' => 'setDownloadPath',
251-
'upload_path' => 'setUploadPath',
263+
'paths.download' => 'setDownloadPath',
264+
'paths.upload' => 'setUploadPath',
252265
];
253-
// For telegram extras, just pass the single param value to the Telegram method.
254-
foreach ($telegram_extras as $param_key => $method) {
266+
// For simple telegram extras, just pass the single param value to the Telegram method.
267+
foreach ($simple_extras as $param_key => $method) {
255268
$param = $this->params->getBotParam($param_key);
256269
if (null !== $param) {
257270
$this->telegram->$method($param);
258271
}
259272
}
260273

274+
// Custom command configs.
275+
$command_configs = $this->params->getBotParam('commands.configs', []);
276+
foreach ($command_configs as $command => $config) {
277+
$this->telegram->setCommandConfig($command, $config);
278+
}
279+
280+
// Botan with options.
281+
if ($botan_token = $this->params->getBotParam('botan.token')) {
282+
$botan_options = $this->params->getBotParam('botan.options', []);
283+
$this->telegram->enableBotan($botan_token, $botan_options);
284+
}
285+
286+
return $this;
287+
}
288+
289+
/**
290+
* Set extra bot parameters for Request class.
291+
*
292+
* @return \NPM\TelegramBotManager\BotManager
293+
* @throws \Longman\TelegramBot\Exception\TelegramException
294+
*/
295+
protected function setBotExtrasRequest(): self
296+
{
261297
$request_extras = [
262298
// None at the moment...
263299
];
@@ -270,19 +306,10 @@ public function setBotExtras(): self
270306
}
271307

272308
// Special cases.
273-
$limiter = $this->params->getBotParam('limiter', []);
274-
if (is_array($limiter)) {
275-
Request::setLimiter(true, $limiter);
276-
} else {
277-
Request::setLimiter($limiter);
278-
}
279-
280-
$command_configs = $this->params->getBotParam('command_configs');
281-
if (is_array($command_configs)) {
282-
/** @var array $command_configs */
283-
foreach ($command_configs as $command => $config) {
284-
$this->telegram->setCommandConfig($command, $config);
285-
}
309+
$limiter_enabled = $this->params->getBotParam('limiter.enabled');
310+
if ($limiter_enabled !== null) {
311+
$limiter_options = $this->params->getBotParam('limiter.options', []);
312+
Request::setLimiter($limiter_enabled, $limiter_options);
286313
}
287314

288315
return $this;

src/Params.php

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,15 @@ class Params
4040
'validate_request',
4141
'valid_ips',
4242
'webhook',
43-
'certificate',
44-
'max_connections',
45-
'allowed_updates',
4643
'logging',
4744
'limiter',
4845
'admins',
4946
'mysql',
50-
'download_path',
51-
'upload_path',
52-
'commands_paths',
53-
'command_configs',
54-
'botan_token',
47+
'paths',
48+
'commands',
49+
'botan',
5550
'custom_input',
51+
'cron',
5652
];
5753

5854
/**
@@ -75,20 +71,30 @@ class Params
7571
* secret (string) Secret string to validate calls
7672
* validate_request (bool) Only allow webhook access from valid Telegram API IPs and defined valid_ips
7773
* valid_ips (array) Any IPs, besides Telegram API IPs, that are allowed to access the webhook
78-
* webhook (string) URI of the webhook
79-
* certificate (string) Path to the self-signed certificate
80-
* max_connections (int) Maximum allowed simultaneous HTTPS connections to the webhook
81-
* allowed_updates (array) List the types of updates you want your bot to receive
74+
* webhook (array)
75+
* - url (string) URI of the webhook
76+
* - certificate (string) Path to the self-signed certificate
77+
* - max_connections (int) Maximum allowed simultaneous HTTPS connections to the webhook
78+
* - allowed_updates (array) List the types of updates you want your bot to receive
8279
* logging (array) Array of logger files to set.
83-
* limiter (bool|array) Set limiter, as bool or options array.
80+
* limiter (array)
81+
* - enabled (bool) Set limiter.
82+
* - options (array) Limiter options.
8483
* admins (array) List of admins to enable.
8584
* mysql (array) MySQL credentials to use.
86-
* download_path (string) Custom download path to set.
87-
* upload_path (string) Custom upload path to set.
88-
* commands_paths (array) Custom commands paths to set.
89-
* command_configs (array) List of custom command configs.
90-
* botan_token (string) Botan token to enable botan.io support.
85+
* paths (array)
86+
* - download (string) Custom download path to set.
87+
* - upload (string) Custom upload path to set.
88+
* commands (array)
89+
* - paths (array) Custom commands paths to set.
90+
* - configs (array) List of custom command configs.
91+
* botan (array)
92+
* - token (string) Botan token to enable botan.io support.
93+
* - options (array) Botan options.
9194
* custom_input (string) Custom raw JSON string to use as input.
95+
* cron (array)
96+
* - groups (array) Groups of cron commands to run.
97+
* - default (array) Default group of cron commands.
9298
*
9399
* @param array $params All params to set the bot up with.
94100
*
@@ -165,7 +171,7 @@ private function validateAndSetScriptParams(): self
165171
}
166172

167173
/**
168-
* Get a specific bot param.
174+
* Get a specific bot param, allowing array-dot notation.
169175
*
170176
* @param string $param
171177
* @param mixed $default
@@ -174,7 +180,17 @@ private function validateAndSetScriptParams(): self
174180
*/
175181
public function getBotParam(string $param, $default = null)
176182
{
177-
return $this->bot_params[$param] ?? $default;
183+
$param_path = explode('.', $param);
184+
185+
$value = $this->bot_params[array_shift($param_path)] ?? null;
186+
foreach ($param_path as $sub_param_key) {
187+
$value = $value[$sub_param_key] ?? null;
188+
if (null === $value) {
189+
break;
190+
}
191+
}
192+
193+
return $value ?? $default;
178194
}
179195

180196
/**

tests/TelegramBotManager/Tests/BotManagerTest.php

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,15 @@ public static function setUpBeforeClass()
4747
public function testSetParameters()
4848
{
4949
$botManager = new BotManager(array_merge(ParamsTest::$demo_vital_params, [
50-
'admins' => [1], // valid
51-
'upload_path' => '/upload/path', // valid
52-
'paramX' => 'something' // invalid
50+
'admins' => [1], // valid
51+
'paths' => [ // valid
52+
'upload' => '/upload/path',
53+
],
54+
'paramX' => 'something' // invalid
5355
]));
5456
$params = $botManager->getParams();
5557
self::assertEquals([1], $params->getBotParam('admins'));
56-
self::assertEquals('/upload/path', $params->getBotParam('upload_path'));
58+
self::assertEquals('/upload/path', $params->getBotParam('paths.upload'));
5759
self::assertNull($params->getBotParam('paramX'));
5860
}
5961

@@ -336,24 +338,32 @@ public function testGetLoopInterval()
336338
public function testSetBotExtras()
337339
{
338340
$extras = [
339-
'limiter' => false,
340-
'admins' => [1, 2, 3],
341-
'download_path' => __DIR__ . '/Download',
342-
'upload_path' => __DIR__ . '/Upload',
343-
'command_configs' => [
344-
'weather' => ['owm_api_key' => 'owm_api_key_12345'],
341+
'limiter' => [
342+
'enabled' => false,
343+
],
344+
'admins' => [1, 2, 3],
345+
'paths' => [
346+
'download' => __DIR__ . '/Download',
347+
'upload' => __DIR__ . '/Upload',
348+
],
349+
'commands' => [
350+
'configs' => [
351+
'weather' => [
352+
'owm_api_key' => 'owm_api_key_12345',
353+
],
354+
],
345355
],
346356
];
347357
$botManager = new BotManager(array_merge(ParamsTest::$demo_vital_params, $extras));
348358

349359
$botManager->setBotExtras();
350360
$telegram = $botManager->getTelegram();
351361

352-
self::assertAttributeEquals($extras['limiter'], 'limiter_enabled', Request::class);
362+
self::assertAttributeEquals($extras['limiter']['enabled'], 'limiter_enabled', Request::class);
353363
self::assertEquals($extras['admins'], $telegram->getAdminList());
354-
self::assertEquals($extras['download_path'], $telegram->getDownloadPath());
355-
self::assertEquals($extras['upload_path'], $telegram->getUploadPath());
356-
self::assertEquals($extras['command_configs']['weather'], $telegram->getCommandConfig('weather'));
364+
self::assertEquals($extras['paths']['download'], $telegram->getDownloadPath());
365+
self::assertEquals($extras['paths']['upload'], $telegram->getUploadPath());
366+
self::assertEquals($extras['commands']['configs']['weather'], $telegram->getCommandConfig('weather'));
357367
}
358368

359369
public function testGetOutput()

tests/TelegramBotManager/Tests/ParamsTest.php

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,26 +28,41 @@ class ParamsTest extends \PHPUnit\Framework\TestCase
2828
*/
2929
public static $demo_extra_params = [
3030
'validate_request' => true,
31-
'webhook' => 'https://php.telegram.bot/manager.php',
32-
'certificate' => __DIR__ . '/server.crt',
33-
'max_connections' => 20,
34-
'allowed_updates' => ['message', 'edited_channel_post', 'callback_query'],
35-
'limiter' => false,
31+
'webhook' => [
32+
'url' => 'https://php.telegram.bot/manager.php',
33+
'certificate' => __DIR__ . '/server.crt',
34+
'max_connections' => 20,
35+
'allowed_updates' => ['message', 'edited_channel_post', 'callback_query'],
36+
],
37+
'limiter' => [
38+
'enabled' => false,
39+
'options' => [
40+
'interval' => 0.5,
41+
],
42+
],
3643
'admins' => [1],
3744
'mysql' => [
3845
'host' => '127.0.0.1',
3946
'user' => 'root',
4047
'password' => 'root',
4148
'database' => 'telegram_bot',
4249
],
43-
'download_path' => __DIR__ . '/Download',
44-
'upload_path' => __DIR__ . '/Upload',
45-
'commands_paths' => __DIR__ . '/CustomCommands',
46-
'command_configs' => [
47-
'weather' => ['owm_api_key' => 'owm_api_key_12345'],
48-
'sendtochannel' => ['your_channel' => '@my_channel'],
50+
'paths' => [
51+
'download' => __DIR__ . '/Download',
52+
'upload' => __DIR__ . '/Upload',
53+
],
54+
'commands' => [
55+
'paths' => [
56+
__DIR__ . '/CustomCommands',
57+
],
58+
'configs' => [
59+
'weather' => ['owm_api_key' => 'owm_api_key_12345'],
60+
'sendtochannel' => ['your_channel' => '@my_channel'],
61+
],
62+
],
63+
'botan' => [
64+
'token' => 'botan_12345',
4965
],
50-
'botan_token' => 'botan_12345',
5166
'custom_input' => '{"some":"raw", "json":"update"}',
5267
];
5368

@@ -148,6 +163,13 @@ public function testSetAndGetBotParams()
148163
self::assertEquals(self::$demo_extra_params[$extra_param_key], $params->getBotParam($extra_param_key));
149164
}
150165

166+
// Test default return values.
151167
self::assertNull($params->getBotParam('non-existent'));
168+
self::assertTrue($params->getBotParam('non-existent', true));
169+
self::assertSame('some-default', $params->getBotParam('non-existent', 'some-default'));
170+
171+
// Test array-dot notation.
172+
self::assertSame([__DIR__ . '/CustomCommands'], $params->getBotParam('commands.paths'));
173+
self::assertSame('owm_api_key_12345', $params->getBotParam('commands.configs.weather.owm_api_key'));
152174
}
153175
}

0 commit comments

Comments
 (0)