Created stuff to handle pagination on list results

This commit is contained in:
Alejandro Celaya 2016-07-04 08:57:37 +02:00
parent 67ef171262
commit 35f1a4b672
8 changed files with 73 additions and 4 deletions

View File

@ -18,6 +18,7 @@
"zendframework/zend-expressive-twigrenderer": "^1.0", "zendframework/zend-expressive-twigrenderer": "^1.0",
"zendframework/zend-stdlib": "^2.7", "zendframework/zend-stdlib": "^2.7",
"zendframework/zend-servicemanager": "^3.0", "zendframework/zend-servicemanager": "^3.0",
"zendframework/zend-paginator": "^2.6",
"doctrine/orm": "^2.5", "doctrine/orm": "^2.5",
"guzzlehttp/guzzle": "^6.2", "guzzlehttp/guzzle": "^6.2",
"acelaya/zsm-annotated-services": "^0.2.0", "acelaya/zsm-annotated-services": "^0.2.0",

View File

@ -0,0 +1,14 @@
<?php
namespace Acelaya\UrlShortener\Repository;
interface PaginableRepository
{
/**
* @param int|null $limit
* @param int|null $offset
* @param string|null $searchTerm
* @param string|array|null $orderBy
* @return array
*/
public function findList($limit = null, $offset = null, $searchTerm = null, $orderBy = null);
}

View File

@ -0,0 +1,42 @@
<?php
namespace Acelaya\UrlShortener\Repository;
use Acelaya\UrlShortener\Entity\ShortUrl;
use Doctrine\ORM\EntityRepository;
class ShortUrlRepository extends EntityRepository implements ShortUrlRepositoryInterface
{
/**
* @param int|null $limit
* @param int|null $offset
* @param string|null $searchTerm
* @param string|array|null $orderBy
* @return ShortUrl[]
*/
public function findList($limit = null, $offset = null, $searchTerm = null, $orderBy = null)
{
$qb = $this->createQueryBuilder('s');
if (isset($limit)) {
$qb->setMaxResults($limit);
}
if (isset($offset)) {
$qb->setFirstResult($offset);
}
if (isset($searchTerm)) {
// TODO
}
if (isset($orderBy)) {
if (is_string($orderBy)) {
$qb->orderBy($orderBy);
} elseif (is_array($orderBy)) {
$key = key($orderBy);
$qb->orderBy($key, $orderBy[$key]);
}
} else {
$qb->orderBy('s.dateCreated');
}
return $qb->getQuery()->getResult();
}
}

View File

@ -0,0 +1,8 @@
<?php
namespace Acelaya\UrlShortener\Repository;
use Doctrine\Common\Persistence\ObjectRepository;
interface ShortUrlRepositoryInterface extends ObjectRepository, PaginableRepository
{
}

View File

@ -4,6 +4,7 @@ namespace Acelaya\UrlShortener\Service;
use Acelaya\UrlShortener\Entity\ShortUrl; use Acelaya\UrlShortener\Entity\ShortUrl;
use Acelaya\ZsmAnnotatedServices\Annotation\Inject; use Acelaya\ZsmAnnotatedServices\Annotation\Inject;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Zend\Paginator\Paginator;
class ShortUrlService implements ShortUrlServiceInterface class ShortUrlService implements ShortUrlServiceInterface
{ {
@ -24,7 +25,7 @@ class ShortUrlService implements ShortUrlServiceInterface
} }
/** /**
* @return ShortUrl[] * @return Paginator|ShortUrl[]
*/ */
public function listShortUrls() public function listShortUrls()
{ {

View File

@ -2,11 +2,12 @@
namespace Acelaya\UrlShortener\Service; namespace Acelaya\UrlShortener\Service;
use Acelaya\UrlShortener\Entity\ShortUrl; use Acelaya\UrlShortener\Entity\ShortUrl;
use Zend\Paginator\Paginator;
interface ShortUrlServiceInterface interface ShortUrlServiceInterface
{ {
/** /**
* @return ShortUrl[] * @return Paginator|ShortUrl[]
*/ */
public function listShortUrls(); public function listShortUrls();
} }

View File

@ -7,6 +7,7 @@ use Acelaya\UrlShortener\Exception\InvalidArgumentException;
use Acelaya\UrlShortener\Exception\InvalidShortCodeException; use Acelaya\UrlShortener\Exception\InvalidShortCodeException;
use Acelaya\ZsmAnnotatedServices\Annotation\Inject; use Acelaya\ZsmAnnotatedServices\Annotation\Inject;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Zend\Paginator\Paginator;
class VisitsTracker implements VisitsTrackerInterface class VisitsTracker implements VisitsTrackerInterface
{ {
@ -65,7 +66,7 @@ class VisitsTracker implements VisitsTrackerInterface
* Returns the visits on certain shortcode * Returns the visits on certain shortcode
* *
* @param $shortCode * @param $shortCode
* @return Visit[] * @return Paginator|Visit[]
*/ */
public function info($shortCode) public function info($shortCode)
{ {

View File

@ -2,6 +2,7 @@
namespace Acelaya\UrlShortener\Service; namespace Acelaya\UrlShortener\Service;
use Acelaya\UrlShortener\Entity\Visit; use Acelaya\UrlShortener\Entity\Visit;
use Zend\Paginator\Paginator;
interface VisitsTrackerInterface interface VisitsTrackerInterface
{ {
@ -17,7 +18,7 @@ interface VisitsTrackerInterface
* Returns the visits on certain shortcode * Returns the visits on certain shortcode
* *
* @param $shortCode * @param $shortCode
* @return Visit[] * @return Paginator|Visit[]
*/ */
public function info($shortCode); public function info($shortCode);
} }