Debian Codegraph @

lib/3rdParty/composer/illuminate/pagination/CursorPaginator.php

raw symbols SCIP
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 * @template TKey of array-key
16 *
17 * @template-covariant TValue
18 *
19 * @extends AbstractCursorPaginator<TKey, TValue>
20 *
21 * @implements Arrayable<TKey, TValue>
22 * @implements ArrayAccess<TKey, TValue>
23 * @implements IteratorAggregate<TKey, TValue>
24 * @implements PaginatorContract<TKey, TValue>
25 */
26 class CursorPaginator extends AbstractCursorPaginator implements Arrayable, ArrayAccess, Countable, IteratorAggregate, Jsonable, JsonSerializable, PaginatorContract
27 {
28 /**
29 * Indicates whether there are more items in the data source.
30 *
31 * @return bool
32 */
33 protected $hasMore;
34
35 /**
36 * Create a new paginator instance.
37 *
38 * @param Collection<TKey, TValue>|Arrayable<TKey, TValue>|iterable<TKey, TValue>|null $items
39 * @param int $perPage
40 * @param \Illuminate\Pagination\Cursor|null $cursor
41 * @param array $options (path, query, fragment, pageName)
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 * Set the items for the paginator.
60 *
61 * @param Collection<TKey, TValue>|Arrayable<TKey, TValue>|iterable<TKey, TValue>|null $items
62 * @return void
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 * Render the paginator using the given view.
79 *
80 * @param string|null $view
81 * @param array $data
82 * @return \Illuminate\Contracts\Support\Htmlable
83 */
84 public function links($view = null, $data = [])
85 {
86 return $this->render($view, $data);
87 }
88
89 /**
90 * Render the paginator using the given view.
91 *
92 * @param string|null $view
93 * @param array $data
94 * @return \Illuminate\Contracts\Support\Htmlable
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 * Determine if there are more items in the data source.
105 *
106 * @return bool
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 * Determine if there are enough items to split into multiple pages.
117 *
118 * @return bool
119 */
120 public function hasPages()
121 {
122 return ! $this->onFirstPage() || $this->hasMorePages();
123 }
124
125 /**
126 * Determine if the paginator is on the first page.
127 *
128 * @return bool
129 */
130 public function onFirstPage()
131 {
132 return is_null($this->cursor) || ($this->cursor->pointsToPreviousItems() && ! $this->hasMore);
133 }
134
135 /**
136 * Determine if the paginator is on the last page.
137 *
138 * @return bool
139 */
140 public function onLastPage()
141 {
142 return ! $this->hasMorePages();
143 }
144
145 /**
146 * Get the instance as an array.
147 *
148 * @return array
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 * Convert the object into something JSON serializable.
165 *
166 * @return array
167 */
168 public function jsonSerialize(): array
169 {
170 return $this->toArray();
171 }
172
173 /**
174 * Convert the object to its JSON representation.
175 *
176 * @param int $options
177 * @return string
178 */
179 public function toJson($options = 0)
180 {
181 return json_encode($this->jsonSerialize(), $options);
182 }
183
184 /**
185 * Convert the object to pretty print formatted JSON.
186 *
187 * @params int $options
188 *
189 * @return string
190 */
191 public function toPrettyJson(int $options = 0)
192 {
193 return $this->toJson(JSON_PRETTY_PRINT | $options);
194 }
195 }