Paging.php 3.47 KB
<?php
/**
 * 分页基类
 *
 * example:
 * <pre>
 * $paging = new Q_Paging();
 * $paging->setTotal(10)->setCursor(1);
 * </pre>
 *
 * @name Q_Paging
 * @version 2.0 (2009-5-11 下午05:47:31)
 * @package Q.Paging
 * @author peter.zyliu liuziyang@gmail.com
 * @since 1.0
 */
namespace Hood\Helper;
class Paging
{

    /**
     * 总分页数
     *
     * @var Integer
     */
    private $total;

    /**
     * 每页记录数
     *
     * @var Integer
     */
    private $size;

    /**
     * 当前页
     * @var Integer
     */
    protected $currentPage = 1;

    /**
     * 光标
     *
     * @var Integer
     */
    private $cursor = 0;

    /**
     * 当前页
     * @return Integer
     */
    public function getCurrent()
    {
        return $this->currentPage;
    }

    /**
     * 设置当前页
     * @param $pageNo
     * @return $this
     */
    public function setCurrent($pageNo)
    {
        $cur = (int)intval($pageNo);
        if ($cur <= 0) {
            $cur = 1;
        }
        $this->currentPage = $cur;
        return $this;
    }

    /**
     * 下一页
     * @return int
     */
    public function getNext()
    {
        $pageNum = $this->getPageNum();
        $current = $this->getCurrent();
        return $current < $pageNum ? ($current + 1) : $pageNum;
    }

    /**
     * 上一页
     * @return int
     */
    public function getPrev()
    {
        $current = $this->getCurrent();
        return $current > 1 ? ($current - 1) : 1;
    }

    /**
     * 取得记录开始的偏移量
     * @return int
     */
    public function getOffset()
    {
        $offset = $this->getSize() * abs($this->getCurrent() - 1);
        if ($offset >= $this->getTotal()) {
            $offset = 0;
            if ($this->getTotal() > 0 && $this->getTotal() > $this->getSize()) {
                $offset = $this->getSize() * abs($this->getPageNum() - 1);
            }
        }
        return (int)abs($offset);
    }


    /**
     * 设置总记录数
     * @param $total
     * @return $this
     */
    public function setTotal($total)
    {
        $this->total = (int)intval($total);
        return $this;
    }

    /**
     * 获取总数
     * @return int
     */
    public function getTotal()
    {
        return (int)intval($this->total);
    }

    /**
     * 获取每页显示数
     * @return int
     */
    public function getSize()
    {
        return (int)intval($this->size);
    }

    /**
     * 设置每页记录数
     * @param $size
     * @return $this
     */
    public function setSize($size)
    {
        if ($size > 0) {
            $this->size = (int)intval($size);
        }
        return $this;
    }

    /**
     * 获取起始数
     * @return int
     */
    public function getStarting()
    {
        return $this->getOffset();
    }

    /**
     * 获取终点数
     * @return int
     */
    public function getEnding()
    {
        return $this->getOffset() + $this->getSize();
    }

    /**
     * 光标
     * @return int
     */
    public function getCursor()
    {
        return $this->cursor;
    }

    /**
     * 设置光标
     * @param $cursor
     * @return $this
     */
    public function setCursor($cursor)
    {
        $this->cursor = intval($cursor);
        return $this;
    }

    /**
     * 取得总分页数
     * @return float|int
     */
    public function getPageNum()
    {
        if ($this->getSize() == 0) {
            return 0;
        }
        return ceil($this->getTotal() / $this->getSize());
    }
}