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

New features for pagination #2899

Merged
merged 3 commits into from
Apr 29, 2020
Merged
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
74 changes: 74 additions & 0 deletions system/Pager/PagerRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -343,4 +343,78 @@ protected function updatePages(int $count = null)
}

//--------------------------------------------------------------------

/**
* Checks to see if there is a "previous" page before our "first" page.
*
* @return boolean
*/
public function hasPreviousPage(): bool
{
return $this->current > 1;
}

//--------------------------------------------------------------------

/**
* Returns a URL to the "previous" page.
*
* You MUST call hasPreviousPage() first, or this value may be invalid.
*
* @return string|null
*/
public function getPreviousPage()
{
if (!$this->hasPreviousPage()) {
return null;
jlamim marked this conversation as resolved.
Show resolved Hide resolved
}

$uri = clone $this->uri;

if ($this->segment === 0) {
$uri->addQuery($this->pageSelector, $this->current - 1);
} else {
$uri->setSegment($this->segment, $this->current - 1);
}

return (string) $uri;
}

//--------------------------------------------------------------------

/**
* Checks to see if there is a "next" page after our "last" page.
*
* @return boolean
*/
public function hasNextPage(): bool
{
return $this->current < $this->last;
}

//--------------------------------------------------------------------

/**
* Returns a URL to the "next" page.
*
* You MUST call hasNextPage() first, or this value may be invalid.
*
* @return string|null
*/
public function getNextPage()
{
if (!$this->hasNextPage()) {
return null;
jlamim marked this conversation as resolved.
Show resolved Hide resolved
}

$uri = clone $this->uri;

if ($this->segment === 0) {
$uri->addQuery($this->pageSelector, $this->current + 1);
} else {
$uri->setSegment($this->segment, $this->current + 1);
}

return (string) $uri;
}
}
79 changes: 77 additions & 2 deletions tests/system/Pager/PagerRendererTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php namespace CodeIgniter\Pager;
<?php

namespace CodeIgniter\Pager;

use CodeIgniter\HTTP\URI;

Expand Down Expand Up @@ -243,7 +245,7 @@ public function testSurroundCount()
'uri' => $uri,
'pageCount' => 10, // 10 pages
'currentPage' => 4,
'total' => 100,// 100 records, so 10 per page
'total' => 100, // 100 records, so 10 per page
];

$pager = new PagerRenderer($details);
Expand Down Expand Up @@ -451,4 +453,77 @@ public function testGetFirstAndGetLastSegment()
$this->assertEquals('http://example.com/foo/50?foo=bar', $pager->getLast());
}

//--------------------------------------------------------------------

public function testHasPreviousPageReturnsFalseWhenCurrentPageIsFirst()
{
$details = [
'uri' => $this->uri,
'pageCount' => 5,
'currentPage' => 1,
'total' => 100
];

$pager = new PagerRenderer($details);

$this->assertNull($pager->getPreviousPage());
$this->assertFalse($pager->hasPreviousPage());
}

//--------------------------------------------------------------------

public function testHasNextPageReturnsFalseWhenCurrentPageIsLast()
{
$details = [
'uri' => $this->uri,
'pageCount' => 5,
'currentPage' => 5,
'total' => 100
];

$pager = new PagerRenderer($details);

$this->assertNull($pager->getNextPage());
$this->assertFalse($pager->hasNextPage());
}

//--------------------------------------------------------------------

public function testHasPreviousPageReturnsTrueWhenFirstIsMoreThanCurrent()
{
$uri = $this->uri;

$details = [
'uri' => $uri,
'pageCount' => 10,
'currentPage' => 3,
'total' => 100
];

$pager = new PagerRenderer($details);

$this->assertNotNull($pager->getPreviousPage());
$this->assertTrue($pager->hasPreviousPage());
$this->assertEquals('http://example.com/foo?page=2', $pager->getPreviousPage());
}

//--------------------------------------------------------------------

public function testHasNextPageReturnsTrueWhenLastIsMoreThanCurrent()
{
$uri = $this->uri;

$details = [
'uri' => $uri,
'pageCount' => 10,
'currentPage' => 3,
'total' => 100
];

$pager = new PagerRenderer($details);

$this->assertNotNull($pager->getNextPage());
$this->assertTrue($pager->hasNextPage());
$this->assertEquals('http://example.com/foo?page=4', $pager->getNextPage());
}
}