Completed
Pull Request — develop (#438)
by Armando
04:59
created

InlineKeyboard::getPageFromCallbackData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * This file is part of the TelegramBot package.
4
 *
5
 * (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Longman\TelegramBot\Entities;
12
13
/**
14
 * Class InlineKeyboard
15
 *
16
 * @link https://core.telegram.org/bots/api#inlinekeyboardmarkup
17
 */
18
class InlineKeyboard extends Keyboard
19
{
20
    /**
21
     * Get an inline pagination keyboard.
22
     *
23
     * - $callback_data is an ID for the CallbackqueryCommand, to know where the request comes from.
24
     * The ID is automatically appended with '_page_%d' to filter out the selected page number.
25
     *
26
     * - $labels allows for custom button labels, using '%d' placeholders.
27
     * Default:
28
     * ```
29
     * [
30
     *     'first'    => '« %d',
31
     *     'previous' => '‹ %d',
32
     *     'current'  => '· %d ·',
33
     *     'next'     => '%d ›',
34
     *     'last'     => '%d »',
35
     * ]
36
     * ``
37
     *`
38
     * initial idea from: https://stackoverflow.com/a/42879866
39
     *
40
     * @param string $callback_data
41
     * @param int    $current_page
42
     * @param int    $max_pages
43
     * @param array  $labels
44
     *
45
     * @return \Longman\TelegramBot\Entities\InlineKeyboard
46
     */
47 1
    public static function getPagination($callback_data, $current_page, $max_pages, array $labels = [])
48
    {
49 1
        $callback_data .= '_page_%d';
50
51
        // Merge labels with defaults.
52 1
        $labels = array_merge([
53 1
            'first'    => '« %d',
54
            'previous' => '‹ %d',
55
            'current'  => '· %d ·',
56
            'next'     => '%d ›',
57
            'last'     => '%d »',
58
        ], $labels);
59
        $pages  = [
60 1
            'first'    => 1,
61 1
            'previous' => $current_page - 1,
62 1
            'current'  => $current_page,
63 1
            'next'     => $current_page + 1,
64 1
            'last'     => $max_pages,
65
        ];
66
67
        // Set labels for keyboard, replacing placeholders with page numbers.
68 1
        foreach ($labels as $key => &$label) {
69 1
            if (strpos($label, '%d') !== false) {
70 1
                $label = sprintf($label, $pages[$key]);
71
            }
72
        }
73 1
        unset($label);
74
75 1
        $callbacks_data = [];
76 1
        foreach ($pages as $key => $page) {
77 1
            $callbacks_data[$key] = sprintf($callback_data, $page);
78
        }
79
80 1
        $buttons = [];
81
82 1 View Code Duplication
        if ($current_page > 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
83 1
            $buttons[] = new InlineKeyboardButton(['text' => $labels['first'], 'callback_data' => $callbacks_data['first']]);
84
        }
85 1 View Code Duplication
        if ($current_page > 2) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
86 1
            $buttons[] = new InlineKeyboardButton(['text' => $labels['previous'], 'callback_data' => $callbacks_data['previous']]);
87
        }
88
89 1
        $buttons[] = new InlineKeyboardButton(['text' => $labels['current'], 'callback_data' => $callbacks_data['current']]);
90
91 1 View Code Duplication
        if ($current_page < $max_pages - 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
92 1
            $buttons[] = new InlineKeyboardButton(['text' => $labels['next'], 'callback_data' => $callbacks_data['next']]);
93
        }
94 1 View Code Duplication
        if ($current_page < $max_pages) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
95 1
            $buttons[] = new InlineKeyboardButton(['text' => $labels['last'], 'callback_data' => $callbacks_data['last']]);
96
        }
97
98 1
        return new InlineKeyboard($buttons);
99
    }
100
101
    /**
102
     * Extract the page number from the passed callback data.
103
     *
104
     * @param string $callback_data
105
     *
106
     * @return int
107
     */
108
    public static function getPageFromCallbackData($callback_data)
109
    {
110
        return (int) preg_replace('/.*_page_(\d+)$/', '$1', $callback_data);
111
    }
112
}
113