| 1 |
<?php |
| 2 |
|
| 3 |
namespace Illuminate\Pagination; |
| 4 |
|
| 5 |
use ArrayAccess; |
| 6 |
use Countable; |
| 7 |
use Illuminate\Contracts\Pagination\CursorPaginator 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 CursorPaginator extends AbstractCursorPaginator 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, $cursor = null, array $options = []) |
| 44 |
{ |
| 45 |
$this->options = $options; |
| 46 |
|
| 47 |
foreach ($options as $key => $value) { |
| 48 |
$this->{$key} = $value; |
| 49 |
} |
| 50 |
|
| 51 |
$this->perPage = (int) $perPage; |
| 52 |
$this->cursor = $cursor; |
| 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 setItems($items) |
| 65 |
{ |
| 66 |
$this->items = $items instanceof Collection ? $items : new Collection($items); |
| 67 |
|
| 68 |
$this->hasMore = $this->items->count() > $this->perPage; |
| 69 |
|
| 70 |
$this->items = $this->items->slice(0, $this->perPage); |
| 71 |
|
| 72 |
if (! is_null($this->cursor) && $this->cursor->pointsToPreviousItems()) { |
| 73 |
$this->items = $this->items->reverse()->values(); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
public function links($view = null, $data = []) |
| 85 |
{ |
| 86 |
return $this->render($view, $data); |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
public function render($view = null, $data = []) |
| 97 |
{ |
| 98 |
return static::viewFactory()->make($view ?: Paginator::$defaultSimpleView, array_merge($data, [ |
| 99 |
'paginator' => $this, |
| 100 |
])); |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
public function hasMorePages() |
| 109 |
{ |
| 110 |
return (is_null($this->cursor) && $this->hasMore) || |
| 111 |
(! is_null($this->cursor) && $this->cursor->pointsToNextItems() && $this->hasMore) || |
| 112 |
(! is_null($this->cursor) && $this->cursor->pointsToPreviousItems()); |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
public function hasPages() |
| 121 |
{ |
| 122 |
return ! $this->onFirstPage() || $this->hasMorePages(); |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
public function onFirstPage() |
| 131 |
{ |
| 132 |
return is_null($this->cursor) || ($this->cursor->pointsToPreviousItems() && ! $this->hasMore); |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
public function onLastPage() |
| 141 |
{ |
| 142 |
return ! $this->hasMorePages(); |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
public function toArray() |
| 151 |
{ |
| 152 |
return [ |
| 153 |
'data' => $this->items->toArray(), |
| 154 |
'path' => $this->path(), |
| 155 |
'per_page' => $this->perPage(), |
| 156 |
'next_cursor' => $this->nextCursor()?->encode(), |
| 157 |
'next_page_url' => $this->nextPageUrl(), |
| 158 |
'prev_cursor' => $this->previousCursor()?->encode(), |
| 159 |
'prev_page_url' => $this->previousPageUrl(), |
| 160 |
]; |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
public function jsonSerialize(): array |
| 169 |
{ |
| 170 |
return $this->toArray(); |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
public function toJson($options = 0) |
| 180 |
{ |
| 181 |
return json_encode($this->jsonSerialize(), $options); |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
public function toPrettyJson(int $options = 0) |
| 192 |
{ |
| 193 |
return $this->toJson(JSON_PRETTY_PRINT | $options); |
| 194 |
} |
| 195 |
} |