| 1 |
<?php |
| 2 |
|
| 3 |
namespace Illuminate\Pagination; |
| 4 |
|
| 5 |
use ArrayAccess; |
| 6 |
use Countable; |
| 7 |
use Illuminate\Contracts\Pagination\Paginator as PaginatorContract; |
| 8 |
use Illuminate\Contracts\Support\Arrayable; |
| 9 |
use Illuminate\Contracts\Support\Jsonable; |
| 10 |
use Illuminate\Support\Collection; |
| 11 |
use IteratorAggregate; |
| 12 |
use JsonSerializable; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
class Paginator extends AbstractPaginator implements Arrayable, ArrayAccess, Countable, IteratorAggregate, Jsonable, JsonSerializable, PaginatorContract |
| 27 |
{ |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
protected $hasMore; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public function __construct($items, $perPage, $currentPage = null, array $options = []) |
| 44 |
{ |
| 45 |
$this->options = $options; |
| 46 |
|
| 47 |
foreach ($options as $key => $value) { |
| 48 |
$this->{$key} = $value; |
| 49 |
} |
| 50 |
|
| 51 |
$this->perPage = $perPage; |
| 52 |
$this->currentPage = $this->setCurrentPage($currentPage); |
| 53 |
$this->path = $this->path !== '/' ? rtrim($this->path, '/') : $this->path; |
| 54 |
|
| 55 |
$this->setItems($items); |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
protected function setCurrentPage($currentPage) |
| 65 |
{ |
| 66 |
$currentPage = $currentPage ?: static::resolveCurrentPage(); |
| 67 |
|
| 68 |
return $this->isValidPageNumber($currentPage) ? (int) $currentPage : 1; |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
protected function setItems($items) |
| 78 |
{ |
| 79 |
$this->items = $items instanceof Collection ? $items : new Collection($items); |
| 80 |
|
| 81 |
$this->hasMore = $this->items->count() > $this->perPage; |
| 82 |
|
| 83 |
$this->items = $this->items->slice(0, $this->perPage); |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
public function nextPageUrl() |
| 92 |
{ |
| 93 |
if ($this->hasMorePages()) { |
| 94 |
return $this->url($this->currentPage() + 1); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
public function links($view = null, $data = []) |
| 106 |
{ |
| 107 |
return $this->render($view, $data); |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
public function render($view = null, $data = []) |
| 118 |
{ |
| 119 |
return static::viewFactory()->make($view ?: static::$defaultSimpleView, array_merge($data, [ |
| 120 |
'paginator' => $this, |
| 121 |
])); |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
public function hasMorePagesWhen($hasMore = true) |
| 131 |
{ |
| 132 |
$this->hasMore = $hasMore; |
| 133 |
|
| 134 |
return $this; |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
public function hasMorePages() |
| 143 |
{ |
| 144 |
return $this->hasMore; |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
public function toArray() |
| 153 |
{ |
| 154 |
return [ |
| 155 |
'current_page' => $this->currentPage(), |
| 156 |
'current_page_url' => $this->url($this->currentPage()), |
| 157 |
'data' => $this->items->toArray(), |
| 158 |
'first_page_url' => $this->url(1), |
| 159 |
'from' => $this->firstItem(), |
| 160 |
'next_page_url' => $this->nextPageUrl(), |
| 161 |
'path' => $this->path(), |
| 162 |
'per_page' => $this->perPage(), |
| 163 |
'prev_page_url' => $this->previousPageUrl(), |
| 164 |
'to' => $this->lastItem(), |
| 165 |
]; |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
public function jsonSerialize(): array |
| 174 |
{ |
| 175 |
return $this->toArray(); |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
public function toJson($options = 0) |
| 185 |
{ |
| 186 |
return json_encode($this->jsonSerialize(), $options); |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
public function toPrettyJson(int $options = 0) |
| 197 |
{ |
| 198 |
return $this->toJson(JSON_PRETTY_PRINT | $options); |
| 199 |
} |
| 200 |
} |