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