From cef218fed5d87ec46e454675513b8d9814527278 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Mon, 27 Nov 2023 18:14:07 +0100 Subject: [PATCH] Add interface for pagination --- .../Pagination/PaginatorFactory.php | 31 ++++------ .../Pagination/PaginatorFactoryInterface.php | 35 ++++++++++++ .../ChillMainBundle/Test/DummyPaginator.php | 56 +++++++++++++++++++ .../config/services/pagination.yaml | 1 + 4 files changed, 103 insertions(+), 20 deletions(-) create mode 100644 src/Bundle/ChillMainBundle/Pagination/PaginatorFactoryInterface.php create mode 100644 src/Bundle/ChillMainBundle/Test/DummyPaginator.php diff --git a/src/Bundle/ChillMainBundle/Pagination/PaginatorFactory.php b/src/Bundle/ChillMainBundle/Pagination/PaginatorFactory.php index 0daad7283..f7829b332 100644 --- a/src/Bundle/ChillMainBundle/Pagination/PaginatorFactory.php +++ b/src/Bundle/ChillMainBundle/Pagination/PaginatorFactory.php @@ -17,7 +17,7 @@ use Symfony\Component\Routing\RouterInterface; /** * Create paginator instances. */ -class PaginatorFactory +final readonly class PaginatorFactory implements PaginatorFactoryInterface { final public const DEFAULT_CURRENT_PAGE_KEY = 'page'; @@ -25,23 +25,20 @@ class PaginatorFactory final public const DEFAULT_PAGE_NUMBER = 1; - /** - * @param int $itemPerPage - */ public function __construct( /** * the request stack. */ - private readonly RequestStack $requestStack, + private RequestStack $requestStack, /** * the router and generator for url. */ - private readonly RouterInterface $router, + private RouterInterface $router, /** * the default item per page. This may be overriden by * 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, * 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, + int $totalItems, ?string $route = null, ?array $routeParameters = null - ) { + ): PaginatorInterface { return new Paginator( $totalItems, $this->getCurrentItemsPerPage(), @@ -74,7 +68,7 @@ class PaginatorFactory ); } - public function getCurrentItemsPerPage() + public function getCurrentItemsPerPage(): int { return $this->requestStack ->getCurrentRequest() @@ -82,16 +76,13 @@ class PaginatorFactory ->getInt(self::DEFAULT_ITEM_PER_NUMBER_KEY, $this->itemPerPage); } - public function getCurrentPageFirstItemNumber() + public function getCurrentPageFirstItemNumber(): int { return ($this->getCurrentPageNumber() - 1) * $this->getCurrentItemsPerPage(); } - /** - * @return int - */ - public function getCurrentPageNumber() + public function getCurrentPageNumber(): int { return $this->requestStack ->getCurrentRequest() @@ -99,14 +90,14 @@ class PaginatorFactory ->getInt(self::DEFAULT_CURRENT_PAGE_KEY, self::DEFAULT_PAGE_NUMBER); } - protected function getCurrentRoute() + private function getCurrentRoute() { $request = $this->requestStack->getCurrentRequest(); return $request->get('_route'); } - protected function getCurrentRouteParameters() + private function getCurrentRouteParameters() { return array_merge( $this->router->getContext()->getParameters(), diff --git a/src/Bundle/ChillMainBundle/Pagination/PaginatorFactoryInterface.php b/src/Bundle/ChillMainBundle/Pagination/PaginatorFactoryInterface.php new file mode 100644 index 000000000..c83d1f45c --- /dev/null +++ b/src/Bundle/ChillMainBundle/Pagination/PaginatorFactoryInterface.php @@ -0,0 +1,35 @@ +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; + } +} diff --git a/src/Bundle/ChillMainBundle/config/services/pagination.yaml b/src/Bundle/ChillMainBundle/config/services/pagination.yaml index f94bc31be..bdcef3a10 100644 --- a/src/Bundle/ChillMainBundle/config/services/pagination.yaml +++ b/src/Bundle/ChillMainBundle/config/services/pagination.yaml @@ -12,6 +12,7 @@ services: - "%chill_main.pagination.item_per_page%" Chill\MainBundle\Pagination\PaginatorFactory: '@chill_main.paginator_factory' + Chill\MainBundle\Pagination\PaginatorFactoryInterface: '@chill_main.paginator_factory' chill_main.paginator.twig_extensions: class: Chill\MainBundle\Pagination\ChillPaginationTwig