@@ -31,6 +31,7 @@ class Pager implements JsonSerializable
31
31
protected ?int $ nextPage = null ;
32
32
protected int $ lastPage ;
33
33
protected int $ itemsPerPage ;
34
+ protected int $ totalItems ;
34
35
protected int $ surround = 2 ;
35
36
/**
36
37
* @var array<string,string>
@@ -109,19 +110,20 @@ public function __construct(
109
110
if ($ language ) {
110
111
$ this ->setLanguage ($ language );
111
112
}
112
- $ this ->currentPage = static ::sanitize ($ currentPage );
113
- $ this ->itemsPerPage = static ::sanitize ($ itemsPerPage );
114
- $ this ->lastPage = (int ) \ceil ($ totalItems / $ this ->itemsPerPage );
115
- $ this ->lastPage = static ::sanitize ($ this ->lastPage );
116
- if ($ this ->currentPage > 1 ) {
117
- if ($ this ->currentPage - 1 <= $ this ->lastPage ) {
118
- $ this ->previousPage = $ this ->currentPage - 1 ;
119
- } elseif ($ this ->lastPage > 1 ) {
120
- $ this ->previousPage = $ this ->lastPage ;
113
+ $ this ->setCurrentPage (static ::sanitize ($ currentPage ))
114
+ ->setItemsPerPage (static ::sanitize ($ itemsPerPage ))
115
+ ->setTotalItems ($ totalItems );
116
+ $ lastPage = (int ) \ceil ($ this ->getTotalItems () / $ this ->getItemsPerPage ());
117
+ $ this ->setLastPage (static ::sanitize ($ lastPage ));
118
+ if ($ this ->getCurrentPage () > 1 ) {
119
+ if ($ this ->getCurrentPage () - 1 <= $ this ->getLastPage ()) {
120
+ $ this ->setPreviousPage ($ this ->getCurrentPage () - 1 );
121
+ } elseif ($ this ->getLastPage () > 1 ) {
122
+ $ this ->setPreviousPage ($ this ->getLastPage ());
121
123
}
122
124
}
123
- if ($ this ->currentPage < $ this ->lastPage ) {
124
- $ this ->nextPage = $ this ->currentPage + 1 ;
125
+ if ($ this ->getCurrentPage () < $ this ->getLastPage () ) {
126
+ $ this ->setNextPage ( $ this ->getCurrentPage () + 1 ) ;
125
127
}
126
128
isset ($ url ) ? $ this ->setUrl ($ url ) : $ this ->prepareUrl ();
127
129
}
@@ -131,6 +133,52 @@ public function __toString() : string
131
133
return $ this ->render ();
132
134
}
133
135
136
+ /**
137
+ * @since 3.5
138
+ *
139
+ * @return int
140
+ */
141
+ public function getItemsPerPage () : int
142
+ {
143
+ return $ this ->itemsPerPage ;
144
+ }
145
+
146
+ /**
147
+ * @since 3.5
148
+ *
149
+ * @param int $itemsPerPage
150
+ *
151
+ * @return static
152
+ */
153
+ protected function setItemsPerPage (int $ itemsPerPage ) : static
154
+ {
155
+ $ this ->itemsPerPage = $ itemsPerPage ;
156
+ return $ this ;
157
+ }
158
+
159
+ /**
160
+ * @since 3.5
161
+ *
162
+ * @return int
163
+ */
164
+ public function getTotalItems () : int
165
+ {
166
+ return $ this ->totalItems ;
167
+ }
168
+
169
+ /**
170
+ * @since 3.5
171
+ *
172
+ * @param int $totalItems
173
+ *
174
+ * @return static
175
+ */
176
+ protected function setTotalItems (int $ totalItems ) : static
177
+ {
178
+ $ this ->totalItems = $ totalItems ;
179
+ return $ this ;
180
+ }
181
+
134
182
/**
135
183
* @param Language|null $language
136
184
*
@@ -286,6 +334,19 @@ public function getCurrentPage() : int
286
334
return $ this ->currentPage ;
287
335
}
288
336
337
+ /**
338
+ * @since 3.5
339
+ *
340
+ * @param int $currentPage
341
+ *
342
+ * @return static
343
+ */
344
+ protected function setCurrentPage (int $ currentPage ) : static
345
+ {
346
+ $ this ->currentPage = $ currentPage ;
347
+ return $ this ;
348
+ }
349
+
289
350
public function getCurrentPageUrl () : string
290
351
{
291
352
return $ this ->getPageUrl ($ this ->currentPage );
@@ -308,6 +369,19 @@ public function getLastPage() : int
308
369
return $ this ->lastPage ;
309
370
}
310
371
372
+ /**
373
+ * @since 3.5
374
+ *
375
+ * @param int $lastPage
376
+ *
377
+ * @return static
378
+ */
379
+ protected function setLastPage (int $ lastPage ) : static
380
+ {
381
+ $ this ->lastPage = $ lastPage ;
382
+ return $ this ;
383
+ }
384
+
311
385
public function getLastPageUrl () : string
312
386
{
313
387
return $ this ->getPageUrl ($ this ->getLastPage ());
@@ -319,6 +393,19 @@ public function getPreviousPage() : ?int
319
393
return $ this ->previousPage ;
320
394
}
321
395
396
+ /**
397
+ * @since 3.5
398
+ *
399
+ * @param int $previousPage
400
+ *
401
+ * @return static
402
+ */
403
+ protected function setPreviousPage (int $ previousPage ) : static
404
+ {
405
+ $ this ->previousPage = $ previousPage ;
406
+ return $ this ;
407
+ }
408
+
322
409
public function getPreviousPageUrl () : ?string
323
410
{
324
411
return $ this ->getPageUrl ($ this ->getPreviousPage ());
@@ -330,6 +417,19 @@ public function getNextPage() : ?int
330
417
return $ this ->nextPage ;
331
418
}
332
419
420
+ /**
421
+ * @since 3.5
422
+ *
423
+ * @param int $nextPage
424
+ *
425
+ * @return static
426
+ */
427
+ protected function setNextPage (int $ nextPage ) : static
428
+ {
429
+ $ this ->nextPage = $ nextPage ;
430
+ return $ this ;
431
+ }
432
+
333
433
public function getNextPageUrl () : ?string
334
434
{
335
435
return $ this ->getPageUrl ($ this ->getNextPage ());
0 commit comments