Skip to content

Commit 5dec048

Browse files
committed
Actually, just make this return a pagination row, not the entire keyboard.
1 parent 16bf661 commit 5dec048

File tree

3 files changed

+53
-36
lines changed

3 files changed

+53
-36
lines changed

README.md

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
- [Usage](#usage)
1414
- [Test Data](#test-data)
1515
- [How To Use](#how-to-use)
16-
- [Result](#result)
1716
- [Code Quality](#code-quality)
1817
- [License](#license)
1918

@@ -31,12 +30,13 @@ composer require php-telegram-bot/inline-keyboard-pagination:^1.0.0
3130
$items = range(1, 100); // required.
3231
$command = 'testCommand'; // optional. Default: pagination
3332
$selected_page = 10; // optional. Default: 1
34-
$labels = [ // optional. Change button labels
35-
'first' => '%d',
36-
'previous' => 'previous %d',
37-
'current' => null,
38-
'next' => '%d next',
39-
'last' => '%d',
33+
$labels = [ // optional. Change button labels (showing defaults)
34+
'default' => '%d',
35+
'first' => '« %d',
36+
'previous' => '‹ %d',
37+
'current' => '· %d ·',
38+
'next' => '%d ›',
39+
'last' => '%d »',
4040
];
4141
```
4242

@@ -50,28 +50,46 @@ $ikp->setLabels($labels);
5050
// Get pagination.
5151
$pagination = $ikp->getPagination($selected_page);
5252

53-
// or, in 2 steps:
53+
// or, in 2 steps.
5454
$ikp->setSelectedPage($selected_page);
5555
$pagination = $ikp->getPagination();
5656
```
5757

58-
### Result
58+
Now, `$pagination['keyboard']` is basically a row that contains the pagination.
59+
5960
```php
61+
// Use it in your request.
6062
if (!empty($pagination['keyboard'])) {
61-
$pagination['keyboard'][0]['callback_data']; // command=testCommand&currentPage=10&nextPage=1
62-
$pagination['keyboard'][1]['callback_data']; // command=testCommand&currentPage=10&nextPage=7
63+
//$pagination['keyboard'][0]['callback_data']; // command=testCommand&currentPage=10&nextPage=1
64+
//$pagination['keyboard'][1]['callback_data']; // command=testCommand&currentPage=10&nextPage=7
65+
6366
...
64-
65-
$response = [
66-
'reply_markup' => [
67-
'inline_keyboard' => [
68-
$pagination['keyboard'],
69-
],
67+
$data['reply_markup' => [
68+
'inline_keyboard' => [
69+
$pagination['keyboard'],
7070
],
7171
];
72+
...
7273
}
7374
```
7475

76+
To get the callback data, you can use the provided helper method:
77+
```php
78+
// e.g. Callback data.
79+
$callback_data = 'command=testCommand&currentPage=10&nextPage=1';
80+
81+
$params = InlineKeyboardPagination::getParametersFromCallbackData($callback_data);
82+
83+
//$params = [
84+
// 'command' => 'testCommand',
85+
// 'currentPage' => '10',
86+
// 'nextPage' => '1',
87+
//];
88+
89+
// or, just use PHP directly if you like. (literally what the helper does!)
90+
parse_str($callback_data, $params);
91+
```
92+
7593
## Code Quality
7694

7795
Run the PHPUnit tests via Composer script.

src/InlineKeyboardPagination.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,7 @@ protected function generateKeyboard(): array
250250
$button['text'] = sprintf($label, $page);
251251
}
252252

253-
// Pagination has 1 single row of buttons, so wrap in (row) array.
254-
return [array_values(array_filter($buttons))];
253+
return array_values(array_filter($buttons));
255254
}
256255

257256
/**

tests/InlineKeyboardPaginationTest.php

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ public function testValidConstructor()
5050
$this->assertArrayHasKey('items', $data);
5151
$this->assertCount($this->items_per_page, $data['items']);
5252
$this->assertArrayHasKey('keyboard', $data);
53-
$this->assertArrayHasKey(0, $data['keyboard'][0]);
54-
$this->assertArrayHasKey('text', $data['keyboard'][0][0]);
55-
$this->assertStringStartsWith("command={$this->command}", $data['keyboard'][0][0]['callback_data']);
53+
$this->assertArrayHasKey(0, $data['keyboard']);
54+
$this->assertArrayHasKey('text', $data['keyboard'][0]);
55+
$this->assertStringStartsWith("command={$this->command}", $data['keyboard'][0]['callback_data']);
5656
}
5757

5858
/**
@@ -80,7 +80,7 @@ public function testCallbackDataParser()
8080
$ikp = new InlineKeyboardPagination($this->items, $this->command, $this->selected_page, $this->items_per_page);
8181
$data = $ikp->getPagination();
8282

83-
$callback_data = $ikp::getParametersFromCallbackData($data['keyboard'][0][0]['callback_data']);
83+
$callback_data = $ikp::getParametersFromCallbackData($data['keyboard'][0]['callback_data']);
8484

8585
self::assertSame([
8686
'command' => $this->command,
@@ -130,41 +130,41 @@ public function testForceButtonsCount()
130130

131131
self::assertAllButtonPropertiesEqual([
132132
['· 1 ·', '2', '3', '4 ›', '5 ›', '6 ›', '7 ›', '10 »'],
133-
], 'text', $ikp->getPagination(1)['keyboard']);
133+
], 'text', [$ikp->getPagination(1)['keyboard']]);
134134

135135
self::assertAllButtonPropertiesEqual([
136136
['« 1', '‹ 4', '· 5 ·', '6 ›', '10 »'],
137-
], 'text', $ikp->getPagination(5)['keyboard']);
137+
], 'text', [$ikp->getPagination(5)['keyboard']]);
138138

139139
// testing with 8 fixed buttons
140140
$ikp->setMaxButtons(8, true);
141141

142142
self::assertAllButtonPropertiesEqual([
143143
['· 1 ·', '2', '3', '4 ›', '5 ›', '6 ›', '7 ›', '10 »'],
144-
], 'text', $ikp->getPagination(1)['keyboard']);
144+
], 'text', [$ikp->getPagination(1)['keyboard']]);
145145

146146
self::assertAllButtonPropertiesEqual([
147147
['· 1 ·', '2', '3', '4 ›', '5 ›', '6 ›', '7 ›', '10 »'],
148-
], 'text', $ikp->getPagination(1)['keyboard']);
148+
], 'text', [$ikp->getPagination(1)['keyboard']]);
149149

150150
self::assertAllButtonPropertiesEqual([
151151
['« 1', '‹ 2', '‹ 3', '‹ 4', '· 5 ·', '6 ›', '7 ›', '10 »'],
152-
], 'text', $ikp->getPagination(5)['keyboard']);
152+
], 'text', [$ikp->getPagination(5)['keyboard']]);
153153

154154
// testing with 7 fixed buttons
155155
$ikp->setMaxButtons(7, true);
156156

157157
self::assertAllButtonPropertiesEqual([
158158
['· 1 ·', '2', '3', '4 ›', '5 ›', '6 ›', '10 »'],
159-
], 'text', $ikp->getPagination(1)['keyboard']);
159+
], 'text', [$ikp->getPagination(1)['keyboard']]);
160160

161161
self::assertAllButtonPropertiesEqual([
162162
['« 1', '‹ 3', '‹ 4', '· 5 ·', '6 ›', '7 ›', '10 »'],
163-
], 'text', $ikp->getPagination(5)['keyboard']);
163+
], 'text', [$ikp->getPagination(5)['keyboard']]);
164164

165165
self::assertAllButtonPropertiesEqual([
166166
['« 1', '‹ 5', '‹ 6', '‹ 7', '8', '9', '· 10 ·'],
167-
], 'text', $ikp->getPagination(10)['keyboard']);
167+
], 'text', [$ikp->getPagination(10)['keyboard']]);
168168
}
169169

170170
/**
@@ -213,7 +213,7 @@ public function testButtonLabels()
213213
$ikp10 = new InlineKeyboardPagination(range(1, $this->items_per_page * 10), $command, 1, $this->items_per_page);
214214

215215
// current
216-
$keyboard = $ikp1->getPagination(1)['keyboard'];
216+
$keyboard = [$ikp1->getPagination(1)['keyboard']];
217217
self::assertAllButtonPropertiesEqual([
218218
['· 1 ·'],
219219
], 'text', $keyboard);
@@ -224,7 +224,7 @@ public function testButtonLabels()
224224
], 'callback_data', $keyboard);
225225

226226
// first, previous, current, next, last
227-
$keyboard = $ikp10->getPagination(5)['keyboard'];
227+
$keyboard = [$ikp10->getPagination(5)['keyboard']];
228228
self::assertAllButtonPropertiesEqual([
229229
['« 1', '‹ 4', '· 5 ·', '6 ›', '10 »'],
230230
], 'text', $keyboard);
@@ -239,7 +239,7 @@ public function testButtonLabels()
239239
], 'callback_data', $keyboard);
240240

241241
// first, previous, current, last
242-
$keyboard = $ikp10->getPagination(9)['keyboard'];
242+
$keyboard = [$ikp10->getPagination(9)['keyboard']];
243243
self::assertAllButtonPropertiesEqual([
244244
['« 1', '‹ 7', '8', '· 9 ·', '10'],
245245
], 'text', $keyboard);
@@ -254,7 +254,7 @@ public function testButtonLabels()
254254
], 'callback_data', $keyboard);
255255

256256
// first, previous, current
257-
$keyboard = $ikp10->getPagination(10)['keyboard'];
257+
$keyboard = [$ikp10->getPagination(10)['keyboard']];
258258
self::assertAllButtonPropertiesEqual([
259259
['« 1', '‹ 7', '8', '9', '· 10 ·'],
260260
], 'text', $keyboard);
@@ -280,7 +280,7 @@ public function testButtonLabels()
280280
$ikp10->setLabels($labels);
281281
self::assertEquals($labels, $ikp10->getLabels());
282282

283-
$keyboard = $ikp10->getPagination(5)['keyboard'];
283+
$keyboard = [$ikp10->getPagination(5)['keyboard']];
284284
self::assertAllButtonPropertiesEqual([
285285
['previous 4', '6 next', 'last'],
286286
], 'text', $keyboard);

0 commit comments

Comments
 (0)