| 1 |
<?php |
| 2 |
|
| 3 |
namespace Illuminate\Pagination; |
| 4 |
|
| 5 |
use ArrayAccess; |
| 6 |
use Countable; |
| 7 |
use Illuminate\Contracts\Pagination\LengthAwarePaginator as LengthAwarePaginatorContract; |
| 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 LengthAwarePaginator extends AbstractPaginator implements Arrayable, ArrayAccess, Countable, IteratorAggregate, Jsonable, JsonSerializable, LengthAwarePaginatorContract |
| 27 |
{ |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
protected $total; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
protected $lastPage; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public function __construct($items, $total, $perPage, $currentPage = null, array $options = []) |
| 52 |
{ |
| 53 |
$this->options = $options; |
| 54 |
|
| 55 |
foreach ($options as $key => $value) { |
| 56 |
$this->{$key} = $value; |
| 57 |
} |
| 58 |
|
| 59 |
$this->total = $total; |
| 60 |
$this->perPage = (int) $perPage; |
| 61 |
$this->lastPage = max((int) ceil($total / $perPage), 1); |
| 62 |
$this->path = $this->path !== '/' ? rtrim($this->path, '/') : $this->path; |
| 63 |
$this->currentPage = $this->setCurrentPage($currentPage, $this->pageName); |
| 64 |
$this->items = $items instanceof Collection ? $items : new Collection($items); |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
protected function setCurrentPage($currentPage, $pageName) |
| 75 |
{ |
| 76 |
$currentPage = $currentPage ?: static::resolveCurrentPage($pageName); |
| 77 |
|
| 78 |
return $this->isValidPageNumber($currentPage) ? (int) $currentPage : 1; |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
public function links($view = null, $data = []) |
| 89 |
{ |
| 90 |
return $this->render($view, $data); |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
public function render($view = null, $data = []) |
| 101 |
{ |
| 102 |
return static::viewFactory()->make($view ?: static::$defaultView, array_merge($data, [ |
| 103 |
'paginator' => $this, |
| 104 |
'elements' => $this->elements(), |
| 105 |
])); |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
public function linkCollection() |
| 114 |
{ |
| 115 |
return (new Collection($this->elements()))->flatMap(function ($item) { |
| 116 |
if (! is_array($item)) { |
| 117 |
return [['url' => null, 'label' => '...', 'active' => false]]; |
| 118 |
} |
| 119 |
|
| 120 |
return (new Collection($item))->map(function ($url, $page) { |
| 121 |
return [ |
| 122 |
'url' => $url, |
| 123 |
'label' => (string) $page, |
| 124 |
'page' => $page, |
| 125 |
'active' => $this->currentPage() === $page, |
| 126 |
]; |
| 127 |
}); |
| 128 |
})->prepend([ |
| 129 |
'url' => $this->previousPageUrl(), |
| 130 |
'label' => function_exists('__') ? __('pagination.previous') : 'Previous', |
| 131 |
'page' => $this->currentPage() > 1 ? $this->currentPage() - 1 : null, |
| 132 |
'active' => false, |
| 133 |
])->push([ |
| 134 |
'url' => $this->nextPageUrl(), |
| 135 |
'label' => function_exists('__') ? __('pagination.next') : 'Next', |
| 136 |
'page' => $this->hasMorePages() ? $this->currentPage() + 1 : null, |
| 137 |
'active' => false, |
| 138 |
]); |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
protected function elements() |
| 147 |
{ |
| 148 |
$window = UrlWindow::make($this); |
| 149 |
|
| 150 |
return array_filter([ |
| 151 |
$window['first'], |
| 152 |
is_array($window['slider']) ? '...' : null, |
| 153 |
$window['slider'], |
| 154 |
is_array($window['last']) ? '...' : null, |
| 155 |
$window['last'], |
| 156 |
]); |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
public function total() |
| 165 |
{ |
| 166 |
return $this->total; |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
public function hasMorePages() |
| 175 |
{ |
| 176 |
return $this->currentPage() < $this->lastPage(); |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
public function nextPageUrl() |
| 185 |
{ |
| 186 |
if ($this->hasMorePages()) { |
| 187 |
return $this->url($this->currentPage() + 1); |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
public function lastPage() |
| 197 |
{ |
| 198 |
return $this->lastPage; |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
public function toArray() |
| 207 |
{ |
| 208 |
return [ |
| 209 |
'current_page' => $this->currentPage(), |
| 210 |
'data' => $this->items->toArray(), |
| 211 |
'first_page_url' => $this->url(1), |
| 212 |
'from' => $this->firstItem(), |
| 213 |
'last_page' => $this->lastPage(), |
| 214 |
'last_page_url' => $this->url($this->lastPage()), |
| 215 |
'links' => $this->linkCollection()->toArray(), |
| 216 |
'next_page_url' => $this->nextPageUrl(), |
| 217 |
'path' => $this->path(), |
| 218 |
'per_page' => $this->perPage(), |
| 219 |
'prev_page_url' => $this->previousPageUrl(), |
| 220 |
'to' => $this->lastItem(), |
| 221 |
'total' => $this->total(), |
| 222 |
]; |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
public function jsonSerialize(): array |
| 231 |
{ |
| 232 |
return $this->toArray(); |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
public function toJson($options = 0) |
| 242 |
{ |
| 243 |
return json_encode($this->jsonSerialize(), $options); |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
public function toPrettyJson(int $options = 0) |
| 254 |
{ |
| 255 |
return $this->toJson(JSON_PRETTY_PRINT | $options); |
| 256 |
} |
| 257 |
} |