Passed
Push — main ( fce709...e7b3ac )
by Miaad
02:07
created

generator::inviteLink()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 3
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 4
rs 10
1
<?php
2
3
namespace BPT\tools;
4
5
use BPT\api\telegram;
6
use BPT\constants\fields;
7
use BPT\constants\loggerTypes;
8
use BPT\constants\pollType;
9
use BPT\exception\bptException;
10
use BPT\logger;
11
use BPT\tools;
12
use BPT\types\inlineKeyboardButton;
13
use BPT\types\inlineKeyboardMarkup;
14
use BPT\types\keyboardButton;
15
use BPT\types\keyboardButtonPollType;
16
use BPT\types\replyKeyboardMarkup;
17
use BPT\types\webAppInfo;
18
19
trait generator {
20
    /**
21
     * Generate random string
22
     *
23
     * e.g. => tools::randomString();
24
     *
25
     * e.g. => tools::randomString(16,'abcdefg');
26
     *
27
     * e.g. => tools::randomString(length: 16,characters: 'abcdefg');
28
     *
29
     * @param int    $length     length of generated string
30
     * @param string $characters string constructor characters
31
     *
32
     * @return string
33
     */
34
    public static function randomString (int $length = 16, string $characters = 'aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ'): string {
35
        $rand_string = '';
36
        $char_len = strlen($characters) - 1;
37
        for ($i = 0; $i < $length; $i ++) {
38
            $rand_string .= $characters[rand(0, $char_len)];
39
        }
40
        return $rand_string;
41
    }
42
43
    /**
44
     * create normal keyboard and inline keyboard easily
45
     *
46
     * you must set keyboard parameter(for normal keyboard) or inline parameter(for inline keyboard)
47
     *
48
     * if you set both , keyboard will be processed and inline will be ignored
49
     *
50
     *  
51
     *
52
     * con for request contact , loc for request location, web||URL for webapp, pull||POLLTYPE for poll
53
     *
54
     * e.g. => tools::easyKey([['button 1 in row 1','button 2 in row 1'],['button 1 in row 2'],['contact button in row 3||con'],['location button in row 4||loc']]);
55
     *
56
     *  
57
     *
58
     * e.g. => tools::easyKey(inline: [[['button 1 in row 1','this is callback button'],['button 2 in row 1','https://this-is-url-button.com']],[['demo button in row 2']]]);
59
     *
60
     * @param string[][] $keyboard array(as rows) of array(buttons) of string
61
     * @param array[][]  $inline   array(as rows) of array(buttons) of array(button data)
62
     *
63
     * @return inlineKeyboardMarkup|replyKeyboardMarkup replyKeyboardMarkup for keyboard and inlineKeyboardMarkup for inline
64
     * @throws bptException
65
     */
66
    public static function easyKey(array $keyboard = [], array $inline = []): inlineKeyboardMarkup|replyKeyboardMarkup {
67
        if (!empty($keyboard)) {
68
            $keyboard_object = new replyKeyboardMarkup();
69
            $keyboard_object->setResize_keyboard($keyboard['resize'] ?? true);
70
            if (isset($keyboard['one_time'])) {
71
                $keyboard_object->setOne_time_keyboard($keyboard['one_time']);
72
            }
73
            foreach ($keyboard as $row) {
74
                if (!is_array($row)) continue;
75
                $buttons = [];
76
                foreach ($row as $base_button) {
77
                    $button_info = explode('||', $base_button);
78
                    $button = new keyboardButton();
79
                    $button->setText($button_info[0] ?? $base_button);
80
                    if (count($button_info) > 1) {
81
                        if ($button_info[1] === 'con') {
82
                            $button->setRequest_contact(true);
83
                        }
84
                        elseif ($button_info[1] === 'loc') {
85
                            $button->setRequest_location(true);
86
                        }
87
                        elseif ($button_info[1] === 'poll') {
88
                            $type = $button_info[2] === pollType::QUIZ ? pollType::QUIZ : pollType::REGULAR;
89
                            $button->setRequest_poll((new keyboardButtonPollType())->setType($type));
90
                        }
91
                        elseif ($button_info[1] === 'web' && isset($button_info[2])) {
92
                            $url = $button_info[2];
93
                            $button->setWeb_app((new webAppInfo())->setUrl($url));
94
                        }
95
                    }
96
                    $buttons[] = $button;
97
                }
98
                $keyboard_object->setKeyboard([$buttons]);
99
            }
100
            return $keyboard_object;
101
        }
102
        elseif (!empty($inline)) {
103
            $keyboard_object = new inlineKeyboardMarkup();
104
            foreach ($inline as $row) {
105
                $buttons = [];
106
                foreach ($row as $button_info) {
107
                    $button = new inlineKeyboardButton();
108
                    if (isset($button_info[1])) {
109
                        if (filter_var($button_info[1], FILTER_VALIDATE_URL) && str_starts_with($button_info[1], 'http')) {
110
                            $button->setText($button_info[0])->setUrl($button_info[1]);
111
                        }
112
                        else {
113
                            $button->setText($button_info[0])->setCallback_data($button_info[1]);
114
                        }
115
                    }
116
                    else {
117
                        $button->setText($button_info[0])->setUrl('https://t.me/BPT_CH');
118
                    }
119
                }
120
                $keyboard_object->setInline_keyboard([$buttons]);
121
            }
122
            return $keyboard_object;
123
        }
124
        else {
125
            logger::write("tools::eKey function used\nkeyboard or inline parameter must be set",loggerTypes::ERROR);
126
            throw new bptException('ARGUMENT_NOT_FOUND_KEYBOARD_INLINE');
127
        }
128
    }
129
130
    /**
131
     * create invite link for user which use shortEncode method and can be handled by BPT database
132
     *
133
     * e.g. => tools::inviteLink(123456789,'Username_bot');
134
     *
135
     * e.g. => tools::inviteLink(123456789);
136
     *
137
     * @param int|null $user_id user id , default : catchFields(fields::USER_ID)
138
     * @param string|null  $bot_username bot username , default : telegram::getMe()->username
139
     *
140
     * @return string
141
     */
142
    public static function inviteLink (int $user_id = null, string $bot_username = null): string {
143
        if (empty($user_id)) $user_id = telegram::catchFields(fields::USER_ID);
144
        if (empty($bot_username)) $bot_username = telegram::getMe()->username;
145
        return 'https://t.me/' . str_replace('@', '', $bot_username) . '?start=ref_' . tools::shortEncode($user_id);
146
    }
147
}