Make a pagination

@param array   $items
@param integer $total
@param integer $pageSize

@return array
171165
public function make($items, $total, $pageSize = 10)
    {
        $this->total    = abs($total);
        $this->pageSize = $pageSize;
        $this->items = $items;

        return $this;
    }
525988
public function append($items)
    {
        return new ResultScan(array_merge($this->buffered()->toArray(), $this->parseItems($items)));
    }
1321420
public function pagination(int $limit = 20, int $offset = 0, ?callable $totalQuery = null)
    {
        $query = clone $this->selectQueryInstance();

        return $query->paginate($limit, $offset, $totalQuery);
    }
496791
public function paginate($size = null, $page = null)
    {
        $this->page($size, $page);

        $items = $this->fetch(true);

        if ($this->_limit === null) {
            $count = count($items);
        } elseif (count($items) % $this->_limit === 0) {
            $count = $this->count();
        } else {
            $count = $this->_offset + count($items);
        }

        $paginator = $this->paginator;

        $paginator->items = $items;

        return $paginator->paginate($count, $this->_limit, (int)($this->_offset / $this->_limit) + 1);
    }
367268
public function collection($params = 'content', $pagination = true)
    {
        if (is_string($params)) {
            $params = (array)$this->value('header.' . $params);
        } elseif (!is_array($params)) {
            throw new \InvalidArgumentException('Argument should be either header variable name or array of parameters');
        }

        if (!isset($params['items'])) {
            return new Collection();
        }

        // See if require published filter is set and use that, if assume published=true
        $only_published = true;
        if (isset($params['filter']['published']) && $params['filter']['published']) {
            $only_published = false;
        } elseif (isset($params['filter']['non-published']) && $params['filter']['non-published']) {
            $only_published = false;
        }

        $collection = $this->evaluate($params['items'], $only_published);
        if (!$collection instanceof Collection) {
            $collection = new Collection();
        }
        $collection->setParams($params);

        /** @var Uri $uri */
        $uri = Grav::instance()['uri'];
        /** @var Config $config */
        $config = Grav::instance()['config'];

        $process_taxonomy = $params['url_taxonomy_filters'] ?? $config->get('system.pages.url_taxonomy_filters');

        if ($process_taxonomy) {
            foreach ((array)$config->get('site.taxonomies') as $taxonomy) {
                if ($uri->param(rawurlencode($taxonomy))) {
                    $items = explode(',', $uri->param($taxonomy));
                    $collection->setParams(['taxonomies' => [$taxonomy => $items]]);

                    foreach ($collection as $page) {
                        // Don't filter modular pages
                        if ($page->modular()) {
                            continue;
                        }
                        foreach ($items as $item) {
                            $item = rawurldecode($item);
                            if (empty($page->taxonomy[$taxonomy]) || !\in_array(htmlspecialchars_decode($item, ENT_QUOTES), $page->taxonomy[$taxonomy], true)
                            ) {
                                $collection->remove($page->path());
                            }
                        }
                    }
                }
            }
        }

        // If  a filter or filters are set, filter the collection...
        if (isset($params['filter'])) {

            // remove any inclusive sets from filer:
            $sets = ['published', 'visible', 'modular', 'routable'];
            foreach ($sets as $type) {
                $var = "non-{$type}";
                if (isset($params['filter'][$type], $params['filter'][$var]) && $params['filter'][$type] && $params['filter'][$var]) {
                    unset ($params['filter'][$type], $params['filter'][$var]);
                }
            }

            foreach ((array)$params['filter'] as $type => $filter) {
                switch ($type) {
                    case 'published':
                        if ((bool) $filter) {
                            $collection->published();
                        }
                        break;
                    case 'non-published':
                        if ((bool) $filter) {
                            $collection->nonPublished();
                        }
                        break;
                    case 'visible':
                        if ((bool) $filter) {
                            $collection->visible();
                        }
                        break;
                    case 'non-visible':
                        if ((bool) $filter) {
                            $collection->nonVisible();
                        }
                        break;
                    case 'modular':
                        if ((bool) $filter) {
                            $collection->modular();
                        }
                        break;
                    case 'non-modular':
                        if ((bool) $filter) {
                            $collection->nonModular();
                        }
                        break;
                    case 'routable':
                        if ((bool) $filter) {
                            $collection->routable();
                        }
                        break;
                    case 'non-routable':
                        if ((bool) $filter) {
                            $collection->nonRoutable();
                        }
                        break;
                    case 'type':
                        $collection->ofType($filter);
                        break;
                    case 'types':
                        $collection->ofOneOfTheseTypes($filter);
                        break;
                    case 'access':
                        $collection->ofOneOfTheseAccessLevels($filter);
                        break;
                }
            }
        }

        if (isset($params['dateRange'])) {
            $start = $params['dateRange']['start'] ?? 0;
            $end = $params['dateRange']['end'] ?? false;
            $field = $params['dateRange']['field'] ?? false;
            $collection->dateRange($start, $end, $field);
        }

        if (isset($params['order'])) {
            $by = $params['order']['by'] ?? 'default';
            $dir = $params['order']['dir'] ?? 'asc';
            $custom = $params['order']['custom'] ?? null;
            $sort_flags = $params['order']['sort_flags'] ?? null;

            if (is_array($sort_flags)) {
                $sort_flags = array_map('constant', $sort_flags); //transform strings to constant value
                $sort_flags = array_reduce($sort_flags, function ($a, $b) {
                    return $a | $b;
                }, 0); //merge constant values using bit or
            }

            $collection->order($by, $dir, $custom, $sort_flags);
        }

        /** @var Grav $grav */
        $grav = Grav::instance();

        // New Custom event to handle things like pagination.
        $grav->fireEvent('onCollectionProcessed', new Event(['collection' => $collection]));

        // Slice and dice the collection if pagination is required
        if ($pagination) {
            $params = $collection->params();

            $limit = $params['limit'] ?? 0;
            $start = !empty($params['pagination']) ? ($uri->currentPage() - 1) * $limit : 0;

            if ($limit && $collection->count() > $limit) {
                $collection->slice($start, $limit);
            }
        }

        return $collection;
    }
