Webklex /
php-imap
| 1 | <?php |
||
| 2 | /* |
||
| 3 | * File: PaginatedCollection.php |
||
| 4 | * Category: Collection |
||
| 5 | * Author: M. Goldenbaum |
||
| 6 | * Created: 16.03.18 03:13 |
||
| 7 | * Updated: - |
||
| 8 | * |
||
| 9 | * Description: |
||
| 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 | * Class PaginatedCollection |
||
| 21 | * |
||
| 22 | * @package Webklex\PHPIMAP\Support |
||
| 23 | */ |
||
| 24 | class PaginatedCollection extends Collection { |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Number of total entries |
||
| 28 | * |
||
| 29 | * @var int $total |
||
| 30 | */ |
||
| 31 | protected $total; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Paginate the current collection. |
||
| 35 | * @param int $per_page |
||
| 36 | * @param int|null $page |
||
| 37 | * @param string $page_name |
||
| 38 | * @param boolean $prepaginated |
||
| 39 | * |
||
| 40 | * @return LengthAwarePaginator |
||
| 41 | */ |
||
| 42 | public function paginate(int $per_page = 15, $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 | * Create a new length-aware paginator instance. |
||
| 57 | * @param array $items |
||
| 58 | * @param int $total |
||
| 59 | * @param int $per_page |
||
| 60 | * @param int|null $current_page |
||
| 61 | * @param array $options |
||
| 62 | * |
||
| 63 | * @return LengthAwarePaginator |
||
| 64 | */ |
||
| 65 | protected function paginator(array $items, int $total, int $per_page, $current_page, array $options): LengthAwarePaginator { |
||
| 66 | return new LengthAwarePaginator($items, $total, $per_page, $current_page, $options); |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Get and set the total amount |
||
| 71 | * @param null $total |
||
|
0 ignored issues
–
show
Documentation
Bug
introduced
by
Loading history...
|
|||
| 72 | * |
||
| 73 | * @return int|null |
||
| 74 | */ |
||
| 75 | public function total($total = null) { |
||
| 76 | if($total === null) { |
||
|
0 ignored issues
–
show
|
|||
| 77 | return $this->total; |
||
| 78 | } |
||
| 79 | |||
| 80 | return $this->total = $total; |
||
| 81 | } |
||
| 82 | } |
||
| 83 |