Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse per_page query params from request #14

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions AbstractCursorPaginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ abstract class AbstractCursorPaginator implements Htmlable, Stringable
*/
protected $perPage;

/**
* The query string variable used to store the per page.
*
* @var string
*/
protected $perPageName = 'per_page';

/**
* The base path to assign to all URLs.
*
Expand Down Expand Up @@ -94,6 +101,13 @@ abstract class AbstractCursorPaginator implements Htmlable, Stringable
*/
protected static $currentCursorResolver;

/**
* The per page resolver callback.
*
* @var \Closure
*/
protected static $perPageResolver;

/**
* Get the URL for a given cursor.
*
Expand All @@ -117,6 +131,44 @@ public function url($cursor)
.$this->buildFragment();
}

/**
* Resolve the per page or return the default value.
*
* @param string $perPageName
* @param int $default
* @return int
*/
public static function resolvePerPage($perPageName = 'per_page', $default = 15)
{
if (isset(static::$perPageResolver)) {
return (int) call_user_func(static::$perPageResolver, $perPageName);
}

return $default;
}

/**
* Set the per page resolver callback.
*
* @param \Closure $resolver
* @return void
*/
public static function perPageResolver(Closure $resolver)
{
static::$perPageResolver = $resolver;
}

/**
* Determine if the given value is a valid per page number.
*
* @param int $perPage
* @return bool
*/
protected function isValidPerPageNumber($perPage)
{
return $perPage > 0 && filter_var($perPage, FILTER_VALIDATE_INT) !== false;
}

/**
* Get the URL for the previous page.
*
Expand Down
52 changes: 52 additions & 0 deletions AbstractPaginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ abstract class AbstractPaginator implements Htmlable, Stringable
*/
protected $pageName = 'page';

/**
* The query string variable used to store the per page.
*
* @var string
*/
protected $perPageName = 'per_page';

/**
* The number of links to display on each side of current page link.
*
Expand Down Expand Up @@ -95,6 +102,13 @@ abstract class AbstractPaginator implements Htmlable, Stringable
*/
protected static $currentPageResolver;

/**
* The per page resolver callback.
*
* @var \Closure
*/
protected static $perPageResolver;

/**
* The query string resolver callback.
*
Expand Down Expand Up @@ -134,6 +148,17 @@ protected function isValidPageNumber($page)
return $page >= 1 && filter_var($page, FILTER_VALIDATE_INT) !== false;
}

/**
* Determine if the given value is a valid per page number.
*
* @param int $perPage
* @return bool
*/
protected function isValidPerPageNumber($perPage)
{
return $perPage > 0 && filter_var($perPage, FILTER_VALIDATE_INT) !== false;
}

/**
* Get the URL for the previous page.
*
Expand Down Expand Up @@ -523,6 +548,33 @@ public static function currentPageResolver(Closure $resolver)
static::$currentPageResolver = $resolver;
}

/**
* Resolve the per page or return the default value.
*
* @param string $perPageName
* @param int $default
* @return int
*/
public static function resolvePerPage($perPageName = 'per_page', $default = 15)
{
if (isset(static::$perPageResolver)) {
return (int) call_user_func(static::$perPageResolver, $perPageName);
}

return $default;
}

/**
* Set the per page resolver callback.
*
* @param \Closure $resolver
* @return void
*/
public static function perPageResolver(Closure $resolver)
{
static::$perPageResolver = $resolver;
}

/**
* Resolve the query string or return the default value.
*
Expand Down
20 changes: 17 additions & 3 deletions CursorPaginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,40 @@ class CursorPaginator extends AbstractCursorPaginator implements Arrayable, Arra
* Create a new paginator instance.
*
* @param mixed $items
* @param int $perPage
* @param int|null $perPage
* @param \Illuminate\Pagination\Cursor|null $cursor
* @param array $options (path, query, fragment, pageName)
* @return void
*/
public function __construct($items, $perPage, $cursor = null, array $options = [])
public function __construct($items, $perPage = null, $cursor = null, array $options = [])
{
$this->options = $options;

foreach ($options as $key => $value) {
$this->{$key} = $value;
}

$this->perPage = (int) $perPage;
$this->perPage = $this->setPerPage($perPage, $this->perPageName);
$this->cursor = $cursor;
$this->path = $this->path !== '/' ? rtrim($this->path, '/') : $this->path;

$this->setItems($items);
}

