@@ -14,18 +14,13 @@ class InlineKeyboardPagination implements InlineKeyboardPaginator
14
14
/**
15
15
* @var integer
16
16
*/
17
- private $ limit ;
17
+ private $ items_per_page ;
18
18
19
19
/**
20
20
* @var integer
21
21
*/
22
22
private $ max_buttons = 5 ;
23
23
24
- /**
25
- * @var integer
26
- */
27
- private $ first_page = 1 ;
28
-
29
24
/**
30
25
* @var integer
31
26
*/
@@ -58,7 +53,7 @@ class InlineKeyboardPagination implements InlineKeyboardPaginator
58
53
public function setMaxButtons (int $ max_buttons = 5 ): InlineKeyboardPagination
59
54
{
60
55
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. ' );
62
57
}
63
58
$ this ->max_buttons = $ max_buttons ;
64
59
@@ -96,28 +91,52 @@ public function setCommand(string $command = 'pagination'): InlineKeyboardPagina
96
91
public function setSelectedPage (int $ selected_page ): InlineKeyboardPagination
97
92
{
98
93
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 );
100
95
}
101
96
$ this ->selected_page = $ selected_page ;
102
97
103
98
return $ this ;
104
99
}
105
100
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
+
106
125
/**
107
126
* TelegramBotPagination constructor.
108
127
*
109
128
* @inheritdoc
110
129
* @throws InlineKeyboardPaginationException
111
130
*/
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 )
113
132
{
114
- $ this ->number_of_pages = $ this ->countTheNumberOfPage (count ($ items ), $ limit );
133
+ $ this ->number_of_pages = $ this ->countTheNumberOfPage (count ($ items ), $ items_per_page );
115
134
116
135
$ this ->setSelectedPage ($ selected_page );
117
136
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 ;
121
140
}
122
141
123
142
/**
@@ -144,7 +163,7 @@ protected function generateKeyboard(): array
144
163
$ buttons = [];
145
164
146
165
if ($ this ->number_of_pages > $ this ->max_buttons ) {
147
- $ buttons [] = $ this ->generateButton ($ this -> first_page );
166
+ $ buttons [] = $ this ->generateButton (1 );
148
167
149
168
$ range = $ this ->generateRange ();
150
169
@@ -169,7 +188,7 @@ protected function generateRange(): array
169
188
{
170
189
$ number_of_intermediate_buttons = $ this ->max_buttons - 2 ;
171
190
172
- if ($ this ->selected_page === $ this -> first_page ) {
191
+ if ($ this ->selected_page === 1 ) {
173
192
$ from = 2 ;
174
193
$ to = $ from + $ number_of_intermediate_buttons ;
175
194
} elseif ($ this ->selected_page === $ this ->number_of_pages ) {
@@ -179,7 +198,7 @@ protected function generateRange(): array
179
198
if (($ this ->selected_page + $ number_of_intermediate_buttons ) > $ this ->number_of_pages ) {
180
199
$ from = $ this ->number_of_pages - $ number_of_intermediate_buttons ;
181
200
$ to = $ this ->number_of_pages ;
182
- } elseif (($ this ->selected_page - 2 ) < $ this -> first_page ) {
201
+ } elseif (($ this ->selected_page - 2 ) < 1 ) {
183
202
$ from = $ this ->selected_page ;
184
203
$ to = $ this ->selected_page + $ number_of_intermediate_buttons ;
185
204
} else {
@@ -226,25 +245,25 @@ protected function generateCallbackData(int $next_page): string
226
245
*/
227
246
protected function getPreparedItems (): array
228
247
{
229
- return array_slice ($ this ->items , $ this ->getOffset (), $ this ->limit );
248
+ return array_slice ($ this ->items , $ this ->getOffset (), $ this ->items_per_page );
230
249
}
231
250
232
251
/**
233
252
* @return int
234
253
*/
235
254
protected function getOffset (): int
236
255
{
237
- return $ this ->limit * ($ this ->selected_page - 1 );
256
+ return $ this ->items_per_page * ($ this ->selected_page - 1 );
238
257
}
239
258
240
259
/**
241
- * @param $items_length
242
- * @param $limit
260
+ * @param $items_count
261
+ * @param $items_per_page
243
262
*
244
263
* @return int
245
264
*/
246
- protected function countTheNumberOfPage ($ items_length , $ limit ): int
265
+ protected function countTheNumberOfPage ($ items_count , $ items_per_page ): int
247
266
{
248
- return (int ) ceil ($ items_length / $ limit );
267
+ return (int ) ceil ($ items_count / $ items_per_page );
249
268
}
250
269
}
0 commit comments