/** * Paginate current selection using Paginator class. * * @param int $limit Pagination limit. * @param string $pageParameter Name of parameter in request query which is * used to store the current page number. "page" * by default. * @param ServerRequestInterface $request Has to be specified if no global container set. * @return $this * @throws PaginationException */ public function paginate($limit = Paginator::DEFAULT_LIMIT, $pageParameter = Paginator::DEFAULT_PARAMETER, ServerRequestInterface $request = null) { //Will be used in two places $container = $this->container(); if (empty($request)) { if (empty($container) || !$container->has(ServerRequestInterface::class)) { throw new SugarException("Unable to create pagination without specified request."); } //Getting request from container scope $request = $container->get(ServerRequestInterface::class); } if (empty($container) || !$container->has(PaginatorInterface::class)) { //Let's use default paginator $this->paginator = new Paginator($request, $pageParameter); } else { //We need constructor if ($container instanceof FactoryInterface) { $constructor = $container; } else { $constructor = $container->get(FactoryInterface::class); } //We can also create paginator using container $this->paginator = $constructor->make(PaginatorInterface::class, compact('request', 'pageParameter')); } $this->paginator->setLimit($limit); return $this; }
/** * Paginate current selection using Paginator class. * * @param int $limit Pagination limit. * @param string $pageParameter Name of parameter in request query which is * used to store the current page number. "page" * by default. * @param ServerRequestInterface $request Has to be specified if no global container set. * @return $this * @throws PaginationException */ public function paginate($limit = Paginator::DEFAULT_LIMIT, $pageParameter = Paginator::DEFAULT_PARAMETER, ServerRequestInterface $request = null) { if (empty($container = $this->container()) && empty($request)) { throw new PaginationException("Unable to create pagination without specified request."); } //If no request provided we can try to fetch it from container $request = !empty($request) ? $request : $container->get(ServerRequestInterface::class); if (empty($container) || !$container->has(PaginatorInterface::class)) { //Let's use default paginator $this->paginator = new Paginator($request, $pageParameter); } else { //We can also create paginator using container $this->paginator = $container->construct(Paginator::class, compact('request', 'pageParameter')); } $this->paginator->setLimit($limit); return $this; }