Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added firstItem, lastItem and a unit test for pagination #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 80 additions & 7 deletions Pagination.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,12 +219,7 @@ public function getRelPrevNextLinkTags()

// Pages
$currentPage = (int) $this->_variables['current'];
$numberOfPages = (
(int) ceil(
$this->_variables['total'] /
$this->_variables['rpp']
)
);
$numberOfPages = $this->getNumberOfPages();

// On first page
if ($currentPage === 1) {
Expand Down Expand Up @@ -261,7 +256,7 @@ public function getRelPrevNextLinkTags()
* logic found in the render.inc.php file.
*
* @access public
* @return void
* @return string
*/
public function parse()
{
Expand Down Expand Up @@ -337,6 +332,14 @@ public function setCurrent($current)
$this->_variables['current'] = $current;
}

/**
* Get the current page number
* @return integer
*/
public function getCurrent() {
return $this->_variables['current'];
}

/**
* setFull
*
Expand Down Expand Up @@ -408,6 +411,17 @@ public function setRPP($rpp)
$this->_variables['rpp'] = $rpp;
}

/**
* Gets the number of records per page
* @access public
* @return integer
*/
public function getRPP()
{
return $this->_variables['rpp'];
}


/**
* setTarget
*
Expand Down Expand Up @@ -435,4 +449,63 @@ public function setTotal($total)
{
$this->_variables['total'] = $total;
}

/**
* Get the total number of records available for pagination
* .
* @return int
*/
public function getTotal()
{
return $this->_variables['total'];
}

/**
* Get the number of the first item on the current page.
*
* @return int
*/
public function firstItem()
{
return ($this->getCurrent() - 1) * $this->getRPP() + 1;
}



/**
* Get the number of items on the current page
*/
public function countCurrentItems() {
// on the last page there could be less records than RPP
if ($this->getCurrent()==$this->getNumberOfPages()) {
return ( $this->getTotal() % $this->getRPP() );
}
else return $this->getRPP();
}


/**
* Get the number of the last item on the current page.
*
* @return int
*/
public function lastItem()
{
return $this->firstItem() + $this->countCurrentItems() - 1;
}

/**
* Get the whole number of pages in this pagination
* @return int
*/
public function getNumberOfPages()
{
return (
(int) ceil(
$this->_variables['total'] /
$this->_variables['rpp']
)
);
}

}
25 changes: 25 additions & 0 deletions test/PaginationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

require_once '../Pagination.class.php';

class PaginationTest extends \PHPUnit_Framework_TestCase
{

public function testPagination() {
$pagination = (new Pagination());
$pagination->setCurrent(10);
$pagination->setTotal(95);
$pagination->setRPP(10);
$this->assertEquals(10, $pagination->getNumberOfPages());
$this->assertEquals(5, $pagination->countCurrentItems());
$this->assertEquals(91, $pagination->firstItem());
$this->assertEquals(95, $pagination->lastItem());

//change to another page
$pagination->setCurrent(5);
$this->assertEquals(10, $pagination->countCurrentItems());
$this->assertEquals(41, $pagination->firstItem());
$this->assertEquals(50, $pagination->lastItem());
}

}