From 32567055a6ed2a771351b526dc3fdb19352a4ded Mon Sep 17 00:00:00 2001 From: LordDeveloper Date: Tue, 16 Aug 2022 02:18:33 +0430 Subject: [PATCH 1/6] [update] Improved and renamed to keyboard --- src/functions.php | 25 ++++++------------------- 1 file changed, 6 insertions(+), 19 deletions(-) diff --git a/src/functions.php b/src/functions.php index 74a38eb..3020e48 100644 --- a/src/functions.php +++ b/src/functions.php @@ -6,7 +6,7 @@ use Amp\Promise; use ArrayAccess; use Botify\Exceptions\RetryException; -use Botify\Utils\Button; +use Botify\Utils\ReplyMarkup; use Botify\Utils\Collection; use Botify\Utils\Config; use Botify\Utils\Dotty; @@ -508,28 +508,15 @@ function array_every(array $array, ?callable $fn = null): bool } } -if (!function_exists('Botify\\button')) { +if (!function_exists('Botify\\keyboard')) { /** - * @param $id + * @param null $key * @param mixed ...$args * @return mixed */ - function button($id = null, ...$args): mixed - { - static $keyboards = null; - $keyboards ??= require_once base_path('utils/keyboards.php'); - $json = $args['json'] ?? true; - $options = $args['options'] ?? []; - $default = $args['default'] ?? null; - unset($args['json'], $args['options'], $args['default']); - - if (isset($args['remove']) && $args['remove'] === true) { - return Button::remove(); - } elseif (is_array($value = value(data_get($keyboards, $id, $default), ... $args))) { - return Button::make($value, $options, $json); - } - - return $default; + function keyboard($key = null, ...$args): mixed + { + return ReplyMarkup::generate($key, ... $args); } } From 4673c19a201dbedf9bd6f4504744e1a32d727867 Mon Sep 17 00:00:00 2001 From: LordDeveloper Date: Tue, 16 Aug 2022 02:19:05 +0430 Subject: [PATCH 2/6] [updated] Updated .gitignore --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 723ef36..331c58f 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -.idea \ No newline at end of file +.idea +vendor \ No newline at end of file From d5891b105578f12a0348d05b4ea0e4369fd86543 Mon Sep 17 00:00:00 2001 From: LordDeveloper Date: Tue, 16 Aug 2022 02:19:31 +0430 Subject: [PATCH 3/6] [updated] Renamed to ReplyMarkup --- src/Methods/MethodsFactory.php | 4 +-- src/Utils/{Button.php => ReplyMarkup.php} | 31 +++++++++++++++++++++-- 2 files changed, 31 insertions(+), 4 deletions(-) rename src/Utils/{Button.php => ReplyMarkup.php} (72%) diff --git a/src/Methods/MethodsFactory.php b/src/Methods/MethodsFactory.php index 4cd4813..92366c8 100644 --- a/src/Methods/MethodsFactory.php +++ b/src/Methods/MethodsFactory.php @@ -8,7 +8,7 @@ use Botify\Request\Client; use Botify\TelegramAPI; use Botify\Types\Map; -use Botify\Utils\Button; +use Botify\Utils\ReplyMarkup; use Botify\Utils\FallbackResponse; use Botify\Utils\Logger\Logger; use Exception; @@ -218,7 +218,7 @@ private function bindAttributes(&$attributes) $replyMarkup = &$attributes['reply_markup']; if (is_array($replyMarkup)) { - $replyMarkup = Button::make($replyMarkup); + $replyMarkup = ReplyMarkup::make($replyMarkup); } } diff --git a/src/Utils/Button.php b/src/Utils/ReplyMarkup.php similarity index 72% rename from src/Utils/Button.php rename to src/Utils/ReplyMarkup.php index 89fc07a..91aee37 100644 --- a/src/Utils/Button.php +++ b/src/Utils/ReplyMarkup.php @@ -3,9 +3,14 @@ namespace Botify\Utils; use function Botify\array_some; +use function Botify\config; +use function Botify\data_get; +use function Botify\value; -class Button +class ReplyMarkup { + private static array $keyboards = []; + /** * @var array */ @@ -31,7 +36,7 @@ public function __construct(array $options = []) * @param bool $json * @return mixed */ - public static function make($rows, array $options = [], $json = true): mixed + public static function make($rows, array $options = [], bool $json = true): mixed { $keyboard = new static($options); @@ -109,4 +114,26 @@ public static function remove(): string 'remove_keyboard' => true, ]); } + + public static function generate(?string $key = null, ...$args) + { + static::$keyboards ??= require_once config('telegram.keyboards_path', function () { + throw new \Exception('You must set keyboards_path key in config/telegram.php'); + }); + + if (isset($args['remove']) && $args['remove'] === true) { + return static::remove(); + } + + $json = $args['json'] ?? true; + $options = $args['options'] ?? []; + $default = $args['default'] ?? null; + unset($args['json'], $args['options'], $args['default']); + + if (is_array($value = value(data_get(static::$keyboards, $key, $default), ... $args))) { + return ReplyMarkup::make($value, $options, $json); + } + + return $default; + } } \ No newline at end of file From cdd6972ce245f23aa7d81de92f7221259268250c Mon Sep 17 00:00:00 2001 From: LordDeveloper Date: Tue, 16 Aug 2022 02:20:17 +0430 Subject: [PATCH 4/6] [updated] Removed file_get_contents and file_put_contents --- src/functions.php | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/src/functions.php b/src/functions.php index 3020e48..4044786 100644 --- a/src/functions.php +++ b/src/functions.php @@ -652,18 +652,4 @@ function array_sole(array $array, callable $fn): mixed return false; } -} - -if (!function_exists('Botify\\file_get_contents')) { - function file_get_contents(string $filename): Promise - { - return read($filename); - } -} - -if (!function_exists('Botify\\file_put_contents')) { - function file_put_contents(string $filename, string $data): Promise - { - return write($filename, $data); - } } \ No newline at end of file From 88428ed85c0c3d2c19841fb56ccf6e555a43980d Mon Sep 17 00:00:00 2001 From: LordDeveloper Date: Tue, 16 Aug 2022 02:24:55 +0430 Subject: [PATCH 5/6] [updated] Removed repeat and arepeat functions --- src/functions.php | 38 -------------------------------------- 1 file changed, 38 deletions(-) diff --git a/src/functions.php b/src/functions.php index 4044786..690e136 100644 --- a/src/functions.php +++ b/src/functions.php @@ -396,44 +396,6 @@ function config($id = null, $default = null) } } -if (!function_exists('Botify\\repeat')) { - /** - * Repeat a code n times - * @param int $times - * @param callable $callback - * @param array $iterable - * @param ...$args - * @return array - */ - function repeat(int $times, callable $callback, array $iterable = [], ...$args): array - { - $returns = []; - - $iterable = array_pad($iterable, $times, '*'); - - foreach ($iterable as $index => $item) { - $returns[] = $callback($item, $index, ... $args); - } - return $returns; - } -} - -if (!function_exists('Botify\\arepeat')) { - /** - * Asynchronous version of repeat - * - * @param int $times - * @param callable $callback - * @param array $iterable - * @param mixed ...$args - * @return Promise - */ - function arepeat(int $times, callable $callback, array $iterable = [], ...$args): Promise - { - return gather(repeat($times, $callback, $iterable, ... $args)); - } -} - if (!function_exists('Botify\\abs_path')) { /** * Get absolute path of a path From 10fc901d0ea7def95192b8700704ff5c42da673e Mon Sep 17 00:00:00 2001 From: LordDeveloper Date: Tue, 16 Aug 2022 02:25:53 +0430 Subject: [PATCH 6/6] [formatted] Improved import functions --- src/Utils/ReplyMarkup.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/Utils/ReplyMarkup.php b/src/Utils/ReplyMarkup.php index 91aee37..3b06fc0 100644 --- a/src/Utils/ReplyMarkup.php +++ b/src/Utils/ReplyMarkup.php @@ -2,10 +2,7 @@ namespace Botify\Utils; -use function Botify\array_some; -use function Botify\config; -use function Botify\data_get; -use function Botify\value; +use function Botify\{array_some, config, data_get, value}; class ReplyMarkup {