| 123456789101112131415161718192021222324252627282930313233343536 |
- <?php
- declare(strict_types=1);
- namespace app\miniapi\http;
- final class Pagination
- {
- public readonly int $page;
- public readonly int $pageSize;
- public function __construct(array $query)
- {
- $this->page = Input::integer($query['page'] ?? 1, 'page', 1, 1000000);
- $this->pageSize = Input::integer($query['page_size'] ?? config('mini_api.default_page_size', 20), 'page_size', 1, (int) config('mini_api.max_page_size', 100));
- if (config('mini_protection.enabled', false) && $this->offset() >= (int) config('mini_protection.max_list_offset', 10000)) {
- throw new \app\miniapi\exception\ApiException(42201, '查询范围过大,请缩小筛选范围', 422, ['page' => '超出允许的分页查询范围']);
- }
- }
- public function offset(): int
- {
- return ($this->page - 1) * $this->pageSize;
- }
- public function result(array $items, int $total): array
- {
- return [
- 'items' => array_values($items),
- 'total' => $total,
- 'page' => $this->page,
- 'page_size' => $this->pageSize,
- 'has_more' => $this->offset() + count($items) < $total,
- ];
- }
- }
|