[李景山php]每天TP5-20170118|thinkphp5-Paginator.php

<?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();
}
时间: 2024-07-28 21:26:16

[李景山php]每天TP5-20170118|thinkphp5-Paginator.php的相关文章

[李景山php]每天TP5-20170108|thinkphp5-Model.php-1

<?php // +---------------------------------------------------------------------- // | ThinkPHP [ WE CAN DO IT JUST THINK ] // +---------------------------------------------------------------------- // | Copyright (c) 2006~2016 http://thinkphp.cn All 

[李景山php]每天TP5-20161221|thinkphp5-jump.php

<?php /**  * 用法:  * load_trait('controller/Jump');  * class index  * {  *     use \traits\controller\Jump;  *     public function index(){  *         $this->error();  *         $this->redirect();  *     }  * }  */ namespace traits\controller; use

[李景山php]每天laravel-20161129|BelongsToMany.php-1

namespace Illuminate\Database\Eloquent\Relations; use Illuminate\Support\Arr; use Illuminate\Support\Str; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Collection; use Illuminate\Da

安装TP5(thinkphp5)Composer

1.下载 composer.phar (http:\\www.cnblogs.com/JANCHAN/7735882.html),把composer.phar放入php中(例如php-5.4.45) 再建一个composer.bat放在php中(放在同级的文件夹中)---在里面写    @php "%~dp0composer.phar" %* 2.去网站根目录中的  配置  php.ini 改openssl.dll和fileinfo.dll去掉分号(按ctrl+F搜索) 3.写环境变量

[李景山php]每天laravel-20161103|CompilerEngine.php-2

    /**      * Handle a view exception.      *      * @param  \Exception  $e      * @param  int  $obLevel      * @return void      *      * @throws $e      */     protected function handleViewException(Exception $e, $obLevel)     {         $e = new E

[李景山php]每天laravel-20161104|Engine.php

<?php namespace Illuminate\View\Engines; abstract class Engine {     /**      * The view that was last to be rendered.      *      * @var string      */     protected $lastRendered;//The view that was last to be rendered.     /**      * Get the last 

[李景山php]每天laravel-20161105|EngineInterface.php

<?php namespace Illuminate\View\Engines; interface EngineInterface {     /**      * Get the evaluated contents of the view.      *      * @param  string  $path      * @param  array   $data      * @return string      */     public function get($path, 

[李景山php]每天laravel-20161106|EngineResolver.php

<?php namespace Illuminate\View\Engines; use Closure; use InvalidArgumentException; class EngineResolver {// engineResolver     /**      * The array of engine resolvers.      *      * @var array      */     protected $resolvers = [];// The array of e

[李景山php]每天laravel-20161107|PhpEngine.php

<?php namespace Illuminate\View\Engines; use Exception; use Throwable; use Symfony\Component\Debug\Exception\FatalThrowableError; class PhpEngine implements EngineInterface {// PhpEngine implements EngineInterface     /**      * Get the evaluated con