Added pagination to ShortUrls list

This commit is contained in:
Alejandro Celaya 2016-07-04 09:15:50 +02:00
parent 35f1a4b672
commit cc1829f9ed
8 changed files with 109 additions and 9 deletions

View file

@ -10,7 +10,7 @@ use Doctrine\ORM\Mapping as ORM;
* @author
* @link
*
* @ORM\Entity
* @ORM\Entity(repositoryClass="Acelaya\UrlShortener\Repository\ShortUrlRepository")
* @ORM\Table(name="short_urls")
*/
class ShortUrl extends AbstractEntity implements \JsonSerializable

View file

@ -8,6 +8,7 @@ use Acelaya\ZsmAnnotatedServices\Annotation\Inject;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Zend\Diactoros\Response\JsonResponse;
use Zend\Stdlib\ArrayUtils;
use Zend\Stratigility\MiddlewareInterface;
class ListShortcodesMiddleware implements MiddlewareInterface
@ -60,8 +61,11 @@ class ListShortcodesMiddleware implements MiddlewareInterface
return new JsonResponse([
'shortUrls' => [
'data' => $shortUrls,
// 'pagination' => [],
'data' => ArrayUtils::iteratorToArray($shortUrls->getCurrentItems()),
'pagination' => [
'currentPage' => $shortUrls->getCurrentPageNumber(),
'pagesCount' => $shortUrls->count(),
],
]
]);
} catch (\Exception $e) {

View file

@ -0,0 +1,56 @@
<?php
namespace Acelaya\UrlShortener\Paginator\Adapter;
use Acelaya\UrlShortener\Repository\PaginableRepositoryInterface;
use Zend\Paginator\Adapter\AdapterInterface;
class PaginableRepositoryAdapter implements AdapterInterface
{
const ITEMS_PER_PAGE = 10;
/**
* @var PaginableRepositoryInterface
*/
private $paginableRepository;
/**
* @var null
*/
private $searchTerm;
/**
* @var null
*/
private $orderBy;
public function __construct(PaginableRepositoryInterface $paginableRepository, $searchTerm = null, $orderBy = null)
{
$this->paginableRepository = $paginableRepository;
$this->searchTerm = $searchTerm;
$this->orderBy = $orderBy;
}
/**
* Returns a collection of items for a page.
*
* @param int $offset Page offset
* @param int $itemCountPerPage Number of items per page
* @return array
*/
public function getItems($offset, $itemCountPerPage)
{
return $this->paginableRepository->findList($itemCountPerPage, $offset, $this->searchTerm, $this->orderBy);
}
/**
* Count elements of an object
* @link http://php.net/manual/en/countable.count.php
* @return int The custom count as an integer.
* </p>
* <p>
* The return value is cast to an integer.
* @since 5.1.0
*/
public function count()
{
return $this->paginableRepository->countList($this->searchTerm);
}
}

View file

@ -1,9 +1,11 @@
<?php
namespace Acelaya\UrlShortener\Repository;
interface PaginableRepository
interface PaginableRepositoryInterface
{
/**
* Gets a list of elements using provided filtering data
*
* @param int|null $limit
* @param int|null $offset
* @param string|null $searchTerm
@ -11,4 +13,12 @@ interface PaginableRepository
* @return array
*/
public function findList($limit = null, $offset = null, $searchTerm = null, $orderBy = null);
/**
* Counts the number of elements in a list using provided filtering data
*
* @param null $searchTerm
* @return int
*/
public function countList($searchTerm = null);
}

View file

@ -3,6 +3,7 @@ namespace Acelaya\UrlShortener\Repository;
use Acelaya\UrlShortener\Entity\ShortUrl;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\QueryBuilder;
class ShortUrlRepository extends EntityRepository implements ShortUrlRepositoryInterface
{
@ -39,4 +40,23 @@ class ShortUrlRepository extends EntityRepository implements ShortUrlRepositoryI
return $qb->getQuery()->getResult();
}
/**
* Counts the number of elements in a list using provided filtering data
*
* @param null $searchTerm
* @return int
*/
public function countList($searchTerm = null)
{
$qb = $this->getEntityManager()->createQueryBuilder();
$qb->select('COUNT(s)')
->from(ShortUrl::class, 's');
if (isset($searchTerm)) {
// TODO
}
return (int) $qb->getQuery()->getSingleScalarResult();
}
}

View file

@ -3,6 +3,6 @@ namespace Acelaya\UrlShortener\Repository;
use Doctrine\Common\Persistence\ObjectRepository;
interface ShortUrlRepositoryInterface extends ObjectRepository, PaginableRepository
interface ShortUrlRepositoryInterface extends ObjectRepository, PaginableRepositoryInterface
{
}

View file

@ -2,6 +2,8 @@
namespace Acelaya\UrlShortener\Service;
use Acelaya\UrlShortener\Entity\ShortUrl;
use Acelaya\UrlShortener\Paginator\Adapter\PaginableRepositoryAdapter;
use Acelaya\UrlShortener\Repository\ShortUrlRepository;
use Acelaya\ZsmAnnotatedServices\Annotation\Inject;
use Doctrine\ORM\EntityManagerInterface;
use Zend\Paginator\Paginator;
@ -25,10 +27,17 @@ class ShortUrlService implements ShortUrlServiceInterface
}
/**
* @param int $page
* @return Paginator|ShortUrl[]
*/
public function listShortUrls()
public function listShortUrls($page = 1)
{
return $this->em->getRepository(ShortUrl::class)->findAll();
/** @var ShortUrlRepository $repo */
$repo = $this->em->getRepository(ShortUrl::class);
$paginator = new Paginator(new PaginableRepositoryAdapter($repo));
$paginator->setItemCountPerPage(PaginableRepositoryAdapter::ITEMS_PER_PAGE)
->setCurrentPageNumber($page);
return $paginator;
}
}

View file

@ -7,7 +7,8 @@ use Zend\Paginator\Paginator;
interface ShortUrlServiceInterface
{
/**
* @return Paginator|ShortUrl[]
* @param int $page
* @return ShortUrl[]|Paginator
*/
public function listShortUrls();
public function listShortUrls($page = 1);
}