165277
private function getPageInfo($paging, $total, $args) {
        $cur = $paging->page;
        $per = isset ($args ['limit']) ? intval($args ['limit']) : 10;
        if (!$per) {
            $per = 10;
        }

        $tp = ceil($total / $per); // 一共有多少页

        $pager ['total']  = $tp;
        $pager ['ctotal'] = $total;
        $pager ['first']  = '#';
        $pager ['prev']   = '#';
        $pager ['next']   = '#';
        $pager ['last']   = '#';

        if ($tp < 2) {
            return $pager;
        }

        $loop = true;
        if (isset ($args ['loop']) && empty ($args ['loop'])) {
            $loop = false;
        }
        $pages = [];
        if ($cur == 1) { // 当前在第一页
            $pager ['first'] = '#';
            $pager ['prev']  = '#';
        } else {
            $pager ['first'] = $paging->base(1);
            $pager ['prev']  = $cur == 2 ? $paging->base(1) : $paging->base($cur - 1);
        }
        // 向前后各多少页
        $pp = isset ($args ['pp']) ? intval($args ['pp']) : 10;
        $sp = $pp % 2 == 0 ? $pp / 2 : ($pp - 1) / 2;
        if ($cur <= $sp) {
            $start = 1;
            $end   = $pp;
            $end   = $end > $tp ? $tp : $end;
        } else {
            $start = $cur - $sp;
            $end   = $cur + $sp;
            if ($pp % 2 == 0) {
                $end -= 1;
            }
            if ($end >= $tp) {
                $start -= ($end - $tp);
                $start > 0 or $start = 1;
                $end = $tp;
            }
        }
        for ($i = $start; $i <= $end; $i++) {
            if ($i == $cur) {
                $pages[ $i ] = $pager [ $i ] = '#';
            } else {
                $pages[ $i ] = $pager [ $i ] = $paging->base($i);
            }
        }
        if ($cur == $tp) {
            $pager ['next'] = '#';
            $pager ['last'] = '#';
        } else {
            $pager ['next'] = $paging->base($cur + 1);
            $pager ['last'] = $paging->base($tp);
        }
        if (!$loop) {
            $pages['pages'] = $pages;
        }

        return $pager;
    }
1264090
public function paginate($limit = null, $columns = ['*'], $pageName = 'page', $page = null)
    {
        return $this->simpleWrap(Action::READ, [$this, '_paginate']);
    }
482149
public static function getItems($pagination, $params = [])
    {
        $pageCount = $pagination->getPageCount();
        if ($pageCount < 2) {
            return [];
        }
        static::$pagination = $pagination;
        $items = [];
        $currentPage = $pagination->getPage();
        static::$params = array_merge(
            static::$defaultParams,
            $params
        );
        // first page
        $firstPageLabel = static::$params['firstPageLabel'] === true ? '1' : static::$params['firstPageLabel'];
        if ($firstPageLabel !== false) {
            $items[] = static::buildPageItem($firstPageLabel, 0, static::$params['firstPageCssClass'], $currentPage <= 0, false);
        }
        // prev page
        if (static::$params['prevPageLabel'] !== false) {
            if (($page = $currentPage - 1) < 0) {
                $page = 0;
            }
            $items[] = static::buildPageItem(static::$params['prevPageLabel'], $page, static::$params['prevPageCssClass'], $currentPage <= 0, false);
        }
        // internal pages
        list($beginPage, $endPage) = static::getPageRange($pagination, static::$params);
        for ($i = $beginPage; $i <= $endPage; ++$i) {
            $items[] = static::buildPageItem($i + 1, $i, null, false, $i == $currentPage);
        }
        // next page
        if (static::$params['nextPageLabel'] !== false) {
            if (($page = $currentPage + 1) >= $pageCount - 1) {
                $page = $pageCount - 1;
            }
            $items[] = static::buildPageItem(static::$params['nextPageLabel'], $page, static::$params['nextPageCssClass'], $currentPage >= $pageCount - 1, false);
        }
        // last page
        $lastPageLabel = static::$params['lastPageLabel'] === true ? $pageCount : static::$params['lastPageLabel'];
        if ($lastPageLabel !== false) {
            $items[] = static::buildPageItem($lastPageLabel, $pageCount - 1, static::$params['lastPageCssClass'], $currentPage >= $pageCount - 1, false);
        }
        return $items;
    }
