@@ -251,21 +251,26 @@ public function getMessage($msgno, $msglist = null){
251
251
* @throws GetMessagesFailedException
252
252
*/
253
253
public function paginate ($ per_page = 5 , $ page = null , $ page_name = 'imap_page ' ){
254
- $ this ->page = $ page > $ this ->page ? $ page : $ this ->page ;
254
+ if (
255
+ $ page === null
256
+ && isset ($ _GET [$ page_name ])
257
+ && $ _GET [$ page_name ] > 0
258
+ ) {
259
+ $ this ->page = intval ($ _GET [$ page_name ]);
260
+ } elseif ($ page > 0 ) {
261
+ $ this ->page = $ page ;
262
+ }
255
263
$ this ->limit = $ per_page ;
256
-
257
264
$ messages = $ this ->get ();
258
- if (($ count = $ messages ->count ()) > 0 ) {
259
- $ limit = $ this ->limit > $ count ? $ count : $ this ->limit ;
260
- $ collection = array_fill (0 , $ messages ->total () - $ limit , true );
261
- $ messages ->each (function ($ message ) use (&$ collection ){
262
- $ collection [] = $ message ;
263
- });
264
- }else {
265
- $ collection = array_fill (0 , $ messages ->total (), true );
265
+
266
+ if ($ messages ->isEmpty ()) {
267
+ $ n_last_page = $ messages ->total () % $ per_page ;
268
+ $ last_page = $ messages ->total () / $ per_page + min (1 , $ n_last_page );
269
+ $ count = $ page == $ last_page ? $ n_last_page : $ per_page ;
270
+ $ messages ->merge (array_fill (0 , $ count , true ));
266
271
}
267
272
268
- return MessageCollection:: make ( $ collection ) ->paginate ($ per_page , $ this ->page , $ page_name );
273
+ return $ messages ->paginate ($ per_page , $ this ->page , $ page_name );
269
274
}
270
275
271
276
/**
0 commit comments