| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
namespace Webklex\PHPIMAP\Support; |
| 14 |
|
| 15 |
use Illuminate\Pagination\LengthAwarePaginator; |
| 16 |
use Illuminate\Support\Collection; |
| 17 |
use Illuminate\Pagination\Paginator; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
class PaginatedCollection extends Collection { |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
protected int $total = 0; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
public function paginate(int $per_page = 15, ?int $page = null, string $page_name = 'page', bool $prepaginated = false): LengthAwarePaginator { |
| 43 |
$page = $page ?: Paginator::resolveCurrentPage($page_name); |
| 44 |
|
| 45 |
$total = $this->total ?: $this->count(); |
| 46 |
|
| 47 |
$results = !$prepaginated && $total ? $this->forPage($page, $per_page)->toArray() : $this->all(); |
| 48 |
|
| 49 |
return $this->paginator($results, $total, $per_page, $page, [ |
| 50 |
'path' => Paginator::resolveCurrentPath(), |
| 51 |
'pageName' => $page_name, |
| 52 |
]); |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
protected function paginator(array $items, int $total, int $per_page, ?int $current_page, array $options): LengthAwarePaginator { |
| 66 |
return new LengthAwarePaginator($items, $total, $per_page, $current_page, $options); |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
public function total($total = null): ?int { |
| 76 |
if($total === null) { |
| 77 |
return $this->total; |
| 78 |
} |
| 79 |
|
| 80 |
return $this->total = $total; |
| 81 |
} |
| 82 |
} |