Paginator.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2018 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. namespace think;
  12. use ArrayAccess;
  13. use ArrayIterator;
  14. use Countable;
  15. use IteratorAggregate;
  16. use JsonSerializable;
  17. use Traversable;
  18. abstract class Paginator implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable
  19. {
  20. /** @var bool 是否为简洁模式 */
  21. protected $simple = false;
  22. /** @var Collection 数据集 */
  23. protected $items;
  24. /** @var integer 当前页 */
  25. protected $currentPage;
  26. /** @var integer 最后一页 */
  27. protected $lastPage;
  28. /** @var integer|null 数据总数 */
  29. protected $total;
  30. /** @var integer 每页的数量 */
  31. protected $listRows;
  32. /** @var bool 是否有下一页 */
  33. protected $hasMore;
  34. /** @var array 一些配置 */
  35. protected $options = [
  36. 'var_page' => 'page',
  37. 'path' => '/',
  38. 'query' => [],
  39. 'fragment' => '',
  40. ];
  41. /** @var mixed simple模式下的下个元素 */
  42. protected $nextItem;
  43. public function __construct($items, $listRows, $currentPage = null, $total = null, $simple = false, $options = [])
  44. {
  45. $this->options = array_merge($this->options, $options);
  46. $this->options['path'] = '/' != $this->options['path'] ? rtrim($this->options['path'], '/') : $this->options['path'];
  47. $this->simple = $simple;
  48. $this->listRows = $listRows;
  49. if (!$items instanceof Collection) {
  50. $items = Collection::make($items);
  51. }
  52. if ($simple) {
  53. $this->currentPage = $this->setCurrentPage($currentPage);
  54. $this->hasMore = count($items) > ($this->listRows);
  55. if ($this->hasMore) {
  56. $this->nextItem = $items->slice($this->listRows, 1);
  57. }
  58. $items = $items->slice(0, $this->listRows);
  59. } else {
  60. $this->total = $total;
  61. $this->lastPage = (int) ceil($total / $listRows);
  62. $this->currentPage = $this->setCurrentPage($currentPage);
  63. $this->hasMore = $this->currentPage < $this->lastPage;
  64. }
  65. $this->items = $items;
  66. }
  67. /**
  68. * @param $items
  69. * @param $listRows
  70. * @param null $currentPage
  71. * @param bool $simple
  72. * @param null $total
  73. * @param array $options
  74. * @return Paginator
  75. */
  76. public static function make($items, $listRows, $currentPage = null, $total = null, $simple = false, $options = [])
  77. {
  78. return new static($items, $listRows, $currentPage, $total, $simple, $options);
  79. }
  80. protected function setCurrentPage($currentPage)
  81. {
  82. if (!$this->simple && $currentPage > $this->lastPage) {
  83. return $this->lastPage > 0 ? $this->lastPage : 1;
  84. }
  85. return $currentPage;
  86. }
  87. /**
  88. * 获取页码对应的链接
  89. *
  90. * @param $page
  91. *
  92. * @param int $list_rows
  93. *
  94. * @return string
  95. */
  96. protected function url($page, $list_rows = 0)
  97. {
  98. if ($page <= 0) {
  99. $page = 1;
  100. }
  101. if (strpos($this->options['path'], '[PAGE]') === false) {
  102. $parameters = [$this->options['var_page'] => $page];
  103. $path = $this->options['path'];
  104. } else {
  105. $parameters = [];
  106. $path = str_replace('[PAGE]', $page, $this->options['path']);
  107. }
  108. /* Modified by wuyonghong BEGIN 2018/12/19 ISSUES 数据分页 */
  109. $_rq_param = request()->param();
  110. if (!empty($_rq_param)) {
  111. $parameters = array_merge($_rq_param, $parameters);
  112. }
  113. if (count($this->options['query']) > 0) {
  114. $parameters = array_merge($this->options['query'], $parameters);
  115. }
  116. if ($list_rows > 0) {
  117. $parameters = array_merge($parameters, ['list_rows' => $list_rows]);
  118. }
  119. /* END 2018/12/19 ISSUES: */
  120. $url = $path;
  121. if (!empty($parameters)) {
  122. $url .= '?' . http_build_query($parameters, null, '&');
  123. }
  124. return $url . $this->buildFragment();
  125. }
  126. /**
  127. * 自动获取当前页码
  128. * @param string $varPage
  129. * @param int $default
  130. * @return int
  131. */
  132. public static function getCurrentPage($varPage = 'page', $default = 1)
  133. {
  134. $page = (int) Request::instance()->param($varPage);
  135. if (filter_var($page, FILTER_VALIDATE_INT) !== false && $page >= 1) {
  136. return $page;
  137. }
  138. return $default;
  139. }
  140. /**
  141. * 自动获取当前的path
  142. * @return string
  143. */
  144. public static function getCurrentPath()
  145. {
  146. return Request::instance()->baseUrl();
  147. }
  148. public function total()
  149. {
  150. if ($this->simple) {
  151. throw new \DomainException('not support total');
  152. }
  153. return $this->total;
  154. }
  155. public function listRows()
  156. {
  157. return $this->listRows;
  158. }
  159. public function currentPage()
  160. {
  161. return $this->currentPage;
  162. }
  163. public function lastPage()
  164. {
  165. if ($this->simple) {
  166. throw new \DomainException('not support last');
  167. }
  168. return $this->lastPage;
  169. }
  170. /**
  171. * 数据是否足够分页
  172. * @return boolean
  173. */
  174. public function hasPages()
  175. {
  176. return !(1 == $this->currentPage && !$this->hasMore);
  177. }
  178. /**
  179. * 创建一组分页链接
  180. *
  181. * @param int $start
  182. * @param int $end
  183. * @return array
  184. */
  185. public function getUrlRange($start, $end)
  186. {
  187. $urls = [];
  188. for ($page = $start; $page <= $end; $page++) {
  189. $urls[$page] = $this->url($page);
  190. }
  191. return $urls;
  192. }
  193. /**
  194. * 设置URL锚点
  195. *
  196. * @param string|null $fragment
  197. * @return $this
  198. */
  199. public function fragment($fragment)
  200. {
  201. $this->options['fragment'] = $fragment;
  202. return $this;
  203. }
  204. /**
  205. * 添加URL参数
  206. *
  207. * @param array|string $key
  208. * @param string|null $value
  209. * @return $this
  210. */
  211. public function appends($key, $value = null)
  212. {
  213. if (!is_array($key)) {
  214. $queries = [$key => $value];
  215. } else {
  216. $queries = $key;
  217. }
  218. foreach ($queries as $k => $v) {
  219. if ($k !== $this->options['var_page']) {
  220. $this->options['query'][$k] = $v;
  221. }
  222. }
  223. return $this;
  224. }
  225. /**
  226. * 构造锚点字符串
  227. *
  228. * @return string
  229. */
  230. protected function buildFragment()
  231. {
  232. return $this->options['fragment'] ? '#' . $this->options['fragment'] : '';
  233. }
  234. /**
  235. * 渲染分页html
  236. * @return mixed
  237. */
  238. abstract public function render();
  239. public function items()
  240. {
  241. return $this->items->all();
  242. }
  243. public function getCollection()
  244. {
  245. return $this->items;
  246. }
  247. public function isEmpty()
  248. {
  249. return $this->items->isEmpty();
  250. }
  251. /**
  252. * 给每个元素执行个回调
  253. *
  254. * @param callable $callback
  255. * @return $this
  256. */
  257. public function each(callable $callback)
  258. {
  259. foreach ($this->items as $key => $item) {
  260. $result = $callback($item, $key);
  261. if (false === $result) {
  262. break;
  263. } elseif (!is_object($item)) {
  264. $this->items[$key] = $result;
  265. }
  266. }
  267. return $this;
  268. }
  269. /**
  270. * Retrieve an external iterator
  271. * @return Traversable An instance of an object implementing <b>Iterator</b> or
  272. * <b>Traversable</b>
  273. */
  274. public function getIterator()
  275. {
  276. return new ArrayIterator($this->items->all());
  277. }
  278. /**
  279. * Whether a offset exists
  280. * @param mixed $offset
  281. * @return bool
  282. */
  283. public function offsetExists($offset)
  284. {
  285. return $this->items->offsetExists($offset);
  286. }
  287. /**
  288. * Offset to retrieve
  289. * @param mixed $offset
  290. * @return mixed
  291. */
  292. public function offsetGet($offset)
  293. {
  294. return $this->items->offsetGet($offset);
  295. }
  296. /**
  297. * Offset to set
  298. * @param mixed $offset
  299. * @param mixed $value
  300. */
  301. public function offsetSet($offset, $value)
  302. {
  303. $this->items->offsetSet($offset, $value);
  304. }
  305. /**
  306. * Offset to unset
  307. * @param mixed $offset
  308. * @return void
  309. * @since 5.0.0
  310. */
  311. public function offsetUnset($offset)
  312. {
  313. $this->items->offsetUnset($offset);
  314. }
  315. /**
  316. * Count elements of an object
  317. */
  318. public function count()
  319. {
  320. return $this->items->count();
  321. }
  322. public function __toString()
  323. {
  324. return (string) $this->render();
  325. }
  326. public function toArray()
  327. {
  328. if ($this->simple) {
  329. return [
  330. 'per_page' => $this->listRows,
  331. 'current_page' => $this->currentPage,
  332. 'has_more' => $this->hasMore,
  333. 'next_item' => $this->nextItem,
  334. 'data' => $this->items->toArray(),
  335. ];
  336. } else {
  337. return [
  338. 'total' => $this->total,
  339. 'per_page' => $this->listRows,
  340. 'current_page' => $this->currentPage,
  341. 'last_page' => $this->lastPage,
  342. 'data' => $this->items->toArray(),
  343. ];
  344. }
  345. }
  346. /**
  347. * Specify data which should be serialized to JSON
  348. */
  349. public function jsonSerialize()
  350. {
  351. return $this->toArray();
  352. }
  353. public function __call($name, $arguments)
  354. {
  355. $collection = $this->getCollection();
  356. $result = call_user_func_array([$collection, $name], $arguments);
  357. if ($result === $collection) {
  358. return $this;
  359. }
  360. return $result;
  361. }
  362. }