Skip to content

Commit 3203c78

Browse files
committed
More helpful error messages, rename some variables and remove superfluous ones.
1 parent ae91d6a commit 3203c78

File tree

3 files changed

+51
-32
lines changed

3 files changed

+51
-32
lines changed

src/InlineKeyboardPagination.php

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,13 @@ class InlineKeyboardPagination implements InlineKeyboardPaginator
1414
/**
1515
* @var integer
1616
*/
17-
private $limit;
17+
private $items_per_page;
1818

1919
/**
2020
* @var integer
2121
*/
2222
private $max_buttons = 5;
2323

24-
/**
25-
* @var integer
26-
*/
27-
private $first_page = 1;
28-
2924
/**
3025
* @var integer
3126
*/
@@ -58,7 +53,7 @@ class InlineKeyboardPagination implements InlineKeyboardPaginator
5853
public function setMaxButtons(int $max_buttons = 5): InlineKeyboardPagination
5954
{
6055
if ($max_buttons < 5 || $max_buttons > 8) {
61-
throw new InlineKeyboardPaginationException('Invalid max buttons');
56+
throw new InlineKeyboardPaginationException('Invalid max buttons, must be between 5 and 8.');
6257
}
6358
$this->max_buttons = $max_buttons;
6459

@@ -96,28 +91,52 @@ public function setCommand(string $command = 'pagination'): InlineKeyboardPagina
9691
public function setSelectedPage(int $selected_page): InlineKeyboardPagination
9792
{
9893
if ($selected_page < 1 || $selected_page > $this->number_of_pages) {
99-
throw new InlineKeyboardPaginationException('Invalid selected page');
94+
throw new InlineKeyboardPaginationException('Invalid selected page, must be between 1 and ' . $this->number_of_pages);
10095
}
10196
$this->selected_page = $selected_page;
10297

10398
return $this;
10499
}
105100

101+
/**
102+
* @return int
103+
*/
104+
public function getItemsPerPage(): int
105+
{
106+
return $this->items_per_page;
107+
}
108+
109+
/**
110+
* @param int $items_per_page
111+
*
112+
* @return InlineKeyboardPagination
113+
* @throws InlineKeyboardPaginationException
114+
*/
115+
public function setItemsPerPage($items_per_page): InlineKeyboardPagination
116+
{
117+
if ($items_per_page <= 0) {
118+
throw new InlineKeyboardPaginationException('Invalid number of items per page, must be at least 1');
119+
}
120+
$this->items_per_page = $items_per_page;
121+
122+
return $this;
123+
}
124+
106125
/**
107126
* TelegramBotPagination constructor.
108127
*
109128
* @inheritdoc
110129
* @throws InlineKeyboardPaginationException
111130
*/
112-
public function __construct(array $items, string $command = 'pagination', int $selected_page = 1, int $limit = 3)
131+
public function __construct(array $items, string $command = 'pagination', int $selected_page = 1, int $items_per_page = 3)
113132
{
114-
$this->number_of_pages = $this->countTheNumberOfPage(count($items), $limit);
133+
$this->number_of_pages = $this->countTheNumberOfPage(count($items), $items_per_page);
115134

116135
$this->setSelectedPage($selected_page);
117136

118-
$this->items = $items;
119-
$this->limit = $limit;
120-
$this->command = $command;
137+
$this->items = $items;
138+
$this->items_per_page = $items_per_page;
139+
$this->command = $command;
121140
}
122141

123142
/**
@@ -144,7 +163,7 @@ protected function generateKeyboard(): array
144163
$buttons = [];
145164

146165
if ($this->number_of_pages > $this->max_buttons) {
147-
$buttons[] = $this->generateButton($this->first_page);
166+
$buttons[] = $this->generateButton(1);
148167

149168
$range = $this->generateRange();
150169

@@ -169,7 +188,7 @@ protected function generateRange(): array
169188
{
170189
$number_of_intermediate_buttons = $this->max_buttons - 2;
171190

172-
if ($this->selected_page === $this->first_page) {
191+
if ($this->selected_page === 1) {
173192
$from = 2;
174193
$to = $from + $number_of_intermediate_buttons;
175194
} elseif ($this->selected_page === $this->number_of_pages) {
@@ -179,7 +198,7 @@ protected function generateRange(): array
179198
if (($this->selected_page + $number_of_intermediate_buttons) > $this->number_of_pages) {
180199
$from = $this->number_of_pages - $number_of_intermediate_buttons;
181200
$to = $this->number_of_pages;
182-
} elseif (($this->selected_page - 2) < $this->first_page) {
201+
} elseif (($this->selected_page - 2) < 1) {
183202
$from = $this->selected_page;
184203
$to = $this->selected_page + $number_of_intermediate_buttons;
185204
} else {
@@ -226,25 +245,25 @@ protected function generateCallbackData(int $next_page): string
226245
*/
227246
protected function getPreparedItems(): array
228247
{
229-
return array_slice($this->items, $this->getOffset(), $this->limit);
248+
return array_slice($this->items, $this->getOffset(), $this->items_per_page);
230249
}
231250

232251
/**
233252
* @return int
234253
*/
235254
protected function getOffset(): int
236255
{
237-
return $this->limit * ($this->selected_page - 1);
256+
return $this->items_per_page * ($this->selected_page - 1);
238257
}
239258

240259
/**
241-
* @param $items_length
242-
* @param $limit
260+
* @param $items_count
261+
* @param $items_per_page
243262
*
244263
* @return int
245264
*/
246-
protected function countTheNumberOfPage($items_length, $limit): int
265+
protected function countTheNumberOfPage($items_count, $items_per_page): int
247266
{
248-
return (int) ceil($items_length / $limit);
267+
return (int) ceil($items_count / $items_per_page);
249268
}
250269
}

