|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace DanySpin97\PhpBotFramework; |
|
4
|
|
|
|
|
5
|
|
|
define("DELIMITER", ':::::::::::::::::::::::::::::::::::::: |
|
6
|
|
|
'); |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* \class Utility |
|
10
|
|
|
* \brief Contains static help methods. |
|
11
|
|
|
*/ |
|
12
|
|
|
class Utility { |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* \addtogroup Utility-methods Utility methods |
|
16
|
|
|
* \brief Helper methods. |
|
17
|
|
|
* @{ |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* \brief Get hashtag contained in a string. |
|
22
|
|
|
* \details Check hashtags in a string using regex. |
|
23
|
|
|
* All valid hashtags will be returned in an array. |
|
24
|
|
|
* [Credis to trante](http://stackoverflow.com/questions/3060601/retrieve-all-hashtags-from-a-tweet-in-a-php-function). |
|
25
|
|
|
* @param $string The string to check for hashtags. |
|
26
|
|
|
* @return An array of valid hashtags, can be empty. |
|
27
|
|
|
*/ |
|
28
|
|
|
static public function getHashtags(string $string) : array { |
|
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
// Use regex to check |
|
31
|
|
|
if (preg_match_all("/(#\w+)/u", $string, $matches) != 0) { |
|
32
|
|
|
|
|
33
|
|
|
$hashtagsArray = array_count_values($matches[0]); |
|
34
|
|
|
|
|
35
|
|
|
$hashtags = array_keys($hashtagsArray); |
|
36
|
|
|
|
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
// Return an array of hashtags |
|
40
|
|
|
return $hashtags ?? []; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* \brief Remove html formattation from telegram usernames in string. |
|
45
|
|
|
* \details Remove the $modificator html formattation from a message containing telegram username, to let the user click them. |
|
46
|
|
|
* @param $string to parse. |
|
47
|
|
|
* @param $tag Formattation tag to remove. |
|
48
|
|
|
* @return The string, modified if there are usernames. Otherwise $string. |
|
49
|
|
|
*/ |
|
50
|
|
|
static public function removeUsernameFormattation(string $string, string $tag) : string { |
|
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
// Check if there are usernames in string using regex |
|
53
|
|
|
if (preg_match_all('/(@\w+)/u', $string, $matches) != 0) { |
|
54
|
|
|
|
|
55
|
|
|
$usernamesArray = array_count_values($matches[0]); |
|
56
|
|
|
|
|
57
|
|
|
$usernames = array_keys($usernamesArray); |
|
58
|
|
|
|
|
59
|
|
|
// Count how many username we've got |
|
60
|
|
|
$count = count($usernames); |
|
61
|
|
|
|
|
62
|
|
|
// Delimitator to make the formattation start |
|
63
|
|
|
$delimitator_start = '<' . $tag . '>'; |
|
64
|
|
|
|
|
65
|
|
|
// and to make it end |
|
66
|
|
|
$delimitator_end = '</' . $tag . '>'; |
|
67
|
|
|
|
|
68
|
|
|
// For each username |
|
69
|
|
|
for($i = 0; $i !== $count; $i++) { |
|
70
|
|
|
|
|
71
|
|
|
// Put the Delimitator_end before the username and the start one after it |
|
72
|
|
|
$string = str_replace($usernames[$i], $delimitator_end . $usernames[$i] . $delimitator_start, $string); |
|
73
|
|
|
|
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
// Return the string, modified or not |
|
79
|
|
|
return $string; |
|
80
|
|
|
|
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
static public function paginateItems($items, int $index, $format_item, int $item_per_page = 3, $keyboard = null, $prefix = 'list', string $delimiter = DELIMITER) { |
|
|
|
|
|
|
84
|
|
|
|
|
85
|
|
|
// Calc the position of the first item to show |
|
86
|
|
|
$item_position = ($index - 1) * $item_per_page + 1; |
|
87
|
|
|
|
|
88
|
|
|
// Counter variable |
|
89
|
|
|
$cont = 1; |
|
90
|
|
|
|
|
91
|
|
|
// How many items did we display? |
|
92
|
|
|
$items_displayed = 0; |
|
93
|
|
|
|
|
94
|
|
|
// If keyboard is valid |
|
95
|
|
|
if (isset($keyboard)) { |
|
96
|
|
|
|
|
97
|
|
|
// Get how many items did the database return |
|
98
|
|
|
$items_number = $items->rowCount(); |
|
99
|
|
|
|
|
100
|
|
|
// Get how many complete pages there are |
|
101
|
|
|
$total_pages = intval($items_number / $item_per_page); |
|
102
|
|
|
|
|
103
|
|
|
// If there an incomplete page |
|
104
|
|
|
if (($items_number % $item_per_page) != 0) { |
|
105
|
|
|
|
|
106
|
|
|
$total_pages++; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
// Initialize keyboard with the list |
|
110
|
|
|
$keyboard->initializeCompositeListKeyboard($index, $total_pages, $prefix); |
|
111
|
|
|
|
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
// Initialize empty string |
|
115
|
|
|
$message = ''; |
|
116
|
|
|
|
|
117
|
|
|
// Iterate over all results |
|
118
|
|
|
while ($item = $items->fetch()) { |
|
119
|
|
|
|
|
120
|
|
|
// If we have to display the first item of the page and we found the item to show (using the position |
|
121
|
|
|
// calculated before) |
|
122
|
|
|
if ($items_displayed === 0 && $cont === $item_position) { |
|
123
|
|
|
|
|
124
|
|
|
// Format the item using closure |
|
125
|
|
|
$message .= $format_item($item, $keyboard); |
|
126
|
|
|
|
|
127
|
|
|
// We displayed an item |
|
128
|
|
|
$items_displayed++; |
|
129
|
|
|
|
|
130
|
|
|
// If we displayed at least an item but still not how much we want |
|
131
|
|
|
} elseif ($items_displayed > 0 && $items_displayed < $item_per_page) { |
|
132
|
|
|
|
|
133
|
|
|
// Add delimiter to the message |
|
134
|
|
|
$message .= DELIMITER; |
|
135
|
|
|
|
|
136
|
|
|
// Format the item using closure |
|
137
|
|
|
$message .= $format_item($item, $keyboard); |
|
138
|
|
|
|
|
139
|
|
|
// We displayed an item |
|
140
|
|
|
$items_displayed++; |
|
141
|
|
|
|
|
142
|
|
|
// If we displayed all the item we wanted |
|
143
|
|
|
} elseif ($items_displayed === $item_per_page) { |
|
144
|
|
|
|
|
145
|
|
|
// Exit the cycle |
|
146
|
|
|
break; |
|
147
|
|
|
|
|
148
|
|
|
// We are just iterating over an unwanted result |
|
149
|
|
|
} else { |
|
150
|
|
|
|
|
151
|
|
|
$cont++; |
|
152
|
|
|
|
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
// Return the created string |
|
158
|
|
|
return $message; |
|
159
|
|
|
|
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
|