Kontakt - Datenschutz

Subversion

<?php
/**
 * Pagination model class
 *
 * @copyright 2016 Sven Drieling
 * @license   http://opensource.org/licenses/mit-license.php MIT license
 * @version   0.1.0alpha1
 */
namespace YD\Web;


/** 
 * Pagination model
 *
 * Validates given page number. Calculates values of the next, previous,
 * last, ... page and provides methods like <code>isFirstPage()</code> 
 * to generate previous, next page links.
 */
class PaginationModel {
    /** @var integer      Number of all entries */
    public $numberOfEntries = 0;

    /** @var integer      Number of entries per page */
    public $entriesPerPage  = 10;


    /** @var integer|null Currently selected page */
    public $currentPage     = null;

    /** @var integer|null Number of first page */
    public $firstPage       = null;

    /** @var integer|null Number of last page */
    public $lastPage        = null;

    /** @var integer|null Number of previous page */
    public $previousPage    = null;

    /** @var integer|null Number of next page */
    public $nextPage        = null;



    /**
     * Create PaginationModel
     *
     * @param integer $numberOfEntries Number of all entries.
     * @param integer $currentPage     Currently selected page.
     * @param integer $entriesPerPage  Number of entries per page.
     */
    public function __construct($numberOfEntries, $currentPage, $entriesPerPage = 10) {
        $this->numberOfEntries = $numberOfEntries;
        $this->currentPage     = $currentPage;
        $this->entriesPerPage  = $entriesPerPage;

        $this->validate();
        $this->calculate();
    }


    public function validate() {
        if(!filter_var($this->numberOfEntries, FILTER_VALIDATE_INT, ['min_range' => 0])) {
            throw new \InvalidArgumentException("numberOfEntries must be a an integer >= 0 is: '$this->numberOfEntries'");
        }

        if(!filter_var($this->currentPage, FILTER_VALIDATE_INT, ['min_range' => 1])) {
            throw new \InvalidArgumentException("currentPage must be a an integer >= 1 is: '$this->currentPage'");
        }

        if(!filter_var($this->entriesPerPage, FILTER_VALIDATE_INT, ['min_range' => 1])) {
            throw new \InvalidArgumentException("entriesPerPage be a an integer >= 1 is: '$this->entriesPerPage'");
        }
    }


    public function calculate() {
        $this->validate();

        $this->firstPage     = null;
        $this->lastPage      = null;

        $this->previousPage  = null;
        $this->nextPage      = null;

        if($this->numberOfEntries > 0) {
            // first and lastPage
            $this->firstPage = 1;
            $this->lastPage  = (int) ceil($this->numberOfEntries / $this->entriesPerPage);

            // previousPage and nextPage
            $this->previousPage = $this->currentPage - 1;
            if($this->previousPage < 1) {
                $this->previousPage = null;
            }

            $this->nextPage = $this->currentPage + 1;
            if($this->nextPage > $this->lastPage) {
                $this->nextPage = null;
            }
        }
    }


    /**
     * Is current page?
     *
     * Checks if the given page number is the currently selected page.
     *
     * @todo Validate, convert $page parameter to integer
     * 
     * @param  integer $page Page number to check.
     * @return boolean true if $page is the currently selected page else false.
     */
    public function isCurrentPage($page) {
        return !is_null($this->currentPage) and $page === $this->currentPage;
    }


    /* TODO Implement
    public function getLinkOfPage($page) {
        // Test int
        // Test range
        return "page={$page}";
    }
    */


    /**
     * Is current page the first page?
     *
     * Checks if the currently selected page is the first page.
     *
     * @return boolean true if $this->currentPage is the first page else false.
     */
    public function isFirstPage() {
        return !is_null($this->currentPage) and $this->currentPage == $this->firstPage;
    }

    /**
     * Is current page the last page?
     *
     * Checks if the currently selected page is the last page.
     *
     * @return boolean true if $this->currentPage is the last page else false.
     */
    public function isLastPage() {
        return !is_null($this->currentPage) and $this->currentPage == $this->lastPage;
    }

    /**
     * Does the current page exists?
     *
     * Checks if the currently selected page exists.
     *
     * @return boolean true if $this->currentPage is the first page.
     */
    public function pageExists() {
        return !is_null($this->firstPage) and $this->currentPage <= $this->lastPage;
    }

    /**
     * Does the previous page exists?
     *
     * Checks if the previous page exist.
     *
     * @return boolean true if the previous page exists else false.
     */
    public function previousPageExists() {
        return !is_null($this->previousPage);
    }

    /**
     * Does the next page exists?
     *
     * Checks if the next page exist.
     *
     * @return boolean true if the next page exists else false.
     */
    public function nextPageExists() {
        return !is_null($this->nextPage);
    }
}
?>