Add interface for pagination

This commit is contained in:
Julien Fastré 2023-11-27 18:14:07 +01:00
parent 930a76cc66
commit cef218fed5
Signed by: julienfastre
GPG Key ID: BDE2190974723FCB
4 changed files with 103 additions and 20 deletions

View File

@ -17,7 +17,7 @@ use Symfony\Component\Routing\RouterInterface;
/** /**
* Create paginator instances. * Create paginator instances.
*/ */
class PaginatorFactory final readonly class PaginatorFactory implements PaginatorFactoryInterface
{ {
final public const DEFAULT_CURRENT_PAGE_KEY = 'page'; final public const DEFAULT_CURRENT_PAGE_KEY = 'page';
@ -25,23 +25,20 @@ class PaginatorFactory
final public const DEFAULT_PAGE_NUMBER = 1; final public const DEFAULT_PAGE_NUMBER = 1;
/**
* @param int $itemPerPage
*/
public function __construct( public function __construct(
/** /**
* the request stack. * the request stack.
*/ */
private readonly RequestStack $requestStack, private RequestStack $requestStack,
/** /**
* the router and generator for url. * the router and generator for url.
*/ */
private readonly RouterInterface $router, private RouterInterface $router,
/** /**
* the default item per page. This may be overriden by * the default item per page. This may be overriden by
* the request or inside the paginator. * the request or inside the paginator.
*/ */
private $itemPerPage = 20 private int $itemPerPage = 20
) { ) {
} }
@ -51,17 +48,14 @@ class PaginatorFactory
* 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 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
*/ */
public function create( public function create(
$totalItems, int $totalItems,
?string $route = null, ?string $route = null,
?array $routeParameters = null ?array $routeParameters = null
) { ): PaginatorInterface {
return new Paginator( return new Paginator(
$totalItems, $totalItems,
$this->getCurrentItemsPerPage(), $this->getCurrentItemsPerPage(),
@ -74,7 +68,7 @@ class PaginatorFactory
); );
} }
public function getCurrentItemsPerPage() public function getCurrentItemsPerPage(): int
{ {
return $this->requestStack return $this->requestStack
->getCurrentRequest() ->getCurrentRequest()
@ -82,16 +76,13 @@ class PaginatorFactory
->getInt(self::DEFAULT_ITEM_PER_NUMBER_KEY, $this->itemPerPage); ->getInt(self::DEFAULT_ITEM_PER_NUMBER_KEY, $this->itemPerPage);
} }
public function getCurrentPageFirstItemNumber() public function getCurrentPageFirstItemNumber(): int
{ {
return ($this->getCurrentPageNumber() - 1) * return ($this->getCurrentPageNumber() - 1) *
$this->getCurrentItemsPerPage(); $this->getCurrentItemsPerPage();
} }
/** public function getCurrentPageNumber(): int
* @return int
*/
public function getCurrentPageNumber()
{ {
return $this->requestStack return $this->requestStack
->getCurrentRequest() ->getCurrentRequest()
@ -99,14 +90,14 @@ class PaginatorFactory
->getInt(self::DEFAULT_CURRENT_PAGE_KEY, self::DEFAULT_PAGE_NUMBER); ->getInt(self::DEFAULT_CURRENT_PAGE_KEY, self::DEFAULT_PAGE_NUMBER);
} }
protected function getCurrentRoute() private function getCurrentRoute()
{ {
$request = $this->requestStack->getCurrentRequest(); $request = $this->requestStack->getCurrentRequest();
return $request->get('_route'); return $request->get('_route');
} }
protected function getCurrentRouteParameters() private function getCurrentRouteParameters()
{ {
return array_merge( return array_merge(
$this->router->getContext()->getParameters(), $this->router->getContext()->getParameters(),

View File

@ -0,0 +1,35 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\MainBundle\Pagination;
/**
* Create paginator instances.
*/
interface PaginatorFactoryInterface
{
/**
* create a paginator instance.
*
* The default route and route parameters are the current ones. If set,
* thos route are overriden.
*
* @param string|null $route the specific route to use in pages
* @param array|null $routeParameters the specific route parameters to use in pages
*/
public function create(int $totalItems, string $route = null, array $routeParameters = null): PaginatorInterface;
public function getCurrentItemsPerPage(): int;
public function getCurrentPageFirstItemNumber(): int;
public function getCurrentPageNumber(): int;
}

View File

@ -0,0 +1,56 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\MainBundle\Test;
use Chill\MainBundle\Pagination\Paginator;
use Chill\MainBundle\Pagination\PaginatorFactory;
use Chill\MainBundle\Pagination\PaginatorFactoryInterface;
use Chill\MainBundle\Pagination\PaginatorInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
class DummyPaginator implements PaginatorFactoryInterface
{
public function __construct(
private UrlGeneratorInterface $urlGenerator,
private string $route,
private array $routeParameters = []
) {}
public function create(int $totalItems, string $route = null, array $routeParameters = null): PaginatorInterface
{
return new Paginator(
$totalItems,
$totalItems,
1,
$this->route,
$this->routeParameters,
$this->urlGenerator,
PaginatorFactory::DEFAULT_CURRENT_PAGE_KEY,
PaginatorFactory::DEFAULT_ITEM_PER_NUMBER_KEY
);
}
public function getCurrentItemsPerPage(): int
{
return 20;
}
public function getCurrentPageFirstItemNumber(): int
{
return 1;
}
public function getCurrentPageNumber(): int
{
return 1;
}
}

View File

@ -12,6 +12,7 @@ services:
- "%chill_main.pagination.item_per_page%" - "%chill_main.pagination.item_per_page%"
Chill\MainBundle\Pagination\PaginatorFactory: '@chill_main.paginator_factory' Chill\MainBundle\Pagination\PaginatorFactory: '@chill_main.paginator_factory'
Chill\MainBundle\Pagination\PaginatorFactoryInterface: '@chill_main.paginator_factory'
chill_main.paginator.twig_extensions: chill_main.paginator.twig_extensions:
class: Chill\MainBundle\Pagination\ChillPaginationTwig class: Chill\MainBundle\Pagination\ChillPaginationTwig