/** * {@inheritdoc} */ public function create($items, $itemsPerPage = null, array $options = []) : PaginationInterface { $itemsPerPage = $itemsPerPage ?? $this->options['items_per_page']; $options = $options + $this->options; $currentPage = max((int) $this->request->get($options['page_key'], 1), 1); $pagination = new Pagination($items, $itemsPerPage, $currentPage, $options); $pagination->setRequest($this->request); $pagination->setURLBuilder($this->urlBuilder); $pagination->setViewFactory($this->viewFactory); return $pagination; }
/** * Builds and returns the pagination array. * * @access public * @return array */ public function pagination() { if (empty($this->pagination)) { if (empty($this->request)) { throw new RuntimeException(vsprintf("%s(): A [ Request ] instance is required to generate the pagination array.", [__METHOD__])); } if (empty($this->urlBuilder)) { throw new RuntimeException(vsprintf("%s(): A [ URLBuilder ] instance is required to generate the pagination array.", [__METHOD__])); } $pagination = ['items' => $this->items, 'items_per_page' => $this->itemsPerPage, 'number_of_pages' => $this->pages]; $params = $this->request->get(); if ($this->currentPage > 1) { $pagination['first'] = $this->urlBuilder->current(array_merge($params, [$this->options['page_key'] => 1])); $pagination['previous'] = $this->urlBuilder->current(array_merge($params, [$this->options['page_key'] => $this->currentPage - 1])); } if ($this->currentPage < $this->pages) { $pagination['last'] = $this->urlBuilder->current(array_merge($params, [$this->options['page_key'] => $this->pages])); $pagination['next'] = $this->urlBuilder->current(array_merge($params, [$this->options['page_key'] => $this->currentPage + 1])); } if ($this->options['max_page_links'] !== 0) { if ($this->pages > $this->options['max_page_links']) { $start = max($this->currentPage - ceil($this->options['max_page_links'] / 2), 0); $end = $start + $this->options['max_page_links']; if ($end > $this->pages) { $end = $this->pages; } if ($start > $end - $this->options['max_page_links']) { $start = $end - $this->options['max_page_links']; } } else { $start = 0; $end = $this->pages; } $pagination['pages'] = []; for ($i = $start + 1; $i <= $end; $i++) { $pagination['pages'][] = ['url' => $this->urlBuilder->current(array_merge($params, [$this->options['page_key'] => $i])), 'number' => $i, 'is_current' => $i == $this->currentPage]; } } $this->pagination = $pagination; } return $this->pagination; }
/** * Returns the current URL of the request. * * @access public * @param array $queryParams Associative array used to build URL-encoded query string * @param string $separator Argument separator * @param mixed $language Request language * @return string */ public function current(array $queryParams = [], $separator = '&', $language = true) { $queryParams = $queryParams ?: $this->request->get(); return $this->to($this->request->path(), $queryParams, $separator, $language); }
/** * */ public function testGet() { $get = ['foo' => 'bar', 'baz' => ['bax']]; $request = new Request(['get' => $get]); $this->assertNull($request->get('bar')); $this->assertFalse($request->get('bar', false)); $this->assertEquals('bar', $request->get('foo')); $this->assertEquals('bax', $request->get('baz.0')); $this->assertEquals($get, $request->get()); }