-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 15bf2bd
Showing
12 changed files
with
1,525 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) Taylor Otwell | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,201 @@ | ||
<?php | ||
|
||
namespace IgniteKit\Backports\Pagination; | ||
|
||
use Countable; | ||
use ArrayAccess; | ||
use JsonSerializable; | ||
use IteratorAggregate; | ||
use IgniteKit\Backports\Support\Collection; | ||
use IgniteKit\Backports\Support\HtmlString; | ||
use IgniteKit\Backports\Contracts\Support\Jsonable; | ||
use IgniteKit\Backports\Contracts\Support\Arrayable; | ||
use IgniteKit\Backports\Contracts\Pagination\LengthAwarePaginator as LengthAwarePaginatorContract; | ||
|
||
class LengthAwarePaginator extends AbstractPaginator implements Arrayable, ArrayAccess, Countable, IteratorAggregate, JsonSerializable, Jsonable, LengthAwarePaginatorContract | ||
{ | ||
/** | ||
* The total number of items before slicing. | ||
* | ||
* @var int | ||
*/ | ||
protected $total; | ||
|
||
/** | ||
* The last available page. | ||
* | ||
* @var int | ||
*/ | ||
protected $lastPage; | ||
|
||
/** | ||
* Create a new paginator instance. | ||
* | ||
* @param mixed $items | ||
* @param int $total | ||
* @param int $perPage | ||
* @param int|null $currentPage | ||
* @param array $options (path, query, fragment, pageName) | ||
* @return void | ||
*/ | ||
public function __construct($items, $total, $perPage, $currentPage = null, array $options = []) | ||
{ | ||
$this->options = $options; | ||
|
||
foreach ($options as $key => $value) { | ||
$this->{$key} = $value; | ||
} | ||
|
||
$this->total = $total; | ||
$this->perPage = $perPage; | ||
$this->lastPage = max((int) ceil($total / $perPage), 1); | ||
$this->path = $this->path !== '/' ? rtrim($this->path, '/') : $this->path; | ||
$this->currentPage = $this->setCurrentPage($currentPage, $this->pageName); | ||
$this->items = $items instanceof Collection ? $items : Collection::make($items); | ||
} | ||
|
||
/** | ||
* Get the current page for the request. | ||
* | ||
* @param int $currentPage | ||
* @param string $pageName | ||
* @return int | ||
*/ | ||
protected function setCurrentPage($currentPage, $pageName) | ||
{ | ||
$currentPage = $currentPage ?: static::resolveCurrentPage($pageName); | ||
|
||
return $this->isValidPageNumber($currentPage) ? (int) $currentPage : 1; | ||
} | ||
|
||
/** | ||
* Render the paginator using the given view. | ||
* | ||
* @param string|null $view | ||
* @param array $data | ||
* @return \IgniteKit\Backports\Support\HtmlString | ||
*/ | ||
public function links($view = null, $data = []) | ||
{ | ||
return $this->render($view, $data); | ||
} | ||
|
||
/** | ||
* Render the paginator using the given view. | ||
* | ||
* @param string|null $view | ||
* @param array $data | ||
* @return \IgniteKit\Backports\Support\HtmlString | ||
*/ | ||
public function render($view = null, $data = []) | ||
{ | ||
return new HtmlString(static::viewFactory()->make($view ?: static::$defaultView, array_merge($data, [ | ||
'paginator' => $this, | ||
'elements' => $this->elements(), | ||
]))->render()); | ||
} | ||
|
||
/** | ||
* Get the array of elements to pass to the view. | ||
* | ||
* @return array | ||
*/ | ||
protected function elements() | ||
{ | ||
$window = UrlWindow::make($this); | ||
|
||
return array_filter([ | ||
$window['first'], | ||
is_array($window['slider']) ? '...' : null, | ||
$window['slider'], | ||
is_array($window['last']) ? '...' : null, | ||
$window['last'], | ||
]); | ||
} | ||
|
||
/** | ||
* Get the total number of items being paginated. | ||
* | ||
* @return int | ||
*/ | ||
public function total() | ||
{ | ||
return $this->total; | ||
} | ||
|
||
/** | ||
* Determine if there are more items in the data source. | ||
* | ||
* @return bool | ||
*/ | ||
public function hasMorePages() | ||
{ | ||
return $this->currentPage() < $this->lastPage(); | ||
} | ||
|
||
/** | ||
* Get the URL for the next page. | ||
* | ||
* @return string|null | ||
*/ | ||
public function nextPageUrl() | ||
{ | ||
if ($this->lastPage() > $this->currentPage()) { | ||
return $this->url($this->currentPage() + 1); | ||
} | ||
} | ||
|
||
/** | ||
* Get the last page. | ||
* | ||
* @return int | ||
*/ | ||
public function lastPage() | ||
{ | ||
return $this->lastPage; | ||
} | ||
|
||
/** | ||
* Get the instance as an array. | ||
* | ||
* @return array | ||
*/ | ||
public function toArray() | ||
{ | ||
return [ | ||
'current_page' => $this->currentPage(), | ||
'data' => $this->items->toArray(), | ||
'first_page_url' => $this->url(1), | ||
'from' => $this->firstItem(), | ||
'last_page' => $this->lastPage(), | ||
'last_page_url' => $this->url($this->lastPage()), | ||
'next_page_url' => $this->nextPageUrl(), | ||
'path' => $this->path, | ||
'per_page' => $this->perPage(), | ||
'prev_page_url' => $this->previousPageUrl(), | ||
'to' => $this->lastItem(), | ||
'total' => $this->total(), | ||
]; | ||
} | ||
|
||
/** | ||
* Convert the object into something JSON serializable. | ||
* | ||
* @return array | ||
*/ | ||
public function jsonSerialize() | ||
{ | ||
return $this->toArray(); | ||
} | ||
|
||
/** | ||
* Convert the object to its JSON representation. | ||
* | ||
* @param int $options | ||
* @return string | ||
*/ | ||
public function toJson($options = 0) | ||
{ | ||
return json_encode($this->jsonSerialize(), $options); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
namespace IgniteKit\Backports\Pagination; | ||
|
||
use IgniteKit\Backports\Support\ServiceProvider; | ||
|
||
class PaginationServiceProvider extends ServiceProvider | ||
{ | ||
/** | ||
* Bootstrap any application services. | ||
* | ||
* @return void | ||
*/ | ||
public function boot() | ||
{ | ||
$this->loadViewsFrom(__DIR__.'/resources/views', 'pagination'); | ||
|
||
if ($this->app->runningInConsole()) { | ||
$this->publishes([ | ||
__DIR__.'/resources/views' => $this->app->resourcePath('views/vendor/pagination'), | ||
], 'laravel-pagination'); | ||
} | ||
} | ||
|
||
/** | ||
* Register the service provider. | ||
* | ||
* @return void | ||
*/ | ||
public function register() | ||
{ | ||
Paginator::viewFactoryResolver(function () { | ||
return $this->app['view']; | ||
}); | ||
|
||
Paginator::currentPathResolver(function () { | ||
return $this->app['request']->url(); | ||
}); | ||
|
||
Paginator::currentPageResolver(function ($pageName = 'page') { | ||
$page = $this->app['request']->input($pageName); | ||
|
||
if (filter_var($page, FILTER_VALIDATE_INT) !== false && (int) $page >= 1) { | ||
return (int) $page; | ||
} | ||
|
||
return 1; | ||
}); | ||
} | ||
} |
Oops, something went wrong.