標籤:thinkphp
<?php// +----------------------------------------------------------------------// | ThinkPHP [ WE CAN DO IT JUST THINK ]// +----------------------------------------------------------------------// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.// +----------------------------------------------------------------------// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )// +----------------------------------------------------------------------// | Author: zhangyajun <[email protected]>// +----------------------------------------------------------------------namespace think;use think\paginator\Collection as PaginatorCollection;use think\Request;abstract class Paginator// 分頁 抽象類別 有意思,哈哈{ /** @var bool 是否為簡潔模式 */ protected $simple = false;// 是否為簡介模式 /** @var PaginatorCollection 資料集 */ protected $items;// 資料集合 /** @var integer 當前頁 */ protected $currentPage;// 當前頁面 /** @var integer 最後一頁 */ protected $lastPage;// 最後一頁 /** @var integer|null 資料總數 */ protected $total;// 資料總數 /** @var integer 每頁的數量 */ protected $listRows;// 每頁的數量 /** @var bool 是否有下一頁 */ protected $hasMore;// 是否下一頁 /** @var array 一些配置 */ protected $options = [ ‘var_page‘ => ‘page‘, ‘path‘ => ‘/‘, ‘query‘ => [], ‘fragment‘ => ‘‘ ];// 配置選項 分頁變數 page 預設路徑 path 為/ 執行的語句 是否有錨點,哈哈 protected function __construct($items, $listRows, $currentPage = null, $total = null, $simple = false, $options = []) {// 建構函式 資料集合 每頁行數 當前頁 總數 是否簡化模式 其它選項 $this->options = array_merge($this->options, $options);// 合并選項 配置大於約定 $this->options[‘path‘] = $this->options[‘path‘] != ‘/‘ ? rtrim($this->options[‘path‘], ‘/‘) : $this->options[‘path‘];// 路徑 及 路徑設定 將路徑中 最後的地方 清除 後面的斜杠 $this->simple = $simple;// 是否簡單模式 $this->listRows = $listRows; // 是否下一頁 我也是醉了 if ($simple) {// 如果是簡單 模式 if (!$items instanceof Collection) {// 如果 資料集合 不是 收集類格式,轉換一下 $items = Collection::make($items); } $this->currentPage = $this->setCurrentPage($currentPage);// 擷取當前 分頁 $this->hasMore = count($items) > ($this->listRows);// 更多的選項 $items = $items->slice(0, $this->listRows);//擷取資料 } else { $this->total = $total;// 全部 $this->lastPage = (int)ceil($total / $listRows);// 尾頁 $this->currentPage = $this->setCurrentPage($currentPage);//當前頁碼 $this->hasMore = $this->currentPage < $this->lastPage;// 更多 } $this->items = PaginatorCollection::make($items, $this);// 處理當前分頁 要進行處理的資料 // 應該是 分頁資料的 樣式 感覺更對 } /** * @param $items * @param $listRows * @param null $currentPage * @param bool $simple * @param null $total * @param array $options * @return PaginatorCollection */ public static function make($items, $listRows, $currentPage = null, $total = null, $simple = false, $options = []) {// make 就是自己 執行個體化 並且是 繼承的子類執行個體化 $paginator = new static($items, $listRows, $currentPage, $total, $simple, $options); return $paginator->items; } protected function setCurrentPage($currentPage) { if (!$this->simple && $currentPage > $this->lastPage) {// 設定當前頁碼 return $this->lastPage > 0 ? $this->lastPage : 1;// 當前頁面為 最後一頁 或者為 1頁 } return $currentPage;// 返回當前頁 } /** * 擷取頁碼對應的連結 * * @param $page * @return string */ protected function url($page)// url 處理 { if ($page <= 0) { $page = 1;// 平滑 分頁資料 } if (strpos($this->options[‘path‘], ‘[PAGE]‘) === false) {// 如果選項中沒有關於 page的配置 $parameters = [$this->options[‘var_page‘] => $page];//參數 當前頁 為1 $path = $this->options[‘path‘];// 路徑為預設路徑 } else { $parameters = [];// 頁數 清空 $path = str_replace(‘[PAGE]‘, $page, $this->options[‘path‘]);//把路徑裡面的 [PAGE] 變換成為真正的代碼 } if (count($this->options[‘query‘]) > 0) {// 如果 查詢語句 大於0 $parameters = array_merge($this->options[‘query‘], $parameters);// 拼接參數 } $url = $path;// 路徑賦值 給 if (!empty($parameters)) {// $url .= ‘?‘ . urldecode(http_build_query($parameters, null, ‘&‘));// 拼接後面的代碼 } return $url . $this->buildFragment();// 拼合 url 跟 相應的 描點 } /** * 自動擷取當前頁碼 * @param string $varPage * @param int $default * @return int */ public static function getCurrentPage($varPage = ‘page‘, $default = 1) { $page = Request::instance()->request($varPage);// 擷取當前頁面 if (filter_var($page, FILTER_VALIDATE_INT) !== false && (int)$page >= 1) { return $page;// 對當前頁面進行處理 } return $default; } /** * 自動擷取當前的path * @return string */ public static function getCurrentPath()// 擷取當前路徑 { return Request::instance()->baseUrl();// 預設路徑 } public function total()// 總頁數 { if ($this->simple) { throw new \DomainException(‘not support total‘); } return $this->total; } public function listRows()// 下一頁 { return $this->listRows; } public function currentPage()// 當前頁 { return $this->currentPage; } public function lastPage()// 最後頁 { if ($this->simple) { throw new \DomainException(‘not support last‘); } return $this->lastPage; } /** * 資料是否足夠分頁 * @return boolean */ public function hasPages()// 分頁 { return !($this->currentPage == 1 && !$this->hasMore); } /** * 建立一組分頁連結 * * @param int $start * @param int $end * @return array */ public function getUrlRange($start, $end)// 建立一組 { $urls = []; for ($page = $start; $page <= $end; $page++) { $urls[$page] = $this->url($page); } return $urls; } /** * 設定URL錨點 * * @param string|null $fragment * @return $this */ public function fragment($fragment)// 設定錨點 { $this->options[‘fragment‘] = $fragment; return $this; } /** * 添加URL參數 * * @param array|string $key * @param string|null $value * @return $this */ public function appends($key, $value = null)// 添加 url 參數 { if (!is_array($key)) { $queries = [$key => $value]; } else { $queries = $key; } foreach ($queries as $k => $v) { if ($k !== $this->options[‘var_page‘]) { $this->options[‘query‘][$k] = $v; } } return $this; } /** * 構造錨點字串 * * @return string */ protected function buildFragment()// 構造錨點字元 { return $this->options[‘fragment‘] ? ‘#‘ . $this->options[‘fragment‘] : ‘‘; } /** * 渲染分頁html * @return mixed */ abstract public function render();}
本文出自 “專註php 群號:414194301” 部落格,請務必保留此出處http://jingshanls.blog.51cto.com/3357095/1883221
[李景山php]每天TP5-20170118|thinkphp5-Paginator.php