renaming some function in paginator + fixing bug in paginator

- the '*itemPerPage' function are renamed '*itemsPerPage'
- the paginatorFactory now get the route parameters correctly
This commit is contained in:
Julien Fastré 2016-09-02 08:20:08 +02:00
parent ede767d01a
commit 1b674f9141
4 changed files with 65 additions and 53 deletions

View File

@ -76,7 +76,7 @@ class SearchController extends Controller
$pattern, $pattern,
$name, $name,
$paginatorFactory->getCurrentPageFirstItemNumber(), $paginatorFactory->getCurrentPageFirstItemNumber(),
$paginatorFactory->getCurrentItemPerPage(), $paginatorFactory->getCurrentItemsPerPage(),
array(SearchInterface::SEARCH_PREVIEW_OPTION => false) array(SearchInterface::SEARCH_PREVIEW_OPTION => false)
)]; )];
} }
@ -104,4 +104,4 @@ class SearchController extends Controller
); );
} }
} }

View File

@ -25,7 +25,7 @@ use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
/** /**
* Standard paginator class. * Standard paginator class.
* *
* Represent a set of paginated pages; * Represent a set of paginated pages;
* *
* @author Julien Fastré <julien.fastre@champs-libres.coop> * @author Julien Fastré <julien.fastre@champs-libres.coop>
@ -75,7 +75,7 @@ class Paginator implements PaginatorInterface
protected $urlGenerator; protected $urlGenerator;
/** /**
* the key in the GET parameter to indicate the page number in * the key in the GET parameter to indicate the page number in
* generated routes * generated routes
* *
* @var string * @var string
@ -85,7 +85,7 @@ class Paginator implements PaginatorInterface
/** /**
* the key in the GET parameter to indicate the number of item per page * the key in the GET parameter to indicate the number of item per page
* *
* @var string * @var string
*/ */
protected $itemPerPageKey; protected $itemPerPageKey;
@ -116,7 +116,7 @@ class Paginator implements PaginatorInterface
} }
/** /**
* *
* @return \Chill\MainBundle\Pagination\Page * @return \Chill\MainBundle\Pagination\Page
*/ */
public function getCurrentPage() public function getCurrentPage()
@ -134,12 +134,12 @@ class Paginator implements PaginatorInterface
return $page->getNumber() === $this->currentPageNumber; return $page->getNumber() === $this->currentPageNumber;
} }
public function getItemPerPage() public function getItemsPerPage()
{ {
return $this->itemPerPage; return $this->itemPerPage;
} }
public function setItemPerPage($itemPerPage) public function setItemsPerPage($itemPerPage)
{ {
$this->itemPerPage = $itemPerPage; $this->itemPerPage = $itemPerPage;
} }
@ -161,7 +161,7 @@ class Paginator implements PaginatorInterface
} }
/** /**
* *
* @return \Chill\MainBundle\Pagination\Page * @return \Chill\MainBundle\Pagination\Page
* @throws \RuntimeException if the next page does not exists * @throws \RuntimeException if the next page does not exists
*/ */
@ -175,7 +175,7 @@ class Paginator implements PaginatorInterface
} }
/** /**
* *
* @return \Chill\MainBundle\Pagination\Page * @return \Chill\MainBundle\Pagination\Page
* @throws \RuntimeException if the next page does not exists * @throws \RuntimeException if the next page does not exists
*/ */
@ -189,7 +189,7 @@ class Paginator implements PaginatorInterface
} }
/** /**
* *
* @return bool * @return bool
*/ */
public function hasNextPage() public function hasNextPage()
@ -198,7 +198,7 @@ class Paginator implements PaginatorInterface
} }
/** /**
* *
* @return bool * @return bool
*/ */
public function hasPreviousPage() public function hasPreviousPage()
@ -208,13 +208,13 @@ class Paginator implements PaginatorInterface
public function hasPage($number) public function hasPage($number)
{ {
return $number > 0 and return $number > 0 and
$number <= $this->countPages(); $number <= $this->countPages();
} }
/** /**
* *
* @param type $number * @param type $number
* @return \Chill\MainBundle\Pagination\Page * @return \Chill\MainBundle\Pagination\Page
*/ */
@ -225,7 +225,7 @@ class Paginator implements PaginatorInterface
} }
return new Page( return new Page(
$number, $number,
$this->itemPerPage, $this->itemPerPage,
$this->urlGenerator, $this->urlGenerator,
$this->route, $this->route,

View File

@ -40,7 +40,7 @@ class PaginatorFactory
private $itemPerPage; private $itemPerPage;
/** /**
* the router and generator for url * the router and generator for url
* *
* @var RouterInterface * @var RouterInterface
*/ */
@ -59,8 +59,8 @@ class PaginatorFactory
public function __construct( public function __construct(
RequestStack $requestStack, RequestStack $requestStack,
RouterInterface $router, RouterInterface $router,
$itemPerPage = 50 $itemPerPage = 50
) { ) {
$this->itemPerPage = $itemPerPage; $this->itemPerPage = $itemPerPage;
@ -70,27 +70,27 @@ class PaginatorFactory
/** /**
* create a paginator instance * create a paginator instance
* *
* The default route and route parameters are the current ones. If set, * The default route and route parameters are the current ones. If set,
* thos route are overriden. * thos route are overriden.
* *
* @param int $totalItems * @param int $totalItems
* @param string|null $route the specific route to use in pages * @param string|null $route the specific route to use in pages
* @param array|null $routeParameters the specific route parameters to use in pages * @param array|null $routeParameters the specific route parameters to use in pages
* @return PaginatorInterface * @return PaginatorInterface
*/ */
public function create( public function create(
$totalItems, $totalItems,
$route = null, $route = null,
array $routeParameters = null array $routeParameters = null
) { ) {
return new Paginator( return new Paginator(
$totalItems, $totalItems,
$this->getCurrentItemPerPage(), $this->getCurrentItemsPerPage(),
$this->getCurrentPageNumber(), $this->getCurrentPageNumber(),
$route === null ? $this->getCurrentRoute() : $route, $route === null ? $this->getCurrentRoute() : $route,
$routeParameters === null ? $this->getCurrentRouteParameters() : $routeParameters === null ? $this->getCurrentRouteParameters() :
$routeParameters, $routeParameters,
$this->router, $this->router,
self::DEFAULT_CURRENT_PAGE_KEY, self::DEFAULT_CURRENT_PAGE_KEY,
@ -98,7 +98,7 @@ class PaginatorFactory
} }
/** /**
* *
* @return int * @return int
*/ */
public function getCurrentPageNumber() public function getCurrentPageNumber()
@ -109,7 +109,7 @@ class PaginatorFactory
->getInt(self::DEFAULT_CURRENT_PAGE_KEY, self::DEFAULT_PAGE_NUMBER); ->getInt(self::DEFAULT_CURRENT_PAGE_KEY, self::DEFAULT_PAGE_NUMBER);
} }
public function getCurrentItemPerPage() public function getCurrentItemsPerPage()
{ {
return $this->requestStack return $this->requestStack
->getCurrentRequest() ->getCurrentRequest()
@ -119,8 +119,8 @@ class PaginatorFactory
public function getCurrentPageFirstItemNumber() public function getCurrentPageFirstItemNumber()
{ {
return ($this->getCurrentPageNumber() - 1) * return ($this->getCurrentPageNumber() - 1) *
$this->getCurrentItemPerPage(); $this->getCurrentItemsPerPage();
} }
protected function getCurrentRoute() protected function getCurrentRoute()
@ -134,7 +134,13 @@ class PaginatorFactory
{ {
return array_merge( return array_merge(
$this->router->getContext()->getParameters(), $this->router->getContext()->getParameters(),
$this->requestStack->getCurrentRequest() // get the route parameters
->query->all()); $this->requestStack
->getCurrentRequest()
->attributes->get('_route_params'),
// get the query parameters
$this->requestStack
->getCurrentRequest()->query->all()
);
} }
} }

View File

@ -22,12 +22,12 @@ namespace Chill\MainBundle\Pagination;
/** /**
* Represent a set of numbered pages * Represent a set of numbered pages
* *
* Allow to calculate and render pagination for a set of pages. * Allow to calculate and render pagination for a set of pages.
* *
* The items are elements that `could` be shown. The item are divided and shown * The items are elements that `could` be shown. The item are divided and shown
* into pages. Each page is numbered and count a given number of item per page. * into pages. Each page is numbered and count a given number of item per page.
* *
* The first page number is 1, although the first result number is 0. * The first page number is 1, although the first result number is 0.
* *
* @author Julien Fastré <julien.fastre@champs-libres.coop> * @author Julien Fastré <julien.fastre@champs-libres.coop>
@ -35,40 +35,46 @@ namespace Chill\MainBundle\Pagination;
interface PaginatorInterface extends \Countable interface PaginatorInterface extends \Countable
{ {
/** /**
* get the number of result for this pagination * get the number of results for this paginator
* *
* @return int * @return int
*/ */
public function getTotalItems(); public function getTotalItems();
/** /**
* get the first result for the current page * get the first result for the current page
* *
* @return int * @return int
*/ */
public function getCurrentPageFirstItemNumber(); public function getCurrentPageFirstItemNumber();
public function getItemPerPage(); /*
* get the number of items per page
public function setItemPerPage($itemPerPage); */
public function getItemsPerPage();
/*
* set the number of items per page
*/
public function setItemsPerPage($itemsPerPage);
/** /**
* get the number of page for this pagination. * get the number of pages for this pagination.
* *
* @return int * @return int
*/ */
public function countPages(); public function countPages();
/** /**
* get the current page * get the current page
* *
* @return PageInterface * @return PageInterface
*/ */
public function getCurrentPage(); public function getCurrentPage();
/** /**
* check if the given page is the current page * check if the given page is the current page
* *
* @param \Chill\MainBundle\Pagination\PageInterface $page * @param \Chill\MainBundle\Pagination\PageInterface $page
* @return bool * @return bool
*/ */
@ -76,14 +82,14 @@ interface PaginatorInterface extends \Countable
/** /**
* check if the page with the given number exists * check if the page with the given number exists
* *
* @param int $number * @param int $number
*/ */
public function hasPage($number); public function hasPage($number);
/** /**
* get page by his number * get page by his number
* *
* @param int $number * @param int $number
* @throws \RuntimeException if the pagination has no page with specified number * @throws \RuntimeException if the pagination has no page with specified number
*/ */
@ -91,7 +97,7 @@ interface PaginatorInterface extends \Countable
/** /**
* get the next page * get the next page
* *
* @return PageInterface * @return PageInterface
* @throws \RuntimeException if the pagination has not next page * @throws \RuntimeException if the pagination has not next page
*/ */
@ -99,7 +105,7 @@ interface PaginatorInterface extends \Countable
/** /**
* get the previous page * get the previous page
* *
* @return PageInterface * @return PageInterface
* @throws \RuntimeException if the pagination has not previous page * @throws \RuntimeException if the pagination has not previous page
*/ */
@ -107,21 +113,21 @@ interface PaginatorInterface extends \Countable
/** /**
* check if the current page has a next page * check if the current page has a next page
* *
* @return bool * @return bool
*/ */
public function hasNextPage(); public function hasNextPage();
/** /**
* check if the current page has a page before * check if the current page has a page before
* *
* @return bool * @return bool
*/ */
public function hasPreviousPage(); public function hasPreviousPage();
/** /**
* get a generator to generate pages * get a generator to generate pages
* *
* @return \Generator which return PageInterface elements * @return \Generator which return PageInterface elements
*/ */
public function getPagesGenerator(); public function getPagesGenerator();