mirror of
				https://gitlab.com/Chill-Projet/chill-bundles.git
				synced 2025-10-31 01:08:26 +00:00 
			
		
		
		
	Add interface for pagination
This commit is contained in:
		| @@ -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(), | ||||
|   | ||||
| @@ -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; | ||||
| } | ||||
							
								
								
									
										56
									
								
								src/Bundle/ChillMainBundle/Test/DummyPaginator.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										56
									
								
								src/Bundle/ChillMainBundle/Test/DummyPaginator.php
									
									
									
									
									
										Normal 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; | ||||
|     } | ||||
| } | ||||
| @@ -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 | ||||
|   | ||||
		Reference in New Issue
	
	Block a user