1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 
<?php
namespace Omeka\Stdlib;

/**
 * Class for performing pagination calculations.
 */
class Paginator
{
    /**
     * The default current page.
     */
    const CURRENT_PAGE = 1;

    /**
     * The default number of records per page.
     */
    const PER_PAGE = 25;

    /**
     * The default total record count.
     */
    const TOTAL_COUNT = 0;

    /**
     * @var ServiceLocatorInterface
     */
    protected $services;

    /**
     * The current page number
     *
     * @var int
     */
    protected $currentPage = self::CURRENT_PAGE;

    /**
     * The number of records per page
     *
     * @var int
     */
    protected $perPage = self::PER_PAGE;

    /**
     * The total record count.
     *
     * @param int
     */
    protected $totalCount = self::TOTAL_COUNT;

    /**
     * Set the current page number.
     *
     * @param int $currentPage
     */
    public function setCurrentPage($currentPage)
    {
        $currentPage = (int) $currentPage;
        if ($currentPage < 1) {
            $currentPage = 1;
        }
        $this->currentPage = $currentPage;
        return $this;
    }

    /**
     * Get the current page number.
     *
     * @param int
     */
    public function getCurrentPage()
    {
        return $this->currentPage;
    }

    /**
     * Set the number of records per page.
     *
     * @param int $perPage
     */
    public function setPerPage($perPage)
    {
        $perPage = (int) $perPage;
        if ($perPage < 1) {
            $perPage = 1;
        }
        $this->perPage = $perPage;
        return $this;
    }

    /**
     * Get the number of records per page.
     *
     * @param int
     */
    public function getPerPage()
    {
        return $this->perPage;
    }

    /**
     * Set the total record count.
     *
     * @param int $totalCount
     */
    public function setTotalCount($totalCount)
    {
        $totalCount = (int) $totalCount;
        if ($totalCount < 0) {
            $totalCount = 0;
        }
        $this->totalCount = $totalCount;
        return $this;
    }

    /**
     * Get the total record count.
     *
     * @param int
     */
    public function getTotalCount()
    {
        return $this->totalCount;
    }

    /**
     * Get the record offset.
     *
     * @return int
     */
    public function getOffset()
    {
        return ($this->perPage * $this->currentPage) - $this->perPage;
    }

    /**
     * Get the number of pages.
     *
     * @return int
     */
    public function getPageCount()
    {
        return (int) ceil($this->totalCount / $this->perPage);
    }

    /**
     * Get the previous page number.
     *
     * Returns null when there is no previous page.
     *
     * @return int|null
     */
    public function getPreviousPage()
    {
        $previousPage = null;
        if ($this->currentPage - 1 > 0) {
            $previousPage = $this->currentPage - 1;
        }
        return $previousPage;
    }

    /**
     * Get the next page number.
     *
     * Returns null when there is no next page.
     *
     * @return int|null
     */
    public function getNextPage()
    {
        $nextPage = null;
        if ($this->currentPage + 1 <= $this->getPageCount()) {
            $nextPage = $this->currentPage + 1;
        }
        return $nextPage;
    }
}