Skip to content

Commit 15bf2bd

Browse files
committed
Init
0 parents  commit 15bf2bd

12 files changed

+1525
-0
lines changed

AbstractPaginator.php

Lines changed: 659 additions & 0 deletions
Large diffs are not rendered by default.

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) Taylor Otwell
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

LengthAwarePaginator.php

Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
<?php
2+
3+
namespace IgniteKit\Backports\Pagination;
4+
5+
use Countable;
6+
use ArrayAccess;
7+
use JsonSerializable;
8+
use IteratorAggregate;
9+
use IgniteKit\Backports\Support\Collection;
10+
use IgniteKit\Backports\Support\HtmlString;
11+
use IgniteKit\Backports\Contracts\Support\Jsonable;
12+
use IgniteKit\Backports\Contracts\Support\Arrayable;
13+
use IgniteKit\Backports\Contracts\Pagination\LengthAwarePaginator as LengthAwarePaginatorContract;
14+
15+
class LengthAwarePaginator extends AbstractPaginator implements Arrayable, ArrayAccess, Countable, IteratorAggregate, JsonSerializable, Jsonable, LengthAwarePaginatorContract
16+
{
17+
/**
18+
* The total number of items before slicing.
19+
*
20+
* @var int
21+
*/
22+
protected $total;
23+
24+
/**
25+
* The last available page.
26+
*
27+
* @var int
28+
*/
29+
protected $lastPage;
30+
31+
/**
32+
* Create a new paginator instance.
33+
*
34+
* @param mixed $items
35+
* @param int $total
36+
* @param int $perPage
37+
* @param int|null $currentPage
38+
* @param array $options (path, query, fragment, pageName)
39+
* @return void
40+
*/
41+
public function __construct($items, $total, $perPage, $currentPage = null, array $options = [])
42+
{
43+
$this->options = $options;
44+
45+
foreach ($options as $key => $value) {
46+
$this->{$key} = $value;
47+
}
48+
49+
$this->total = $total;
50+
$this->perPage = $perPage;
51+
$this->lastPage = max((int) ceil($total / $perPage), 1);
52+
$this->path = $this->path !== '/' ? rtrim($this->path, '/') : $this->path;
53+
$this->currentPage = $this->setCurrentPage($currentPage, $this->pageName);
54+
$this->items = $items instanceof Collection ? $items : Collection::make($items);
55+
}
56+
57+
/**
58+
* Get the current page for the request.
59+
*
60+
* @param int $currentPage
61+
* @param string $pageName
62+
* @return int
63+
*/
64+
protected function setCurrentPage($currentPage, $pageName)
65+
{
66+
$currentPage = $currentPage ?: static::resolveCurrentPage($pageName);
67+
68+
return $this->isValidPageNumber($currentPage) ? (int) $currentPage : 1;
69+
}
70+
71+
/**
72+
* Render the paginator using the given view.
73+
*
74+
* @param string|null $view
75+
* @param array $data
76+
* @return \IgniteKit\Backports\Support\HtmlString
77+
*/
78+
public function links($view = null, $data = [])
79+
{
80+
return $this->render($view, $data);
81+
}
82+
83+
/**
84+
* Render the paginator using the given view.
85+
*
86+
* @param string|null $view
87+
* @param array $data
88+
* @return \IgniteKit\Backports\Support\HtmlString
89+
*/
90+
public function render($view = null, $data = [])
91+
{
92+
return new HtmlString(static::viewFactory()->make($view ?: static::$defaultView, array_merge($data, [
93+
'paginator' => $this,
94+
'elements' => $this->elements(),
95+
]))->render());
96+
}
97+
98+
/**
99+
* Get the array of elements to pass to the view.
100+
*
101+
* @return array
102+
*/
103+
protected function elements()
104+
{
105+
$window = UrlWindow::make($this);
106+
107+
return array_filter([
108+
$window['first'],
109+
is_array($window['slider']) ? '...' : null,
110+
$window['slider'],
111+
is_array($window['last']) ? '...' : null,
112+
$window['last'],
113+
]);
114+
}
115+
116+
/**
117+
* Get the total number of items being paginated.
118+
*
119+
* @return int
120+
*/
121+
public function total()
122+
{
123+
return $this->total;
124+
}
125+
126+
/**
127+
* Determine if there are more items in the data source.
128+
*
129+
* @return bool
130+
*/
131+
public function hasMorePages()
132+
{
133+
return $this->currentPage() < $this->lastPage();
134+
}
135+
136+
/**
137+
* Get the URL for the next page.
138+
*
139+
* @return string|null
140+
*/
141+
public function nextPageUrl()
142+
{
143+
if ($this->lastPage() > $this->currentPage()) {
144+
return $this->url($this->currentPage() + 1);
145+
}
146+
}
147+
148+
/**
149+
* Get the last page.
150+
*
151+
* @return int
152+
*/
153+
public function lastPage()
154+
{
155+
return $this->lastPage;
156+
}
157+
158+
/**
159+
* Get the instance as an array.
160+
*
161+
* @return array
162+
*/
163+
public function toArray()
164+
{
165+
return [
166+
'current_page' => $this->currentPage(),
167+
'data' => $this->items->toArray(),
168+
'first_page_url' => $this->url(1),
169+
'from' => $this->firstItem(),
170+
'last_page' => $this->lastPage(),
171+
'last_page_url' => $this->url($this->lastPage()),
172+
'next_page_url' => $this->nextPageUrl(),
173+
'path' => $this->path,
174+
'per_page' => $this->perPage(),
175+
'prev_page_url' => $this->previousPageUrl(),
176+
'to' => $this->lastItem(),
177+
'total' => $this->total(),
178+
];
179+
}
180+
181+
/**
182+
* Convert the object into something JSON serializable.
183+
*
184+
* @return array
185+
*/
186+
public function jsonSerialize()
187+
{
188+
return $this->toArray();
189+
}
190+
191+
/**
192+
* Convert the object to its JSON representation.
193+
*
194+
* @param int $options
195+
* @return string
196+
*/
197+
public function toJson($options = 0)
198+
{
199+
return json_encode($this->jsonSerialize(), $options);
200+
}
201+
}

PaginationServiceProvider.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace IgniteKit\Backports\Pagination;
4+
5+
use IgniteKit\Backports\Support\ServiceProvider;
6+
7+
class PaginationServiceProvider extends ServiceProvider
8+
{
9+
/**
10+
* Bootstrap any application services.
11+
*
12+
* @return void
13+
*/
14+
public function boot()
15+
{
16+
$this->loadViewsFrom(__DIR__.'/resources/views', 'pagination');
17+
18+
if ($this->app->runningInConsole()) {
19+
$this->publishes([
20+
__DIR__.'/resources/views' => $this->app->resourcePath('views/vendor/pagination'),
21+
], 'laravel-pagination');
22+
}
23+
}
24+
25+
/**
26+
* Register the service provider.
27+
*
28+
* @return void
29+
*/
30+
public function register()
31+
{
32+
Paginator::viewFactoryResolver(function () {
33+
return $this->app['view'];
34+
});
35+
36+
Paginator::currentPathResolver(function () {
37+
return $this->app['request']->url();
38+
});
39+
40+
Paginator::currentPageResolver(function ($pageName = 'page') {
41+
$page = $this->app['request']->input($pageName);
42+
43+
if (filter_var($page, FILTER_VALIDATE_INT) !== false && (int) $page >= 1) {
44+
return (int) $page;
45+
}
46+
47+
return 1;
48+
});
49+
}
50+
}

0 commit comments

Comments
 (0)