/**
* Get the per page for the request.
*
* @param int $perPage
* @param string $perPageName
* @return int
*/
protected function setPerPage($perPage, $perPageName)
{
$perPage = $perPage ?: static::resolvePerPage($perPageName);

return $this->isValidPerPageNumber($perPage) ? (int) $perPage : 15;
}

/**
* Set the items for the paginator.
*
Expand Down
20 changes: 17 additions & 3 deletions LengthAwarePaginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ class LengthAwarePaginator extends AbstractPaginator implements Arrayable, Array
*
* @param mixed $items
* @param int $total
* @param int $perPage
* @param int|null $perPage
* @param int|null $currentPage
* @param array $options (path, query, fragment, pageName)
* @return void
*/
public function __construct($items, $total, $perPage, $currentPage = null, array $options = [])
public function __construct($items, $total, $perPage = null, $currentPage = null, array $options = [])
{
$this->options = $options;

Expand All @@ -46,7 +46,7 @@ public function __construct($items, $total, $perPage, $currentPage = null, array
}

$this->total = $total;
$this->perPage = (int) $perPage;
$this->perPage = $this->setPerPage($perPage, $this->perPageName);
$this->lastPage = max((int) ceil($total / $perPage), 1);
$this->path = $this->path !== '/' ? rtrim($this->path, '/') : $this->path;
$this->currentPage = $this->setCurrentPage($currentPage, $this->pageName);
Expand All @@ -67,6 +67,20 @@ protected function setCurrentPage($currentPage, $pageName)
return $this->isValidPageNumber($currentPage) ? (int) $currentPage : 1;
}

/**
* Get the per page for the request.
*
* @param int $perPage
* @param string $perPageName
* @return int
*/
protected function setPerPage($perPage, $perPageName)
{
$perPage = $perPage ?: static::resolvePerPage($perPageName);

return $this->isValidPerPageNumber($perPage) ? (int) $perPage : 15;
}

/**
* Render the paginator using the given view.
*
Expand Down
10 changes: 10 additions & 0 deletions PaginationState.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ public static function resolveUsing($app)
return 1;
});

Paginator::perPageResolver(function ($perPageName = 'per_page') use ($app) {
$perPage = $app['request']->input($perPageName);

if (filter_var($perPage, FILTER_VALIDATE_INT) !== false && (int) $perPage > 0) {
return (int) $perPage;
}

return 15;
});

Paginator::queryStringResolver(fn () => $app['request']->query());

CursorPaginator::currentCursorResolver(function ($cursorName = 'cursor') use ($app) {
Expand Down
19 changes: 16 additions & 3 deletions Paginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,20 @@ class Paginator extends AbstractPaginator implements Arrayable, ArrayAccess, Cou
* Create a new paginator instance.
*
* @param mixed $items
* @param int $perPage
* @param int|null $perPage
* @param int|null $currentPage
* @param array $options (path, query, fragment, pageName)
* @return void
*/
public function __construct($items, $perPage, $currentPage = null, array $options = [])
public function __construct($items, $perPage = null, $currentPage = null, array $options = [])
{
$this->options = $options;

foreach ($options as $key => $value) {
$this->{$key} = $value;
}

$this->perPage = $perPage;
$this->perPage = $this->setPerPage($perPage);
$this->currentPage = $this->setCurrentPage($currentPage);
$this->path = $this->path !== '/' ? rtrim($this->path, '/') : $this->path;

Expand All @@ -57,6 +57,19 @@ protected function setCurrentPage($currentPage)
return $this->isValidPageNumber($currentPage) ? (int) $currentPage : 1;
}

/**
* Get the per page for the request.
*
* @param int $perPage
* @return int
*/
protected function setPerPage($perPage)
{
$perPage = $perPage ?: static::resolvePerPage();

return $this->isValidPerPageNumber($perPage) ? (int) $perPage : 15;
}

/**
* Set the items for the paginator.
*
Expand Down
Loading