Debian Codegraph @

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

raw symbols SCIP
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 * @template TKey of array-key
16 *
17 * @template-covariant TValue
18 *
19 * @extends AbstractPaginator<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 Paginator extends AbstractPaginator implements Arrayable, ArrayAccess, Countable, IteratorAggregate, Jsonable, JsonSerializable, PaginatorContract
27 {
28 /**
29 * Determine if 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> $items
39 * @param int $perPage
40 * @param int|null $currentPage
41 * @param array $options (path, query, fragment, pageName)
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 * Get the current page for the request.
60 *
61 * @param int $currentPage
62 * @return int
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 * Set the items for the paginator.
73 *
74 * @param Collection<TKey, TValue>|Arrayable<TKey, TValue>|iterable<TKey, TValue>|null $items
75 * @return void
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 * Get the URL for the next page.
88 *
89 * @return string|null
90 */
91 public function nextPageUrl()
92 {
93 if ($this->hasMorePages()) {
94 return $this->url($this->currentPage() + 1);
95 }
96 }
97
98 /**
99 * Render the paginator using the given view.
100 *
101 * @param string|null $view
102 * @param array $data
103 * @return string
104 */
105 public function links($view = null, $data = [])
106 {
107 return $this->render($view, $data);
108 }
109
110 /**
111 * Render the paginator using the given view.
112 *
113 * @param string|null $view
114 * @param array $data
115 * @return \Illuminate\Contracts\Support\Htmlable
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 * Manually indicate that the paginator does have more pages.
126 *
127 * @param bool $hasMore
128 * @return $this
129 */
130 public function hasMorePagesWhen($hasMore = true)
131 {
132 $this->hasMore = $hasMore;
133
134 return $this;
135 }
136
137 /**
138 * Determine if there are more items in the data source.
139 *
140 * @return bool
141 */
142 public function hasMorePages()
143 {
144 return $this->hasMore;
145 }
146
147 /**
148 * Get the instance as an array.
149 *
150 * @return array
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 * Convert the object into something JSON serializable.
170 *
171 * @return array
172 */
173 public function jsonSerialize(): array
174 {
175 return $this->toArray();
176 }
177
178 /**
179 * Convert the object to its JSON representation.
180 *
181 * @param int $options
182 * @return string
183 */
184 public function toJson($options = 0)
185 {
186 return json_encode($this->jsonSerialize(), $options);
187 }
188
189 /**
190 * Convert the object to pretty print formatted JSON.
191 *
192 * @params int $options
193 *
194 * @return string
195 */
196 public function toPrettyJson(int $options = 0)
197 {
198 return $this->toJson(JSON_PRETTY_PRINT | $options);
199 }
200 }