Skip to content

Commit

Permalink
extract pager constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
juliangut committed Jan 23, 2017
1 parent a5b3373 commit 86039ef
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 34 deletions.
2 changes: 2 additions & 0 deletions src/MongoDBRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ public function findPagedBy($criteria, array $orderBy = null, $limit = 10, $offs
*
* @param array|Builder $criteria
*
* @throws \InvalidArgumentException
*
* @return int
*/
public function countBy($criteria)
Expand Down
71 changes: 48 additions & 23 deletions src/Pager/DefaultPager.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,44 +37,37 @@ class DefaultPager extends ArrayCollection implements Pager
*
* @var int
*/
protected $pageSize;
protected $pageCount;

/**
* Total number of elements.
*
* @var int
*/
protected $totalSize;
protected $totalCount;

/**
* {@inheritdoc}
*
* @throws \OutOfBoundsException
*/
public function __construct(array $elements, $page = 1, $pageSize = 10, $totalSize = 0)
public function __construct(array $elements, $currentPage = 1, $pageCount = 10, $totalCount = 0)
{
if ((int) $pageSize < 1) {
throw new \OutOfBoundsException(sprintf('Page size must be at least 1. %d given', $pageSize));
}
$this->pageSize = (int) $pageSize;

if ((int) $page < 1) {
throw new \OutOfBoundsException(sprintf('Page can not be lower than 1. %s given', $page));
}
$this->currentPage = (int) $page;
$this->setCurrentPage($currentPage);
$this->setPageCount($pageCount);

$elements = array_slice($elements, 0, $this->pageSize);
$elements = array_slice($elements, 0, $this->pageCount);

if ((int) $totalSize < 1) {
$totalSize = count($elements);
if ((int) $totalCount < 1) {
$totalCount = count($elements);
}
$this->totalSize = (int) $totalSize;
$this->totalCount = (int) $totalCount;

$this->totalPages = max(1, (int) ceil($this->totalSize / $this->pageSize));
$this->totalPages = max(1, (int) ceil($this->totalCount / $this->pageCount));

if ($this->currentPage > $this->totalPages) {
throw new \OutOfBoundsException(sprintf(
'Page can not be higher than %d. %d given',
'Current page can not be higher than %d. %d given',
$this->totalPages,
$this->currentPage
));
Expand All @@ -83,6 +76,38 @@ public function __construct(array $elements, $page = 1, $pageSize = 10, $totalSi
parent::__construct($elements);
}

/**
* Set current page.
*
* @param int $currentPage
*
* @throws \OutOfBoundsException
*/
protected function setCurrentPage($currentPage)
{
if ((int) $currentPage < 1) {
throw new \OutOfBoundsException(sprintf('Current page can not be lower than 1. %s given', $currentPage));
}

$this->currentPage = (int) $currentPage;
}

/**
* Set pager page size.
*
* @param int $pageCount
*
* @throws \OutOfBoundsException
*/
protected function setPageCount($pageCount)
{
if ((int) $pageCount < 1) {
throw new \OutOfBoundsException(sprintf('Page count must be at least 1. %d given', $pageCount));
}

$this->pageCount = (int) $pageCount;
}

/**
* {@inheritdoc}
*/
Expand All @@ -96,15 +121,15 @@ public function getCurrentPage()
*/
public function getCurrentPageOffsetStart()
{
return ($this->currentPage - 1) * $this->pageSize;
return ($this->currentPage - 1) * $this->pageCount;
}

/**
* {@inheritdoc}
*/
public function getCurrentPageOffsetEnd()
{
return min($this->totalSize, $this->getCurrentPageOffsetStart() + $this->pageSize);
return min($this->totalCount, $this->getCurrentPageOffsetStart() + $this->pageCount);
}

/**
Expand Down Expand Up @@ -142,9 +167,9 @@ public function isLastPage()
/**
* {@inheritdoc}
*/
public function getPageSize()
public function getPageCount()
{
return $this->pageSize;
return $this->pageCount;
}

/**
Expand All @@ -160,6 +185,6 @@ public function getTotalPages()
*/
public function getTotalCount()
{
return $this->totalSize;
return $this->totalCount;
}
}
10 changes: 5 additions & 5 deletions src/Pager/Pager.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ interface Pager extends \Countable, \IteratorAggregate
* Page constructor.
*
* @param array $elements
* @param int $page
* @param int $pageSize
* @param int $totalSize
* @param int $currentPage
* @param int $pageCount
* @param int $totalCount
*/
public function __construct(array $elements, $page = 1, $pageSize = 10, $totalSize = 0);
public function __construct(array $elements, $currentPage = 1, $pageCount = 10, $totalCount = 0);

/**
* Get current page.
Expand Down Expand Up @@ -80,7 +80,7 @@ public function isLastPage();
*
* @return int
*/
public function getPageSize();
public function getPageCount();

/**
* Get total number of pages.
Expand Down
2 changes: 2 additions & 0 deletions src/RelationalRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ public function findPagedBy($criteria, array $orderBy = null, $limit = 10, $offs
*
* @param array|QueryBuilder $criteria
*
* @throws \InvalidArgumentException
*
* @return int
*/
public function countBy($criteria)
Expand Down
12 changes: 6 additions & 6 deletions tests/Repository/Pager/DefaultPageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class DefaultPageTest extends \PHPUnit_Framework_TestCase
{
/**
* @expectedException \OutOfBoundsException
* @expectedExceptionMessageRegExp /^Page can not be lower than 1. 0 given/
* @expectedExceptionMessageRegExp /^Current page can not be lower than 1. 0 given/
*/
public function testBadPageLowerLimit()
{
Expand All @@ -31,7 +31,7 @@ public function testBadPageLowerLimit()

/**
* @expectedException \OutOfBoundsException
* @expectedExceptionMessageRegExp /^Page can not be higher than 1. 2 given/
* @expectedExceptionMessageRegExp /^Current page can not be higher than 1. 2 given/
*/
public function testBadPageUpperLimit()
{
Expand All @@ -40,18 +40,18 @@ public function testBadPageUpperLimit()

/**
* @expectedException \OutOfBoundsException
* @expectedExceptionMessageRegExp /^Page size must be at least 1. 0 given/
* @expectedExceptionMessageRegExp /^Page count must be at least 1. 0 given/
*/
public function testBadPageSize()
public function testBadPageCount()
{
new DefaultPager([], 0, 0);
new DefaultPager([], 1, 0);
}

public function testPage()
{
$page = new DefaultPager(['a', 'b', 'c', 'd'], 1, 4, 12);

static::assertEquals(4, $page->getPageSize());
static::assertEquals(4, $page->getPageCount());
static::assertEquals(12, $page->getTotalCount());

static::assertEquals(1, $page->getCurrentPage());
Expand Down

0 comments on commit 86039ef

Please sign in to comment.