Skip to content

Commit e33a39d

Browse files
committed
Add custom callback_data format.
Rename callback_data parameters.
1 parent 5dec048 commit e33a39d

File tree

3 files changed

+72
-16
lines changed

3 files changed

+72
-16
lines changed

Diff for: README.md

+11-7
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ $labels = [ // optional. Change button labels (showing defau
3838
'next' => '%d ›',
3939
'last' => '%d »',
4040
];
41+
42+
// optional. Change the callback_data format, adding placeholders for data (showing default)
43+
$callback_data_format = 'command={COMMAND}&oldPage={OLD_PAGE}&newPage={NEW_PAGE}'
4144
```
4245

4346
### How To Use
@@ -46,6 +49,7 @@ $labels = [ // optional. Change button labels (showing defau
4649
$ikp = new InlineKeyboardPagination($items, $command);
4750
$ikp->setMaxButtons(7, true); // Second parameter set to always show 7 buttons if possible.
4851
$ikp->setLabels($labels);
52+
$ikp->setCallbackDataFormat($callback_data_format);
4953

5054
// Get pagination.
5155
$pagination = $ikp->getPagination($selected_page);
@@ -60,8 +64,8 @@ Now, `$pagination['keyboard']` is basically a row that contains the pagination.
6064
```php
6165
// Use it in your request.
6266
if (!empty($pagination['keyboard'])) {
63-
//$pagination['keyboard'][0]['callback_data']; // command=testCommand&currentPage=10&nextPage=1
64-
//$pagination['keyboard'][1]['callback_data']; // command=testCommand&currentPage=10&nextPage=7
67+
//$pagination['keyboard'][0]['callback_data']; // command=testCommand&oldPage=10&newPage=1
68+
//$pagination['keyboard'][1]['callback_data']; // command=testCommand&oldPage=10&newPage=7
6569

6670
...
6771
$data['reply_markup' => [
@@ -73,17 +77,17 @@ if (!empty($pagination['keyboard'])) {
7377
}
7478
```
7579

76-
To get the callback data, you can use the provided helper method:
80+
To get the callback data, you can use the provided helper method (only works when using the default callback data format):
7781
```php
7882
// e.g. Callback data.
79-
$callback_data = 'command=testCommand&currentPage=10&nextPage=1';
83+
$callback_data = 'command=testCommand&oldPage=10&newPage=1';
8084

8185
$params = InlineKeyboardPagination::getParametersFromCallbackData($callback_data);
8286

8387
//$params = [
84-
// 'command' => 'testCommand',
85-
// 'currentPage' => '10',
86-
// 'nextPage' => '1',
88+
// 'command' => 'testCommand',
89+
// 'oldPage' => '10',
90+
// 'newPage' => '1',
8791
//];
8892

8993
// or, just use PHP directly if you like. (literally what the helper does!)

Diff for: src/InlineKeyboardPagination.php

+35-2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ class InlineKeyboardPagination implements InlineKeyboardPaginator
4141
*/
4242
private $command;
4343

44+
/**
45+
* @var string
46+
*/
47+
private $callback_data_format = 'command={COMMAND}&oldPage={OLD_PAGE}&newPage={NEW_PAGE}';
48+
4449
/**
4550
* @var array
4651
*/
@@ -68,6 +73,30 @@ public function setMaxButtons(int $max_buttons = 5, bool $force_button_count = f
6873
return $this;
6974
}
7075

76+
/**
77+
* Get the current callback format.
78+
*
79+
* @return string
80+
*/
81+
public function getCallbackDataFormat(): string
82+
{
83+
return $this->callback_data_format;
84+
}
85+
86+
/**
87+
* Set the callback_data format.
88+
*
89+
* @param string $callback_data_format
90+
*
91+
* @return InlineKeyboardPagination
92+
*/
93+
public function setCallbackDataFormat(string $callback_data_format): InlineKeyboardPagination
94+
{
95+
$this->callback_data_format = $callback_data_format;
96+
97+
return $this;
98+
}
99+
71100
/**
72101
* Return list of keyboard button labels.
73102
*
@@ -315,7 +344,11 @@ protected function generateButton(int $page): array
315344
*/
316345
protected function generateCallbackData(int $page): string
317346
{
318-
return "command={$this->command}&currentPage={$this->selected_page}&nextPage={$page}";
347+
return str_replace(
348+
['{COMMAND}', '{OLD_PAGE}', '{NEW_PAGE}'],
349+
[$this->command, $this->selected_page, $page],
350+
$this->callback_data_format
351+
);
319352
}
320353

321354
/**
@@ -337,7 +370,7 @@ protected function getOffset(): int
337370
}
338371

339372
/**
340-
* Get the parameters from the calback query.
373+
* Get the parameters from the callback query.
341374
*
342375
* @param string $data
343376
*

Diff for: tests/InlineKeyboardPaginationTest.php

+26-7
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,27 @@ public function testEmptyItemsConstructor()
7575
$ikp->getPagination();
7676
}
7777

78+
public function testCallbackDataFormat()
79+
{
80+
$ikp = new InlineKeyboardPagination(range(1, 10), 'cmd', 2, 5);
81+
82+
self::assertAllButtonPropertiesEqual([
83+
[
84+
'command=cmd&oldPage=2&newPage=1',
85+
'command=cmd&oldPage=2&newPage=2',
86+
],
87+
], 'callback_data', [$ikp->getPagination()['keyboard']]);
88+
89+
$ikp->setCallbackDataFormat('{COMMAND};{OLD_PAGE};{NEW_PAGE}');
90+
91+
self::assertAllButtonPropertiesEqual([
92+
[
93+
'cmd;2;1',
94+
'cmd;2;2',
95+
],
96+
], 'callback_data', [$ikp->getPagination()['keyboard']]);
97+
}
98+
7899
public function testCallbackDataParser()
79100
{
80101
$ikp = new InlineKeyboardPagination($this->items, $this->command, $this->selected_page, $this->items_per_page);
@@ -83,9 +104,9 @@ public function testCallbackDataParser()
83104
$callback_data = $ikp::getParametersFromCallbackData($data['keyboard'][0]['callback_data']);
84105

85106
self::assertSame([
86-
'command' => $this->command,
87-
'currentPage' => "$this->selected_page",
88-
'nextPage' => '1', // because we're getting the button at position 0, which is page 1
107+
'command' => $this->command,
108+
'oldPage' => "$this->selected_page",
109+
'newPage' => '1', // because we're getting the button at position 0, which is page 1
89110
], $callback_data);
90111
}
91112

@@ -121,9 +142,7 @@ public function testSetMaxButtons()
121142

122143
public function testForceButtonsCount()
123144
{
124-
$cbdata = 'command=%s&currentPage=%d&nextPage=%d';
125-
$command = 'cbdata';
126-
$ikp = new InlineKeyboardPagination(range(1, 10), $command, 1, 1);
145+
$ikp = new InlineKeyboardPagination(range(1, 10), 'cbdata', 1, 1);
127146

128147
// testing with 8 flexible buttons
129148
$ikp->setMaxButtons(8, false);
@@ -207,7 +226,7 @@ public function testInvalidItemsPerPage()
207226

208227
public function testButtonLabels()
209228
{
210-
$cbdata = 'command=%s&currentPage=%d&nextPage=%d';
229+
$cbdata = 'command=%s&oldPage=%d&newPage=%d';
211230
$command = 'cbdata';
212231
$ikp1 = new InlineKeyboardPagination(range(1, 1), $command, 1, $this->items_per_page);
213232
$ikp10 = new InlineKeyboardPagination(range(1, $this->items_per_page * 10), $command, 1, $this->items_per_page);

0 commit comments

Comments
 (0)