Paginator分页类   
namespace Tang\Pagination;
use Tang\I18n\II18n;
 
/**
 * 分页实现
 * Class Paginator
 * @package Tang\Pagination
 */
class Paginator implements IPaginator
{
    private $total = 0;
    private $pageNumber = 20;
    private $maxPage = 0;
    private $nowPage = 0;
    /**
     * 语言包
     * @var II18n
     */
    private $II18n;
    public function setl18n(II18n $II18n)
    {
        $this->II18n = $II18n;
    }
    /**
     * 设置总量
     * @param $total
     * @return mixed
     */
    public function setTotal($total)
    {
        $total = (int) $total;
        $total < 0 && $total = 0;
        $this->total = $total;
    }
 
    /**
     * 设置每页数量
     * @param $pageNumber
     * @return mixed
     */
    public function setPageNumber($pageNumber)
    {
        $pageNumber = (int)$pageNumber;
        $pageNumber < 1 && $pageNumber = 20;
        $this->pageNumber = $pageNumber;
    }
 
    /**
     * 获取每页数量
     * @return int
     */
    public function getPageNumber()
    {
        return $this->pageNumber;
    }
 
    /**
     * 获取最大页数
     * @return int
     */
    public function getMaxPage()
    {
        return $this->maxPage;
    }
 
    /**
     * 获取当前页
     * @return int
     */
    public function getNowPage()
    {
        return $this->nowPage;
    }
 
    /**
     * 获取分页数组
     * @param int $nowPage 当前页
     * @param int $total 总数量
     * @param int $pageNumber 一页数量
     * @throws NowPageLtMaxPageException
     * @return array
     */
    public function getPages($nowPage,$total = 0,$pageNumber = 0)
    {
        if ($total)
        {
            $this->setTotal($total);
        }
        if ($pageNumber)
        {
            $this->setPageNumber($pageNumber);
        }
        $nowPage = (int) $nowPage;
        $nowPage < 1 && $nowPage = 1;
        $this->nowPage = $nowPage;
        $maxPage = $this->maxPage = ceil($this->total / $this->pageNumber);
        if (!$maxPage)
        {
            return false;
        }
        if ($nowPage > $maxPage)
        {
            throw new NowPageLtMaxPageException('No more pages!',null,50013);
        }
        $pages = array();
        $pages[] = array('name' => $this->II18n->get('First page'),'page'=>1);
        if ($nowPage > 1)
        {
            $pages[] = array('name' => $this->II18n->get('Prev page'),'page'=>$nowPage-1);
        }
        if ($nowPage > 5)
        {
            for ($i = $nowPage - 5; $i < $nowPage; $i ++)
            {
                if ($i <= 0)
                    continue;
                $pages[] = array('name' => $i,'page'=>$i);
            }
            for ($i = 0; $i < 5; $i ++)
            {
                $p = $nowPage + $i;
                if ($p > $maxPage)
                {
                    break;
                }
                $pages[] = array('name' => $p,'page'=>$p);
            }
        } else
        {
            for ($i = 1; $i < 10; $i ++)
            {
                if ($i > $maxPage)
                {
                    break;
                }
                $pages[] = array('name' => $i,'page'=>$i);
            }
        }
        $nextPage = $nowPage + 1;
        if ($nowPage < $maxPage)
        {
            $pages[] = array('name' => $this->II18n->get('Next page'),'page'=>$nextPage);
        }
        $pages[] = array('name' => $this->II18n->get('End page'),'page'=>$maxPage);
        return $pages;
    }
}
 

IPaginator分页接口

namespace Tang\Pagination;
use Tang\I18n\II18n;
use Tang\Interfaces\ISetConfig;
 
/**
 * 分页接口
 * Interface IPaginator
 * @package Tang\Pagination
 */
interface IPaginator
{
    /**
     * 设置语言包
     * @param II18n $II18n
     * @return void
     */
    public function setl18n(II18n $II18n);
    /**
     * 设置总量
     * @param $total
     * @return void
     */
    public function setTotal($total);
 
    /**
     * 设置每页数量
     * @param $pageNumber
     * @return void
     */
    public function setPageNumber($pageNumber);
 
    /**
     * 获取每页数量
     * @return int
     */
    public function getPageNumber();
 
    /**
     * 获取最大页数
     * @return int
     */
    public function getMaxPage();
 
    /**
     * 获取当前页
     * @return int
     */
    public function getNowPage();
 
    /**
     * 获取分页数组
     * 返回的结果数组为array(
     *       array('name'=>'第一页','page' => 1),
     * array('name'=>'1','page' => 1),
     * array('name'=>'2','page' => 2),
     *      array('name'=>'下一页','page' => 2)
     * )形式
     * @param int $nowPage 当前页
     * @param int $total 总数量
     * @param int $pageNumber 一页数量
     * @return array
     */
    public function getPages ($nowPage,$total = 0,$pageNumber = 0);
}

 

稳定

产品可用性高于99.9%

贴心

全国7*24小时客服热线

专业

产品经理在线技术支持

快速

快速建站上线运营快

承诺

我们选择声誉

坚持

10年专注高端品质开发
  • 返回顶部