| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCommunity\Framework\Pagination; |
| 4 |
|
| 5 |
use Countable; |
| 6 |
use ArrayAccess; |
| 7 |
use JsonSerializable; |
| 8 |
use IteratorAggregate; |
| 9 |
use FluentCommunity\Framework\Support\Collection; |
| 10 |
use FluentCommunity\Framework\Support\JsonableInterface; |
| 11 |
use FluentCommunity\Framework\Support\ArrayableInterface; |
| 12 |
use FluentCommunity\Framework\Pagination\PaginatorInterface; |
| 13 |
|
| 14 |
class Paginator extends AbstractPaginator implements ArrayableInterface, ArrayAccess, Countable, IteratorAggregate, JsonSerializable, JsonableInterface, PaginatorInterface |
| 15 |
{ |
| 16 |
/** |
| 17 |
* Determine if there are more items in the data source. |
| 18 |
* |
| 19 |
* @return bool |
| 20 |
*/ |
| 21 |
protected $hasMore; |
| 22 |
|
| 23 |
/** |
| 24 |
* Create a new paginator instance. |
| 25 |
* |
| 26 |
* @param mixed $items |
| 27 |
* @param int $perPage |
| 28 |
* @param int|null $currentPage |
| 29 |
* @param array $options (path, query, fragment, pageName) |
| 30 |
* @return void |
| 31 |
*/ |
| 32 |
public function __construct($items, $perPage, $currentPage = null, array $options = []) |
| 33 |
{ |
| 34 |
$this->options = $options; |
| 35 |
|
| 36 |
foreach ($options as $key => $value) { |
| 37 |
$this->{$key} = $value; |
| 38 |
} |
| 39 |
|
| 40 |
$this->perPage = $perPage; |
| 41 |
$this->currentPage = $this->setCurrentPage($currentPage); |
| 42 |
$this->path = $this->path !== '/' ? rtrim($this->path, '/') : $this->path; |
| 43 |
|
| 44 |
$this->setItems($items); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Get the current page for the request. |
| 49 |
* |
| 50 |
* @param int $currentPage |
| 51 |
* @return int |
| 52 |
*/ |
| 53 |
protected function setCurrentPage($currentPage) |
| 54 |
{ |
| 55 |
$currentPage = $currentPage ?: static::resolveCurrentPage(); |
| 56 |
|
| 57 |
return $this->isValidPageNumber($currentPage) ? (int) $currentPage : 1; |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Set the items for the paginator. |
| 62 |
* |
| 63 |
* @param mixed $items |
| 64 |
* @return void |
| 65 |
*/ |
| 66 |
protected function setItems($items) |
| 67 |
{ |
| 68 |
$this->items = $items instanceof Collection ? $items : Collection::make($items); |
| 69 |
|
| 70 |
$this->hasMore = $this->items->count() > $this->perPage; |
| 71 |
|
| 72 |
$this->items = $this->items->slice(0, $this->perPage); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Get the URL for the next page. |
| 77 |
* |
| 78 |
* @return string|null |
| 79 |
*/ |
| 80 |
public function nextPageUrl() |
| 81 |
{ |
| 82 |
if ($this->hasMorePages()) { |
| 83 |
return $this->url($this->currentPage() + 1); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Manually indicate that the paginator does have more pages. |
| 89 |
* |
| 90 |
* @param bool $hasMore |
| 91 |
* @return $this |
| 92 |
*/ |
| 93 |
public function hasMorePagesWhen($hasMore = true) |
| 94 |
{ |
| 95 |
$this->hasMore = $hasMore; |
| 96 |
|
| 97 |
return $this; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Determine if there are more items in the data source. |
| 102 |
* |
| 103 |
* @return bool |
| 104 |
*/ |
| 105 |
public function hasMorePages() |
| 106 |
{ |
| 107 |
return $this->hasMore; |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Get the instance as an array. |
| 112 |
* |
| 113 |
* @return array |
| 114 |
*/ |
| 115 |
public function toArray() |
| 116 |
{ |
| 117 |
return [ |
| 118 |
'current_page' => $this->currentPage(), |
| 119 |
'data' => $this->items->toArray(), |
| 120 |
'first_page_url' => $this->url(1), |
| 121 |
'from' => $this->firstItem(), |
| 122 |
'next_page_url' => $this->nextPageUrl(), |
| 123 |
'path' => $this->path(), |
| 124 |
'per_page' => $this->perPage(), |
| 125 |
'prev_page_url' => $this->previousPageUrl(), |
| 126 |
'to' => $this->lastItem(), |
| 127 |
]; |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Convert the object into something JSON serializable. |
| 132 |
* |
| 133 |
* @return array |
| 134 |
*/ |
| 135 |
#[\ReturnTypeWillChange] |
| 136 |
public function jsonSerialize() |
| 137 |
{ |
| 138 |
return $this->toArray(); |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Convert the object to its JSON representation. |
| 143 |
* |
| 144 |
* @param int $options |
| 145 |
* @return string |
| 146 |
*/ |
| 147 |
public function toJson($options = 0) |
| 148 |
{ |
| 149 |
return json_encode($this->jsonSerialize(), $options); |
| 150 |
} |
| 151 |
} |
| 152 |
|