64724
protected function handlePaginationRequest($request, $model, $total = null)
    {
        $page = $request->pageNumber;
        $perPage = $request->pageSize;
        if (!$total) {
            $total = $model->count();
        }
        $results = $model->forPage($page, $perPage)->get(array('*'));
        $paginator = new LengthAwarePaginator($results, $total, $perPage, $page, [
            'path' => Paginator::resolveCurrentPath(),
            'pageName' => 'page[number]'
        ]);
        $paginator->appends('page[size]', $perPage);
        if (!empty($request->filter)) {
            foreach ($request->filter as $key=>$value) {
                $paginator->appends($key, $value);
            }
        }
        if (!empty($request->sort)) {
            $paginator->appends('sort', implode(',', $request->sort));
        }

        return $paginator;
    }
491436
public function LengthAwarePaginator($items, $resultsPerPage = 4)
    {
        //Get current page form url e.g. &page=6
        $currentPage = LengthAwarePaginator::resolveCurrentPage();

        //Create a new Laravel collection from data
        $collection = new Collection($items);

        //Slice the collection to get the items to display in current page
        $currentPageSearchResults = $collection->slice(($currentPage - 1) * $resultsPerPage, $resultsPerPage) -> all();

        //Create our paginator and pass it to the view
        return new LengthAwarePaginator($currentPageSearchResults, count($collection), $resultsPerPage);
    }
946419
public static int totalPage(int totalCount, int pageSize) {
		if (pageSize == 0) {
			return 0;
		}
		return totalCount % pageSize == 0 ? (totalCount / pageSize) : (totalCount / pageSize + 1);
	}
880473
public function paginate($perPage = 15, $columns = ['*'], $pageName = 'page', $page = null)
    {
        $per_page = ($this->request->has('per_page'))
            ? $this->request->get('per_page')
            : $perPage;

        $paginator = $this->db->paginate($per_page, $columns, $pageName, $page);

        $paginator->appends($this->request->all());

        return $paginator;
    }
1084464
public function getAll($pageSize = 200, $page = 1)
    {
        return $this->rest->get($this->action, [
            'pageSize' => $pageSize,
            'page' => $page
        ]);
    }
1172446
public function showPager(int $page = 0, int $total_rows = 0, int $per_page = 0)
    {
        $this->show_pager = true;
        $per_page = $per_page ?? $this->per_page;

        if (isset($_GET['per_page']) && ctype_digit((string)$_GET['per_page'])) {
            $per_page = (int)$_GET['per_page'];
        }

        $this->pager = new TablePager($page, $total_rows, $per_page);

        return $this;
    }
1244988
private function getTableCellWidth($total, $cells)
    {
        $widths = [];
        if (is_int($cells)) {
            $width = floor($total/$cells);
            for ($i=0; $i<$cells; ++$i) {
                $widths[] = $width;
            }
        }
        else if (is_array($cells)){
            $width = floor($total/count($cells));
            foreach ($cells as $i) {
                $widths[$i] = $width;
            }
        }
        return $widths;
    }
759740
public static function page( int $page , int $count , int $pagesize = 10) :array
    {

//        if(empty($count)){
//            throw new \Exception('Tools page throw : count must is integer and greater than 0!');
//        }
        if( empty($page) || !is_numeric($page) ){
            $page = 1;
        }

        if( empty($pagesize) || !is_numeric($pagesize)){
            $pagesize = 10;
        }

        $maxpage = ceil($count / $pagesize);//获取最大页数

        $limit = [($page - 1) * $pagesize,$pagesize];

        return [
            'page' => $page,
            'pagesize' => $pagesize,
            'count' => $count,
            'maxpage' => $maxpage,
            'limit' => $limit
        ];
    }
651877
private function createLinkClosure(int $pageSize): Closure
    {
        assert($pageSize > 0);

        parse_str($this->getOriginalUri()->getQuery(), $queryParams);

        return function ($offset) use ($pageSize, $queryParams) {
            $paramsWithPaging = array_merge($queryParams, [
                BaseQueryParserInterface::PARAM_PAGE => [
                    JsonApiQueryParserInterface::PARAM_PAGING_OFFSET => $offset,
                    JsonApiQueryParserInterface::PARAM_PAGING_LIMIT  => $pageSize,
                ],
            ]);
            $newUri  = $this->getOriginalUri()->withQuery(http_build_query($paramsWithPaging));
            $fullUrl = (string)$newUri;
            $link    = $this->getFactory()->createLink(false, $fullUrl, false);

            return $link;
        };
    }