mirror of
https://github.com/shlinkio/shlink.git
synced 2024-12-23 07:33:58 -06:00
Created trait to serialize paginators
This commit is contained in:
parent
30773c66d0
commit
b4e6fe7135
@ -1,6 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace Acelaya\UrlShortener\Middleware\Rest;
|
namespace Acelaya\UrlShortener\Middleware\Rest;
|
||||||
|
|
||||||
|
use Acelaya\UrlShortener\Paginator\Util\PaginatorSerializerTrait;
|
||||||
use Acelaya\UrlShortener\Service\ShortUrlService;
|
use Acelaya\UrlShortener\Service\ShortUrlService;
|
||||||
use Acelaya\UrlShortener\Service\ShortUrlServiceInterface;
|
use Acelaya\UrlShortener\Service\ShortUrlServiceInterface;
|
||||||
use Acelaya\UrlShortener\Util\RestUtils;
|
use Acelaya\UrlShortener\Util\RestUtils;
|
||||||
@ -13,6 +14,8 @@ use Zend\Stratigility\MiddlewareInterface;
|
|||||||
|
|
||||||
class ListShortcodesMiddleware implements MiddlewareInterface
|
class ListShortcodesMiddleware implements MiddlewareInterface
|
||||||
{
|
{
|
||||||
|
use PaginatorSerializerTrait;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var ShortUrlServiceInterface
|
* @var ShortUrlServiceInterface
|
||||||
*/
|
*/
|
||||||
@ -57,17 +60,9 @@ class ListShortcodesMiddleware implements MiddlewareInterface
|
|||||||
public function __invoke(Request $request, Response $response, callable $out = null)
|
public function __invoke(Request $request, Response $response, callable $out = null)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$shortUrls = $this->shortUrlService->listShortUrls();
|
$query = $request->getQueryParams();
|
||||||
|
$shortUrls = $this->shortUrlService->listShortUrls(isset($query['page']) ? $query['page'] : 1);
|
||||||
return new JsonResponse([
|
return new JsonResponse(['shortUrls' => $this->serializePaginator($shortUrls)]);
|
||||||
'shortUrls' => [
|
|
||||||
'data' => ArrayUtils::iteratorToArray($shortUrls->getCurrentItems()),
|
|
||||||
'pagination' => [
|
|
||||||
'currentPage' => $shortUrls->getCurrentPageNumber(),
|
|
||||||
'pagesCount' => $shortUrls->count(),
|
|
||||||
],
|
|
||||||
]
|
|
||||||
]);
|
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
return new JsonResponse([
|
return new JsonResponse([
|
||||||
'error' => RestUtils::UNKNOWN_ERROR,
|
'error' => RestUtils::UNKNOWN_ERROR,
|
||||||
|
19
src/Paginator/Util/PaginatorSerializerTrait.php
Normal file
19
src/Paginator/Util/PaginatorSerializerTrait.php
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
<?php
|
||||||
|
namespace Acelaya\UrlShortener\Paginator\Util;
|
||||||
|
|
||||||
|
use Zend\Paginator\Paginator;
|
||||||
|
use Zend\Stdlib\ArrayUtils;
|
||||||
|
|
||||||
|
trait PaginatorSerializerTrait
|
||||||
|
{
|
||||||
|
protected function serializePaginator(Paginator $paginator)
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'data' => ArrayUtils::iteratorToArray($paginator->getCurrentItems()),
|
||||||
|
'pagination' => [
|
||||||
|
'currentPage' => $paginator->getCurrentPageNumber(),
|
||||||
|
'pagesCount' => $paginator->count(),
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user