<?php
namespace App\Tools;
/**
* 自定義分頁類,主要用於產生分頁試圖
* Class Common
* @package App\Library
*/
class CustomPage
{
/**
* @param $text
* @return string
*/
public static function getActivePageWrapper($text)
{
return '<li><span>' . $text . '</span></li>';
}
/**
* 獲取當前頁按鈕的頁面樣式
* @param $url
* @param $page
* @return string
*/
public static function getActivePageLinkWrapper($url, $page)
{
return '<li class="active"><a href="' . $url . '">' . $page . '</a></li>';
}
/**
* 獲取非當前頁按鈕的頁面樣式
* @param $url
* @param $page
* @return string
*/
public static function getPageLinkWrapper($url, $page)
{
return '<li><a href="' . $url . '">' . $page . '</a></li>';
}
/**
* 獲取整個的分頁樣式
* @param $nowPage 當前頁
* @param $totalPage 共多少頁面
* @param $baseUrl 當前url
* @param $search 搜尋
* @return string
*/
public static function getSelfPageView($nowPage, $totalPage, $baseUrl, $search)
{
$pagePre = '<ul class="pagination">';
$pageEnd = '</ul>';
$pageLastStr = '';
$pageNextStr = '';
if ($nowPage <= 1) {
$nowPage = 1;
$pageLastStr = '<li class="disabled"><span>«</span></li>';
}
if ($nowPage >= $totalPage) {
$nowPage = $totalPage;
$pageNextStr = '<li class="disabled"><span>»</span></li>';
}
$search['totalPage'] = $totalPage;
if (empty($pageLastStr)) {
$lastPage = $nowPage - 1;
$search['nowPage'] = $lastPage;
$lastSearchStr = self::arrayToSearchStr($search);
$url = $baseUrl . '?' . $lastSearchStr;
$pageLastStr = self::getPageLinkWrapper($url, '«');
}
if (empty($pageNextStr)) {
$pageNext = $nowPage + 1;
$search['nowPage'] = $pageNext;
$lastSearchStr = self::arrayToSearchStr($search);
$url = $baseUrl . '?' . $lastSearchStr;
$pageNextStr = self::getPageLinkWrapper($url, '»');
}
$pageTemp = '';
$pageRange = self::getPageRange($nowPage, $totalPage);
$pageTemp .= $pageLastStr;
foreach ($pageRange as $page) {
$search['nowPage'] = $page;
$searchStr = self::arrayToSearchStr($search);
$url = $baseUrl . '?' . $searchStr;
if ($page == $nowPage) {
$pageTemp .= self::getActivePageLinkWrapper($url, $page);
} else {
$pageTemp .= self::getPageLinkWrapper($url, $page);
}
}
$pageTemp .= $pageNextStr;
$pageView = $pagePre . $pageTemp . $pageEnd;
return $pageView;
}
/**
* 獲取實際顯示頁面範圍的範圍
* @param $nowPage
* @param $totalPage
* @return array
*/
public static function getPageRange($nowPage, $totalPage)
{
$returnArray = [];
if ($totalPage <= 5) {
for ($i = 1; $i <= $totalPage; $i++) {
$returnArray[] = $i;
}
} else {
$lengthLeft = $nowPage - 1;
$lengthRight = $totalPage - $nowPage;
if (($lengthLeft < 2) && ($lengthRight < 2)) {
$returnArray = [];
} elseif (($lengthLeft < 2) && ($lengthRight > 2)) {
for ($i = 1; $i <= 5; $i++) {
$returnArray[] = $i;
}
} elseif (($lengthLeft > 2) && ($lengthRight < 2)) {
$start = $totalPage - 4;
for ($i = $start; $i <= $totalPage; $i++) {
$returnArray[] = $i;
}
} else {
for ($i = $nowPage - 2; $i <= $nowPage + 2; $i++) {
$returnArray[] = $i;
}
}
}
return $returnArray;
}
/**
* 將搜尋的陣列拼接成為url
* 注意:PHP的內建函式http_build_query,會自動將沒有值的引數清除,導致blade模板報錯
* @param $array
* @return string
*/
public static function arrayToSearchStr($array)
{
$fields_string = '';
reset($array);
end($array);
$lastKey = key($array);
reset($array);
foreach ($array as $key => $value) {
if ($key != $lastKey) {
$fields_string .= $key . '=' . $value . '&';
} else {
$fields_string .= $key . '=' . $value;
}
}
rtrim($fields_string, '&');
return $fields_string;
}
}
本作品採用《CC 協議》,轉載必須註明作者和本文連結