itemPerPage = $itemPerPage; $this->requestStack = $requestStack; $this->router = $router; } /** * create a paginator instance. * * 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, ?array $routeParameters = null ) { return new Paginator( $totalItems, $this->getCurrentItemsPerPage(), $this->getCurrentPageNumber(), null === $route ? $this->getCurrentRoute() : $route, null === $routeParameters ? $this->getCurrentRouteParameters() : $routeParameters, $this->router, self::DEFAULT_CURRENT_PAGE_KEY, self::DEFAULT_ITEM_PER_NUMBER_KEY ); } public function getCurrentItemsPerPage() { return $this->requestStack ->getCurrentRequest() ->query ->getInt(self::DEFAULT_ITEM_PER_NUMBER_KEY, $this->itemPerPage); } public function getCurrentPageFirstItemNumber() { return ($this->getCurrentPageNumber() - 1) * $this->getCurrentItemsPerPage(); } /** * @return int */ public function getCurrentPageNumber() { return $this->requestStack ->getCurrentRequest() ->query ->getInt(self::DEFAULT_CURRENT_PAGE_KEY, self::DEFAULT_PAGE_NUMBER); } protected function getCurrentRoute() { $request = $this->requestStack->getCurrentRequest(); return $request->get('_route'); } protected function getCurrentRouteParameters() { return array_merge( $this->router->getContext()->getParameters(), // get the route parameters $this->requestStack ->getCurrentRequest() ->attributes->get('_route_params'), // get the query parameters $this->requestStack ->getCurrentRequest()->query->all() ); } }