Paginator.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: zhangyajun <448901948@qq.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types = 1);
  12. namespace think;
  13. use ArrayAccess;
  14. use ArrayIterator;
  15. use Closure;
  16. use Countable;
  17. use DomainException;
  18. use IteratorAggregate;
  19. use JsonSerializable;
  20. use think\paginator\driver\Bootstrap;
  21. use Traversable;
  22. /**
  23. * 分页基础类
  24. * @mixin Collection
  25. */
  26. abstract class Paginator implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable
  27. {
  28. /**
  29. * 是否简洁模式
  30. * @var bool
  31. */
  32. protected $simple = false;
  33. /**
  34. * 数据集
  35. * @var Collection
  36. */
  37. protected $items;
  38. /**
  39. * 当前页
  40. * @var int
  41. */
  42. protected $currentPage;
  43. /**
  44. * 最后一页
  45. * @var int
  46. */
  47. protected $lastPage;
  48. /**
  49. * 数据总数
  50. * @var integer|null
  51. */
  52. protected $total;
  53. /**
  54. * 每页数量
  55. * @var int
  56. */
  57. protected $listRows;
  58. /**
  59. * 是否有下一页
  60. * @var bool
  61. */
  62. protected $hasMore;
  63. /**
  64. * 分页配置
  65. * @var array
  66. */
  67. protected $options = [
  68. 'var_page' => 'page',
  69. 'path' => '/',
  70. 'query' => [],
  71. 'fragment' => '',
  72. ];
  73. /**
  74. * 获取当前页码
  75. * @var Closure
  76. */
  77. protected static $currentPageResolver;
  78. /**
  79. * 获取当前路径
  80. * @var Closure
  81. */
  82. protected static $currentPathResolver;
  83. /**
  84. * @var Closure
  85. */
  86. protected static $maker;
  87. public function __construct($items, int $listRows, int $currentPage = 1, int $total = null, bool $simple = false, array $options = [])
  88. {
  89. $this->options = array_merge($this->options, $options);
  90. $this->options['path'] = '/' != $this->options['path'] ? rtrim($this->options['path'], '/') : $this->options['path'];
  91. $this->simple = $simple;
  92. $this->listRows = $listRows;
  93. if (!$items instanceof Collection) {
  94. $items = Collection::make($items);
  95. }
  96. if ($simple) {
  97. $this->currentPage = $this->setCurrentPage($currentPage);
  98. $this->hasMore = count($items) > ($this->listRows);
  99. $items = $items->slice(0, $this->listRows);
  100. } else {
  101. $this->total = $total;
  102. $this->lastPage = (int) ceil($total / $listRows);
  103. $this->currentPage = $this->setCurrentPage($currentPage);
  104. $this->hasMore = $this->currentPage < $this->lastPage;
  105. }
  106. $this->items = $items;
  107. }
  108. /**
  109. * @access public
  110. * @param mixed $items
  111. * @param int $listRows
  112. * @param int $currentPage
  113. * @param int $total
  114. * @param bool $simple
  115. * @param array $options
  116. * @return Paginator
  117. */
  118. public static function make($items, int $listRows, int $currentPage = 1, int $total = null, bool $simple = false, array $options = [])
  119. {
  120. if (isset(static::$maker)) {
  121. return call_user_func(static::$maker, $items, $listRows, $currentPage, $total, $simple, $options);
  122. }
  123. return new Bootstrap($items, $listRows, $currentPage, $total, $simple, $options);
  124. }
  125. public static function maker(Closure $resolver)
  126. {
  127. static::$maker = $resolver;
  128. }
  129. protected function setCurrentPage(int $currentPage): int
  130. {
  131. if (!$this->simple && $currentPage > $this->lastPage) {
  132. return $this->lastPage > 0 ? $this->lastPage : 1;
  133. }
  134. return $currentPage;
  135. }
  136. /**
  137. * 获取页码对应的链接
  138. *
  139. * @access protected
  140. * @param int $page
  141. * @return string
  142. */
  143. protected function url(int $page): string
  144. {
  145. if ($page <= 0) {
  146. $page = 1;
  147. }
  148. if (strpos($this->options['path'], '[PAGE]') === false) {
  149. $parameters = [$this->options['var_page'] => $page];
  150. $path = $this->options['path'];
  151. } else {
  152. $parameters = [];
  153. $path = str_replace('[PAGE]', (string) $page, $this->options['path']);
  154. }
  155. if (count($this->options['query']) > 0) {
  156. $parameters = array_merge($this->options['query'], $parameters);
  157. }
  158. $url = $path;
  159. if (!empty($parameters)) {
  160. $url .= '?' . http_build_query($parameters, '', '&');
  161. }
  162. return $url . $this->buildFragment();
  163. }
  164. /**
  165. * 自动获取当前页码
  166. * @access public
  167. * @param string $varPage
  168. * @param int $default
  169. * @return int
  170. */
  171. public static function getCurrentPage(string $varPage = 'page', int $default = 1): int
  172. {
  173. if (isset(static::$currentPageResolver)) {
  174. return call_user_func(static::$currentPageResolver, $varPage);
  175. }
  176. return $default;
  177. }
  178. /**
  179. * 设置获取当前页码闭包
  180. * @param Closure $resolver
  181. */
  182. public static function currentPageResolver(Closure $resolver)
  183. {
  184. static::$currentPageResolver = $resolver;
  185. }
  186. /**
  187. * 自动获取当前的path
  188. * @access public
  189. * @param string $default
  190. * @return string
  191. */
  192. public static function getCurrentPath($default = '/'): string
  193. {
  194. if (isset(static::$currentPathResolver)) {
  195. return call_user_func(static::$currentPathResolver);
  196. }
  197. return $default;
  198. }
  199. /**
  200. * 设置获取当前路径闭包
  201. * @param Closure $resolver
  202. */
  203. public static function currentPathResolver(Closure $resolver)
  204. {
  205. static::$currentPathResolver = $resolver;
  206. }
  207. /**
  208. * 获取数据总条数
  209. * @return int
  210. */
  211. public function total(): int
  212. {
  213. if ($this->simple) {
  214. throw new DomainException('not support total');
  215. }
  216. return $this->total;
  217. }
  218. /**
  219. * 获取每页数量
  220. * @return int
  221. */
  222. public function listRows(): int
  223. {
  224. return $this->listRows;
  225. }
  226. /**
  227. * 获取当前页页码
  228. * @return int
  229. */
  230. public function currentPage(): int
  231. {
  232. return $this->currentPage;
  233. }
  234. /**
  235. * 获取最后一页页码
  236. * @return int
  237. */
  238. public function lastPage(): int
  239. {
  240. if ($this->simple) {
  241. throw new DomainException('not support last');
  242. }
  243. return $this->lastPage;
  244. }
  245. /**
  246. * 数据是否足够分页
  247. * @access public
  248. * @return bool
  249. */
  250. public function hasPages(): bool
  251. {
  252. return !(1 == $this->currentPage && !$this->hasMore);
  253. }
  254. /**
  255. * 创建一组分页链接
  256. *
  257. * @access public
  258. * @param int $start
  259. * @param int $end
  260. * @return array
  261. */
  262. public function getUrlRange(int $start, int $end): array
  263. {
  264. $urls = [];
  265. for ($page = $start; $page <= $end; $page++) {
  266. $urls[$page] = $this->url($page);
  267. }
  268. return $urls;
  269. }
  270. /**
  271. * 设置URL锚点
  272. *
  273. * @access public
  274. * @param string|null $fragment
  275. * @return $this
  276. */
  277. public function fragment(string $fragment = null)
  278. {
  279. $this->options['fragment'] = $fragment;
  280. return $this;
  281. }
  282. /**
  283. * 添加URL参数
  284. *
  285. * @access public
  286. * @param array $append
  287. * @return $this
  288. */
  289. public function appends(array $append)
  290. {
  291. foreach ($append as $k => $v) {
  292. if ($k !== $this->options['var_page']) {
  293. $this->options['query'][$k] = $v;
  294. }
  295. }
  296. return $this;
  297. }
  298. /**
  299. * 构造锚点字符串
  300. *
  301. * @access public
  302. * @return string
  303. */
  304. protected function buildFragment(): string
  305. {
  306. return $this->options['fragment'] ? '#' . $this->options['fragment'] : '';
  307. }
  308. /**
  309. * 渲染分页html
  310. * @access public
  311. * @return mixed
  312. */
  313. abstract public function render();
  314. public function items()
  315. {
  316. return $this->items->all();
  317. }
  318. /**
  319. * 获取数据集
  320. *
  321. * @return Collection|\think\model\Collection
  322. */
  323. public function getCollection()
  324. {
  325. return $this->items;
  326. }
  327. /**
  328. * 设置数据集
  329. *
  330. * @param Collection $items
  331. * @return $this
  332. */
  333. public function setCollection(Collection $items)
  334. {
  335. $this->items = $items;
  336. return $this;
  337. }
  338. public function isEmpty(): bool
  339. {
  340. return $this->items->isEmpty();
  341. }
  342. /**
  343. * 给每个元素执行个回调
  344. *
  345. * @access public
  346. * @param callable $callback
  347. * @return $this
  348. */
  349. public function each(callable $callback)
  350. {
  351. foreach ($this->items as $key => $item) {
  352. $result = $callback($item, $key);
  353. if (false === $result) {
  354. break;
  355. } elseif (!is_object($item)) {
  356. $this->items[$key] = $result;
  357. }
  358. }
  359. return $this;
  360. }
  361. /**
  362. * Retrieve an external iterator
  363. * @access public
  364. * @return Traversable An instance of an object implementing <b>Iterator</b> or
  365. * <b>Traversable</b>
  366. */
  367. #[\ReturnTypeWillChange]
  368. public function getIterator(): Traversable
  369. {
  370. return new ArrayIterator($this->items->all());
  371. }
  372. /**
  373. * Whether a offset exists
  374. * @access public
  375. * @param mixed $offset
  376. * @return bool
  377. */
  378. #[\ReturnTypeWillChange]
  379. public function offsetExists($offset): bool
  380. {
  381. return $this->items->offsetExists($offset);
  382. }
  383. /**
  384. * Offset to retrieve
  385. * @access public
  386. * @param mixed $offset
  387. * @return mixed
  388. */
  389. #[\ReturnTypeWillChange]
  390. public function offsetGet($offset)
  391. {
  392. return $this->items->offsetGet($offset);
  393. }
  394. /**
  395. * Offset to set
  396. * @access public
  397. * @param mixed $offset
  398. * @param mixed $value
  399. */
  400. #[\ReturnTypeWillChange]
  401. public function offsetSet($offset, $value)
  402. {
  403. $this->items->offsetSet($offset, $value);
  404. }
  405. /**
  406. * Offset to unset
  407. * @access public
  408. * @param mixed $offset
  409. * @return void
  410. * @since 5.0.0
  411. */
  412. #[\ReturnTypeWillChange]
  413. public function offsetUnset($offset)
  414. {
  415. $this->items->offsetUnset($offset);
  416. }
  417. /**
  418. * 统计数据集条数
  419. * @return int
  420. */
  421. public function count(): int
  422. {
  423. return $this->items->count();
  424. }
  425. public function __toString()
  426. {
  427. return (string) $this->render();
  428. }
  429. /**
  430. * 转换为数组
  431. * @return array
  432. */
  433. public function toArray(): array
  434. {
  435. try {
  436. $total = $this->total();
  437. } catch (DomainException $e) {
  438. $total = null;
  439. }
  440. return [
  441. 'total' => $total,
  442. 'per_page' => $this->listRows(),
  443. 'current_page' => $this->currentPage(),
  444. 'last_page' => $this->lastPage,
  445. 'data' => $this->items->toArray(),
  446. ];
  447. }
  448. /**
  449. * Specify data which should be serialized to JSON
  450. */
  451. #[\ReturnTypeWillChange]
  452. public function jsonSerialize()
  453. {
  454. return $this->toArray();
  455. }
  456. public function __call($name, $arguments)
  457. {
  458. $result = call_user_func_array([$this->items, $name], $arguments);
  459. if ($result instanceof Collection) {
  460. $this->items = $result;
  461. return $this;
  462. }
  463. return $result;
  464. }
  465. }