Skip to content

Commit 6983b2c

Browse files
committed
Allow hiding of certain pagination buttons.
1 parent 9174fe4 commit 6983b2c

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

src/Entities/InlineKeyboard.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ public static function getPagination($callback_data, $current_page, $max_pages,
6868
foreach ($labels as $key => &$label) {
6969
if (strpos($label, '%d') !== false) {
7070
$label = sprintf($label, $pages[$key]);
71+
} elseif ($label === '') {
72+
$label = null;
7173
}
7274
}
7375
unset($label);
@@ -79,19 +81,21 @@ public static function getPagination($callback_data, $current_page, $max_pages,
7981

8082
$buttons = [];
8183

82-
if ($current_page > 1) {
84+
if ($current_page > 1 && $labels['first'] !== null) {
8385
$buttons[] = new InlineKeyboardButton(['text' => $labels['first'], 'callback_data' => $callbacks_data['first']]);
8486
}
85-
if ($current_page > 2) {
87+
if ($current_page > 2 && $labels['previous'] !== null) {
8688
$buttons[] = new InlineKeyboardButton(['text' => $labels['previous'], 'callback_data' => $callbacks_data['previous']]);
8789
}
8890

89-
$buttons[] = new InlineKeyboardButton(['text' => $labels['current'], 'callback_data' => $callbacks_data['current']]);
91+
if ($labels['current'] !== null) {
92+
$buttons[] = new InlineKeyboardButton(['text' => $labels['current'], 'callback_data' => $callbacks_data['current']]);
93+
}
9094

91-
if ($current_page < $max_pages - 1) {
95+
if ($current_page < $max_pages - 1 && $labels['next'] !== null) {
9296
$buttons[] = new InlineKeyboardButton(['text' => $labels['next'], 'callback_data' => $callbacks_data['next']]);
9397
}
94-
if ($current_page < $max_pages) {
98+
if ($current_page < $max_pages && $labels['last'] !== null) {
9599
$buttons[] = new InlineKeyboardButton(['text' => $labels['last'], 'callback_data' => $callbacks_data['last']]);
96100
}
97101

tests/unit/Entities/InlineKeyboardTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -200,20 +200,20 @@ public function testInlineKeyboardPagination()
200200
['cbdata_page_1', 'cbdata_page_9', 'cbdata_page_10'],
201201
], 'callback_data', $keyboard);
202202

203-
// custom labels
203+
// custom labels, skipping some buttons
204204
// first, previous, current, next, last
205205
$keyboard = InlineKeyboard::getPagination($callback_data, 5, 10, [
206-
'first' => 'first %d',
206+
'first' => '',
207207
'previous' => 'previous %d',
208-
'current' => 'cur %d rent',
208+
'current' => null,
209209
'next' => '%d next',
210210
'last' => '%d last',
211211
]);
212212
KeyboardTest::assertAllButtonPropertiesEqual([
213-
['first 1', 'previous 4', 'cur 5 rent', '6 next', '10 last'],
213+
['previous 4', '6 next', '10 last'],
214214
], 'text', $keyboard);
215215
KeyboardTest::assertAllButtonPropertiesEqual([
216-
['cbdata_page_1', 'cbdata_page_4', 'cbdata_page_5', 'cbdata_page_6', 'cbdata_page_10'],
216+
['cbdata_page_4', 'cbdata_page_6', 'cbdata_page_10'],
217217
], 'callback_data', $keyboard);
218218
}
219219
}

0 commit comments

Comments
 (0)