Skip to content

Commit 435f64d

Browse files
committed
Fixed Query::paginate()
1 parent 2eeef00 commit 435f64d

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

src/Query/Query.php

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -251,21 +251,26 @@ public function getMessage($msgno, $msglist = null){
251251
* @throws GetMessagesFailedException
252252
*/
253253
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+
}
255263
$this->limit = $per_page;
256-
257264
$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));
266271
}
267272

268-
return MessageCollection::make($collection)->paginate($per_page, $this->page, $page_name);
273+
return $messages->paginate($per_page, $this->page, $page_name);
269274
}
270275

271276
/**

0 commit comments

Comments
 (0)