Paginator.php 9.1 KB

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