1 | <?php |
||
25 | */ |
||
26 | |||
27 | // Delimitate items in one page. |
||
28 | ======= |
||
29 | use PhpBotFramework\Entities\InlineKeyboard as Keyboard; |
||
30 | |||
31 | >>>>>>> master |
||
32 | define("DELIMITER", "::::::::::::::::::::::::::::::::::::::\n"); |
||
33 | |||
34 | /* \class Paginator */ |
||
35 | class Paginator |
||
36 | { |
||
37 | /** |
||
38 | * \addtogroup Utility-classes Utility classes |
||
39 | * @{ |
||
40 | */ |
||
41 | |||
42 | /** |
||
43 | * \brief Paginate a number of items got as a result by a query. |
||
44 | * \details Take items to show in the page $index, delimiting in by $delimiter, and call the closure $format_item on each item paginated. |
||
45 | * Taking a select query result, take items that have to be shown on page of index $index (calculated with $item_per_page). |
||
46 | * @param mixed $items Result of a select query using pdo object. |
||
47 | * @param int $index Index of the page to show. |
||
48 | * @param PhpBotFramework\Entities\InlineKeyboard $keyboard Inline keyboard object to call PhpBotFramework\\Entities\\InlineKeyboard::addListKeyboard() on it for browsing results. |
||
49 | * @param closure $format_item A closure that take the item to paginate and the keyboard. You can use it to format the item and add some inline keyboard button for each item. |
||
50 | * @param string $prefix Prefix to pass to InlineKeyboard::addListKeyboard. |
||
51 | * @param string $delimiter A string that will be used to concatenate each item. |
||
52 | * @return string The string message with the items of page $index, with $delimiter between each of them. |
||
53 | * @see PhpBotFramework\\Entities\\InlineKeyboard |
||
54 | */ |
||
55 | public static function paginateItems( |
||
56 | $items, |
||
57 | int $index, |
||
58 | Keyboard &$keyboard, |
||
59 | callable $format_item, |
||
60 | int $item_per_page = 3, |
||
61 | string $prefix = 'list', |
||
62 | string $delimiter = DELIMITER |
||
63 | ) : string { |
||
64 | // Assign the position of first item to show |
||
65 | $item_position = ($index - 1) * $item_per_page + 1; |
||
66 | |||
67 | $items_number = $items->rowCount(); |
||
68 | |||
69 | $counter = 1; |
||
70 | |||
71 | $items_displayed = 0; |
||
72 | |||
73 | $total_pages = intval($items_number / $item_per_page); |
||
74 | |||
75 | // If there an incomplete page |
||
76 | if (($items_number % $item_per_page) != 0) { |
||
77 | $total_pages++; |
||
78 | } |
||
79 | |||
80 | // Initialize keyboard with the list |
||
81 | $keyboard->addListKeyboard($index, $total_pages, $prefix); |
||
82 | |||
83 | $message = ''; |
||
84 | |||
85 | // Iterate over all results |
||
86 | while ($item = $items->fetch()) { |
||
87 | // If we have to display the first item of the page and we |
||
88 | // found the item to show (using the position calculated before) |
||
89 | if ($items_displayed === 0 && $counter === $item_position) { |
||
90 | $message .= $format_item($item, $keyboard); |
||
91 | $items_displayed++; |
||
92 | // If there are space for other items |
||
93 | } elseif ($items_displayed > 0 && $items_displayed < $item_per_page) { |
||
94 | $message .= $delimiter; |
||
95 | $message .= $format_item($item, $keyboard); |
||
96 | |||
97 | $items_displayed++; |
||
98 | } elseif ($items_displayed === $item_per_page) { |
||
99 | break; |
||
100 | } else { |
||
112 |