From c716a38d965bcbcb2f1b96c6f938274408a579c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nil=20Portugu=C3=A9s=20Calder=C3=B3?= Date: Thu, 17 Dec 2015 00:42:57 +0100 Subject: [PATCH] Adding page size for listResource --- src/Http/Request/Parameters/Page.php | 8 +++++++ src/Server/Actions/ListResource.php | 36 +++++++++++++++++----------- 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/src/Http/Request/Parameters/Page.php b/src/Http/Request/Parameters/Page.php index d21ff0a..5c91580 100644 --- a/src/Http/Request/Parameters/Page.php +++ b/src/Http/Request/Parameters/Page.php @@ -52,6 +52,14 @@ public function __construct($number, $cursor, $limit, $offset, $size) $this->size = $size; } + /** + * @param $size + */ + public function setSize($size) + { + $this->size = (int) $size; + } + /** * @return int|string */ diff --git a/src/Server/Actions/ListResource.php b/src/Server/Actions/ListResource.php index 81f9111..bd4c007 100644 --- a/src/Server/Actions/ListResource.php +++ b/src/Server/Actions/ListResource.php @@ -110,7 +110,7 @@ public function get(callable $totalAmountCallable, callable $resultsCallable, $r ); $totalAmount = $totalAmountCallable(); - if ($totalAmount > 0 && $this->page->size()>0 && $this->page->number() > ceil($totalAmount / $this->page->size())) { + if ($totalAmount > 0 && $this->page->size() > 0 && $this->page->number() > ceil($totalAmount / $this->page->size())) { return $this->resourceNotFound( new ErrorBag([new OufOfBoundsError($this->page->number(), $this->page->size())]) ); @@ -169,7 +169,7 @@ protected function pagePaginationLinks( ) { $next = $pageNumber + 1; $previous = $pageNumber - 1; - $last = ($pageSize == 0) ? 0: ceil($totalPages / $pageSize); + $last = ($pageSize == 0) ? 0 : ceil($totalPages / $pageSize); $links = array_filter( [ @@ -218,20 +218,28 @@ protected function pagePaginatedRoute( Included $included, $filters ) { + $fieldKeys = []; + if (false === $fields->isEmpty()) { + $fieldKeys = $fields->get(); + foreach ($fieldKeys as &$v) { + $v = implode(',', $v); + } + } + $queryParams = urldecode( http_build_query( - [ - 'page' => array_filter( - [ - 'number' => $pageNumber, - 'size' => $pageSize, - ] - ), - 'fields' => $fields->get(), - 'filter' => $filters, - 'sort' => $sorting->get(), - 'include' => $included->get(), - ] + array_filter([ + 'page' => array_filter( + [ + 'number' => $pageNumber, + 'size' => $pageSize, + ] + ), + 'fields' => $fieldKeys, + 'filter' => $filters, + 'sort' => $sorting->get(), + 'include' => $included->get(), + ]) ) );