Created trait to serialize paginators

This commit is contained in:
Alejandro Celaya 2016-07-04 12:50:06 +02:00
parent 30773c66d0
commit b4e6fe7135
2 changed files with 25 additions and 11 deletions

View File

@ -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,

View 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(),
],
];
}
}