Completed
Push — master ( 3845e2...a95f7d )
by Danilo
09:37
created

Paginator   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 1
dl 0
loc 75
ccs 0
cts 35
cp 0
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the PhpBotFramework.
5
 *
6
 * PhpBotFramework is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU Lesser General Public License as
8
 * published by the Free Software Foundation, version 3.
9
 *
10
 * PhpBotFramework is distributed in the hope that it will be useful, but
11
 * WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
 * Lesser General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Lesser General Public License
16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17
 */
18
19
namespace PhpBotFramework\Utilities;
20
21
<<<<<<< HEAD
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_SL
Loading history...
22
/**
23
 * \addtogroup Modules
24
 * @{
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 {
101
                $counter++;
102
            }
103
        }
104
105
        return $message;
106
    }
107
}
108
109
/*
110
 * @}
111
 */
112