Paginator.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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]', $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. public function total(): int
  208. {
  209. if ($this->simple) {
  210. throw new DomainException('not support total');
  211. }
  212. return $this->total;
  213. }
  214. public function listRows(): int
  215. {
  216. return $this->listRows;
  217. }
  218. public function currentPage(): int
  219. {
  220. return $this->currentPage;
  221. }
  222. public function lastPage(): int
  223. {
  224. if ($this->simple) {
  225. throw new DomainException('not support last');
  226. }
  227. return $this->lastPage;
  228. }
  229. /**
  230. * 数据是否足够分页
  231. * @access public
  232. * @return bool
  233. */
  234. public function hasPages(): bool
  235. {
  236. return !(1 == $this->currentPage && !$this->hasMore);
  237. }
  238. /**
  239. * 创建一组分页链接
  240. *
  241. * @access public
  242. * @param int $start
  243. * @param int $end
  244. * @return array
  245. */
  246. public function getUrlRange(int $start, int $end): array
  247. {
  248. $urls = [];
  249. for ($page = $start; $page <= $end; $page++) {
  250. $urls[$page] = $this->url($page);
  251. }
  252. return $urls;
  253. }
  254. /**
  255. * 设置URL锚点
  256. *
  257. * @access public
  258. * @param string|null $fragment
  259. * @return $this
  260. */
  261. public function fragment(string $fragment = null)
  262. {
  263. $this->options['fragment'] = $fragment;
  264. return $this;
  265. }
  266. /**
  267. * 添加URL参数
  268. *
  269. * @access public
  270. * @param array $append
  271. * @return $this
  272. */
  273. public function appends(array $append)
  274. {
  275. foreach ($append as $k => $v) {
  276. if ($k !== $this->options['var_page']) {
  277. $this->options['query'][$k] = $v;
  278. }
  279. }
  280. return $this;
  281. }
  282. /**
  283. * 构造锚点字符串
  284. *
  285. * @access public
  286. * @return string
  287. */
  288. protected function buildFragment(): string
  289. {
  290. return $this->options['fragment'] ? '#' . $this->options['fragment'] : '';
  291. }
  292. /**
  293. * 渲染分页html
  294. * @access public
  295. * @return mixed
  296. */
  297. abstract public function render();
  298. public function items()
  299. {
  300. return $this->items->all();
  301. }
  302. /**
  303. * 获取数据集
  304. *
  305. * @return Collection|\think\model\Collection
  306. */
  307. public function getCollection()
  308. {
  309. return $this->items;
  310. }
  311. public function isEmpty(): bool
  312. {
  313. return $this->items->isEmpty();
  314. }
  315. /**
  316. * 给每个元素执行个回调
  317. *
  318. * @access public
  319. * @param callable $callback
  320. * @return $this
  321. */
  322. public function each(callable $callback)
  323. {
  324. foreach ($this->items as $key => $item) {
  325. $result = $callback($item, $key);
  326. if (false === $result) {
  327. break;
  328. } elseif (!is_object($item)) {
  329. $this->items[$key] = $result;
  330. }
  331. }
  332. return $this;
  333. }
  334. /**
  335. * Retrieve an external iterator
  336. * @access public
  337. * @return Traversable An instance of an object implementing <b>Iterator</b> or
  338. * <b>Traversable</b>
  339. */
  340. public function getIterator()
  341. {
  342. return new ArrayIterator($this->items->all());
  343. }
  344. /**
  345. * Whether a offset exists
  346. * @access public
  347. * @param mixed $offset
  348. * @return bool
  349. */
  350. public function offsetExists($offset)
  351. {
  352. return $this->items->offsetExists($offset);
  353. }
  354. /**
  355. * Offset to retrieve
  356. * @access public
  357. * @param mixed $offset
  358. * @return mixed
  359. */
  360. public function offsetGet($offset)
  361. {
  362. return $this->items->offsetGet($offset);
  363. }
  364. /**
  365. * Offset to set
  366. * @access public
  367. * @param mixed $offset
  368. * @param mixed $value
  369. */
  370. public function offsetSet($offset, $value)
  371. {
  372. $this->items->offsetSet($offset, $value);
  373. }
  374. /**
  375. * Offset to unset
  376. * @access public
  377. * @param mixed $offset
  378. * @return void
  379. * @since 5.0.0
  380. */
  381. public function offsetUnset($offset)
  382. {
  383. $this->items->offsetUnset($offset);
  384. }
  385. /**
  386. * Count elements of an object
  387. */
  388. public function count(): int
  389. {
  390. return $this->items->count();
  391. }
  392. public function __toString()
  393. {
  394. return (string) $this->render();
  395. }
  396. public function toArray(): array
  397. {
  398. try {
  399. $total = $this->total();
  400. } catch (DomainException $e) {
  401. $total = null;
  402. }
  403. return [
  404. 'total' => $total,
  405. 'per_page' => $this->listRows(),
  406. 'current_page' => $this->currentPage(),
  407. 'last_page' => $this->lastPage,
  408. 'data' => $this->items->toArray(),
  409. ];
  410. }
  411. /**
  412. * Specify data which should be serialized to JSON
  413. */
  414. public function jsonSerialize()
  415. {
  416. return $this->toArray();
  417. }
  418. public function __call($name, $arguments)
  419. {
  420. $result = call_user_func_array([$this->items, $name], $arguments);
  421. if ($result instanceof Collection) {
  422. $this->items = $result;
  423. return $this;
  424. }
  425. return $result;
  426. }
  427. }