src/InlineKeyboardPaginator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ public function setSelectedPage(int $selected_page): InlineKeyboardPagination;
4545
* @param array $items
4646
* @param string $command
4747
* @param int $selected_page
48-
* @param int $limit
48+
* @param int $items_per_page
4949
*/
50-
public function __construct(array $items, string $command, int $selected_page, int $limit);
50+
public function __construct(array $items, string $command, int $selected_page, int $items_per_page);
5151

5252
/**
5353
* @param int $selected_page

tests/InlineKeyboardPaginationTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ final class InlineKeyboardPaginationTest extends \PHPUnit\Framework\TestCase
1212
/**
1313
* @var int
1414
*/
15-
private $limit = 5;
15+
private $items_per_page = 5;
1616

1717
/**
1818
* @var int
@@ -43,11 +43,11 @@ public function __construct()
4343

4444
public function testValidConstructor()
4545
{
46-
$ikp = new InlineKeyboardPagination($this->items, $this->command, $this->selected_page, $this->limit);
46+
$ikp = new InlineKeyboardPagination($this->items, $this->command, $this->selected_page, $this->items_per_page);
4747

4848
$data = $ikp->paginate();
4949

50-
$this->assertCount($this->limit, $data['items']);
50+
$this->assertCount($this->items_per_page, $data['items']);
5151
$this->assertArrayHasKey('keyboard', $data);
5252
$this->assertArrayHasKey(0, $data['keyboard']);
5353
$this->assertArrayHasKey('text', $data['keyboard'][0]);
@@ -59,15 +59,15 @@ public function testValidConstructor()
5959
*/
6060
public function testInvalidConstructor()
6161
{
62-
$ikp = new InlineKeyboardPagination($this->items, $this->command, 10000, $this->limit);
62+
$ikp = new InlineKeyboardPagination($this->items, $this->command, 10000, $this->items_per_page);
6363
$ikp->paginate();
6464
}
6565

6666
public function testValidPaginate()
6767
{
68-
$ikp = new InlineKeyboardPagination($this->items, $this->command, $this->selected_page, $this->limit);
68+
$ikp = new InlineKeyboardPagination($this->items, $this->command, $this->selected_page, $this->items_per_page);
6969

70-
$length = (int) ceil(count($this->items) / $this->limit);
70+
$length = (int) ceil(count($this->items) / $this->items_per_page);
7171

7272
for ($i = 1; $i < $length; $i++) {
7373
$ikp->paginate($i);
@@ -81,9 +81,9 @@ public function testValidPaginate()
8181
*/
8282
public function testInvalidPaginate()
8383
{
84-
$ikp = new InlineKeyboardPagination($this->items, $this->command, $this->selected_page, $this->limit);
84+
$ikp = new InlineKeyboardPagination($this->items, $this->command, $this->selected_page, $this->items_per_page);
8585

86-
$length = (int) ceil(count($this->items) / $this->limit) + 1;
86+
$length = (int) ceil(count($this->items) / $this->items_per_page) + 1;
8787

8888
for ($i = $length; $i < $length * 2; $i++) {
8989
$ikp->paginate($i);

0 commit comments

Comments
 (0)