@@ -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}
0 commit comments