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

View File

@ -40,7 +40,7 @@ class PaginatorFactory
private $itemPerPage;
/**
* the router and generator for url
* the router and generator for url
*
* @var RouterInterface
*/
@ -59,8 +59,8 @@ class PaginatorFactory
public function __construct(
RequestStack $requestStack,
RouterInterface $router,
RequestStack $requestStack,
RouterInterface $router,
$itemPerPage = 50
) {
$this->itemPerPage = $itemPerPage;
@ -70,27 +70,27 @@ class PaginatorFactory
/**
* 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.
*
*
* @param int $totalItems
* @param string|null $route the specific route to use in pages
* @param array|null $routeParameters the specific route parameters to use in pages
* @return PaginatorInterface
*/
public function create(
$totalItems,
$route = null,
$totalItems,
$route = null,
array $routeParameters = null
) {
return new Paginator(
$totalItems,
$this->getCurrentItemPerPage(),
$this->getCurrentItemsPerPage(),
$this->getCurrentPageNumber(),
$route === null ? $this->getCurrentRoute() : $route,
$routeParameters === null ? $this->getCurrentRouteParameters() :
$routeParameters === null ? $this->getCurrentRouteParameters() :
$routeParameters,
$this->router,
self::DEFAULT_CURRENT_PAGE_KEY,
@ -98,7 +98,7 @@ class PaginatorFactory
}
/**
*
*
* @return int
*/
public function getCurrentPageNumber()
@ -109,7 +109,7 @@ class PaginatorFactory
->getInt(self::DEFAULT_CURRENT_PAGE_KEY, self::DEFAULT_PAGE_NUMBER);
}
public function getCurrentItemPerPage()
public function getCurrentItemsPerPage()
{
return $this->requestStack
->getCurrentRequest()
@ -119,8 +119,8 @@ class PaginatorFactory
public function getCurrentPageFirstItemNumber()
{
return ($this->getCurrentPageNumber() - 1) *
$this->getCurrentItemPerPage();
return ($this->getCurrentPageNumber() - 1) *
$this->getCurrentItemsPerPage();
}
protected function getCurrentRoute()
@ -134,7 +134,13 @@ class PaginatorFactory
{
return array_merge(
$this->router->getContext()->getParameters(),
$this->requestStack->getCurrentRequest()
->query->all());
// get the route parameters
$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
*
*
* 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
* into pages. Each page is numbered and count a given number of item per page.
*
*
* 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.
*
* The first page number is 1, although the first result number is 0.
*
* @author Julien Fastré <julien.fastre@champs-libres.coop>
@ -35,40 +35,46 @@ namespace Chill\MainBundle\Pagination;
interface PaginatorInterface extends \Countable
{
/**
* get the number of result for this pagination
*
* get the number of results for this paginator
*
* @return int
*/
public function getTotalItems();
/**
* get the first result for the current page
*
*
* @return int
*/
public function getCurrentPageFirstItemNumber();
public function getItemPerPage();
public function setItemPerPage($itemPerPage);
/*
* get the number of items per page
*/
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
*/
public function countPages();
/**
* get the current page
*
*
* @return PageInterface
*/
public function getCurrentPage();
/**
* check if the given page is the current page
*
*
* @param \Chill\MainBundle\Pagination\PageInterface $page
* @return bool
*/
@ -76,14 +82,14 @@ interface PaginatorInterface extends \Countable
/**
* check if the page with the given number exists
*
*
* @param int $number
*/
public function hasPage($number);
/**
* get page by his number
*
*
* @param int $number
* @throws \RuntimeException if the pagination has no page with specified number
*/
@ -91,7 +97,7 @@ interface PaginatorInterface extends \Countable
/**
* get the next page
*
*
* @return PageInterface
* @throws \RuntimeException if the pagination has not next page
*/
@ -99,7 +105,7 @@ interface PaginatorInterface extends \Countable
/**
* get the previous page
*
*
* @return PageInterface
* @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
*
*
* @return bool
*/
public function hasNextPage();
/**
* check if the current page has a page before
*
*
* @return bool
*/
public function hasPreviousPage();
/**
* get a generator to generate pages
*
*
* @return \Generator which return PageInterface elements
*/
public function getPagesGenerator();