PageRenderTime 42ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/blog/vendor/laravel/framework/src/Illuminate/Pagination/AbstractPaginator.php

https://gitlab.com/zan_zan/laravel_sample
PHP | 487 lines | 185 code | 59 blank | 243 comment | 12 complexity | d111421017f28d6116169ab20a669c3e MD5 | raw file
  1. <?php
  2. namespace Illuminate\Pagination;
  3. use Closure;
  4. use ArrayIterator;
  5. abstract class AbstractPaginator
  6. {
  7. /**
  8. * All of the items being paginated.
  9. *
  10. * @var \Illuminate\Support\Collection
  11. */
  12. protected $items;
  13. /**
  14. * The number of items to be shown per page.
  15. *
  16. * @var int
  17. */
  18. protected $perPage;
  19. /**
  20. * The current page being "viewed".
  21. *
  22. * @var int
  23. */
  24. protected $currentPage;
  25. /**
  26. * The base path to assign to all URLs.
  27. *
  28. * @var string
  29. */
  30. protected $path = '/';
  31. /**
  32. * The query parameters to add to all URLs.
  33. *
  34. * @var array
  35. */
  36. protected $query = [];
  37. /**
  38. * The URL fragment to add to all URLs.
  39. *
  40. * @var string|null
  41. */
  42. protected $fragment = null;
  43. /**
  44. * The query string variable used to store the page.
  45. *
  46. * @var string
  47. */
  48. protected $pageName = 'page';
  49. /**
  50. * The current page resolver callback.
  51. *
  52. * @var \Closure
  53. */
  54. protected static $currentPathResolver;
  55. /**
  56. * The current page resolver callback.
  57. *
  58. * @var \Closure
  59. */
  60. protected static $currentPageResolver;
  61. /**
  62. * The default presenter resolver.
  63. *
  64. * @var \Closure
  65. */
  66. protected static $presenterResolver;
  67. /**
  68. * Determine if the given value is a valid page number.
  69. *
  70. * @param int $page
  71. * @return bool
  72. */
  73. protected function isValidPageNumber($page)
  74. {
  75. return $page >= 1 && filter_var($page, FILTER_VALIDATE_INT) !== false;
  76. }
  77. /**
  78. * Create a range of pagination URLs.
  79. *
  80. * @param int $start
  81. * @param int $end
  82. * @return string
  83. */
  84. public function getUrlRange($start, $end)
  85. {
  86. $urls = [];
  87. for ($page = $start; $page <= $end; $page++) {
  88. $urls[$page] = $this->url($page);
  89. }
  90. return $urls;
  91. }
  92. /**
  93. * Get a URL for a given page number.
  94. *
  95. * @param int $page
  96. * @return string
  97. */
  98. public function url($page)
  99. {
  100. if ($page <= 0) {
  101. $page = 1;
  102. }
  103. // If we have any extra query string key / value pairs that need to be added
  104. // onto the URL, we will put them in query string form and then attach it
  105. // to the URL. This allows for extra information like sortings storage.
  106. $parameters = [$this->pageName => $page];
  107. if (count($this->query) > 0) {
  108. $parameters = array_merge($this->query, $parameters);
  109. }
  110. return $this->path.'?'
  111. .http_build_query($parameters, null, '&')
  112. .$this->buildFragment();
  113. }
  114. /**
  115. * Get the URL for the previous page.
  116. *
  117. * @return string|null
  118. */
  119. public function previousPageUrl()
  120. {
  121. if ($this->currentPage() > 1) {
  122. return $this->url($this->currentPage() - 1);
  123. }
  124. }
  125. /**
  126. * Get / set the URL fragment to be appended to URLs.
  127. *
  128. * @param string|null $fragment
  129. * @return $this|string|null
  130. */
  131. public function fragment($fragment = null)
  132. {
  133. if (is_null($fragment)) {
  134. return $this->fragment;
  135. }
  136. $this->fragment = $fragment;
  137. return $this;
  138. }
  139. /**
  140. * Add a set of query string values to the paginator.
  141. *
  142. * @param array|string $key
  143. * @param string|null $value
  144. * @return $this
  145. */
  146. public function appends($key, $value = null)
  147. {
  148. if (is_array($key)) {
  149. return $this->appendArray($key);
  150. }
  151. return $this->addQuery($key, $value);
  152. }
  153. /**
  154. * Add an array of query string values.
  155. *
  156. * @param array $keys
  157. * @return $this
  158. */
  159. protected function appendArray(array $keys)
  160. {
  161. foreach ($keys as $key => $value) {
  162. $this->addQuery($key, $value);
  163. }
  164. return $this;
  165. }
  166. /**
  167. * Add a query string value to the paginator.
  168. *
  169. * @param string $key
  170. * @param string $value
  171. * @return $this
  172. */
  173. public function addQuery($key, $value)
  174. {
  175. if ($key !== $this->pageName) {
  176. $this->query[$key] = $value;
  177. }
  178. return $this;
  179. }
  180. /**
  181. * Build the full fragment portion of a URL.
  182. *
  183. * @return string
  184. */
  185. protected function buildFragment()
  186. {
  187. return $this->fragment ? '#'.$this->fragment : '';
  188. }
  189. /**
  190. * Get the slice of items being paginated.
  191. *
  192. * @return array
  193. */
  194. public function items()
  195. {
  196. return $this->items->all();
  197. }
  198. /**
  199. * Get the number of the first item in the slice.
  200. *
  201. * @return int
  202. */
  203. public function firstItem()
  204. {
  205. return ($this->currentPage - 1) * $this->perPage + 1;
  206. }
  207. /**
  208. * Get the number of the last item in the slice.
  209. *
  210. * @return int
  211. */
  212. public function lastItem()
  213. {
  214. return $this->firstItem() + $this->count() - 1;
  215. }
  216. /**
  217. * Get the number of items shown per page.
  218. *
  219. * @return int
  220. */
  221. public function perPage()
  222. {
  223. return $this->perPage;
  224. }
  225. /**
  226. * Get the current page.
  227. *
  228. * @return int
  229. */
  230. public function currentPage()
  231. {
  232. return $this->currentPage;
  233. }
  234. /**
  235. * Determine if there are enough items to split into multiple pages.
  236. *
  237. * @return bool
  238. */
  239. public function hasPages()
  240. {
  241. return !($this->currentPage() == 1 && !$this->hasMorePages());
  242. }
  243. /**
  244. * Resolve the current request path or return the default value.
  245. *
  246. * @param string $default
  247. * @return string
  248. */
  249. public static function resolveCurrentPath($default = '/')
  250. {
  251. if (isset(static::$currentPathResolver)) {
  252. return call_user_func(static::$currentPathResolver);
  253. }
  254. return $default;
  255. }
  256. /**
  257. * Set the current request path resolver callback.
  258. *
  259. * @param \Closure $resolver
  260. * @return void
  261. */
  262. public static function currentPathResolver(Closure $resolver)
  263. {
  264. static::$currentPathResolver = $resolver;
  265. }
  266. /**
  267. * Resolve the current page or return the default value.
  268. *
  269. * @param string $pageName
  270. * @param int $default
  271. * @return int
  272. */
  273. public static function resolveCurrentPage($pageName = 'page', $default = 1)
  274. {
  275. if (isset(static::$currentPageResolver)) {
  276. return call_user_func(static::$currentPageResolver, $pageName);
  277. }
  278. return $default;
  279. }
  280. /**
  281. * Set the current page resolver callback.
  282. *
  283. * @param \Closure $resolver
  284. * @return void
  285. */
  286. public static function currentPageResolver(Closure $resolver)
  287. {
  288. static::$currentPageResolver = $resolver;
  289. }
  290. /**
  291. * Set the default Presenter resolver.
  292. *
  293. * @param \Closure $resolver
  294. * @return void
  295. */
  296. public static function presenter(Closure $resolver)
  297. {
  298. static::$presenterResolver = $resolver;
  299. }
  300. /**
  301. * Get the query string variable used to store the page.
  302. *
  303. * @return string
  304. */
  305. public function getPageName()
  306. {
  307. return $this->pageName;
  308. }
  309. /**
  310. * Set the query string variable used to store the page.
  311. *
  312. * @param string $name
  313. * @return $this
  314. */
  315. public function setPageName($name)
  316. {
  317. $this->pageName = $name;
  318. return $this;
  319. }
  320. /**
  321. * Set the base path to assign to all URLs.
  322. *
  323. * @param string $path
  324. * @return $this
  325. */
  326. public function setPath($path)
  327. {
  328. $this->path = $path;
  329. return $this;
  330. }
  331. /**
  332. * Get an iterator for the items.
  333. *
  334. * @return \ArrayIterator
  335. */
  336. public function getIterator()
  337. {
  338. return new ArrayIterator($this->items->all());
  339. }
  340. /**
  341. * Determine if the list of items is empty or not.
  342. *
  343. * @return bool
  344. */
  345. public function isEmpty()
  346. {
  347. return $this->items->isEmpty();
  348. }
  349. /**
  350. * Get the number of items for the current page.
  351. *
  352. * @return int
  353. */
  354. public function count()
  355. {
  356. return $this->items->count();
  357. }
  358. /**
  359. * Get the paginator's underlying collection.
  360. *
  361. * @return \Illuminate\Support\Collection
  362. */
  363. public function getCollection()
  364. {
  365. return $this->items;
  366. }
  367. /**
  368. * Determine if the given item exists.
  369. *
  370. * @param mixed $key
  371. * @return bool
  372. */
  373. public function offsetExists($key)
  374. {
  375. return $this->items->has($key);
  376. }
  377. /**
  378. * Get the item at the given offset.
  379. *
  380. * @param mixed $key
  381. * @return mixed
  382. */
  383. public function offsetGet($key)
  384. {
  385. return $this->items->get($key);
  386. }
  387. /**
  388. * Set the item at the given offset.
  389. *
  390. * @param mixed $key
  391. * @param mixed $value
  392. * @return void
  393. */
  394. public function offsetSet($key, $value)
  395. {
  396. $this->items->put($key, $value);
  397. }
  398. /**
  399. * Unset the item at the given key.
  400. *
  401. * @param mixed $key
  402. * @return void
  403. */
  404. public function offsetUnset($key)
  405. {
  406. $this->items->forget($key);
  407. }
  408. /**
  409. * Make dynamic calls into the collection.
  410. *
  411. * @param string $method
  412. * @param array $parameters
  413. * @return mixed
  414. */
  415. public function __call($method, $parameters)
  416. {
  417. return call_user_func_array([$this->getCollection(), $method], $parameters);
  418. }
  419. /**
  420. * Render the contents of the paginator when casting to string.
  421. *
  422. * @return string
  423. */
  424. public function __toString()
  425. {
  426. return $this->render();
  427. }
  428. }