Skip to content

Commit 1b099a7

Browse files
authored
Merge pull request #2899 from jlamim/pagination_new_features
New features for pagination
2 parents a5815ce + 1e3e02e commit 1b099a7

2 files changed

Lines changed: 151 additions & 2 deletions

File tree

system/Pager/PagerRenderer.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,4 +343,78 @@ protected function updatePages(int $count = null)
343343
}
344344

345345
//--------------------------------------------------------------------
346+
347+
/**
348+
* Checks to see if there is a "previous" page before our "first" page.
349+
*
350+
* @return boolean
351+
*/
352+
public function hasPreviousPage(): bool
353+
{
354+
return $this->current > 1;
355+
}
356+
357+
//--------------------------------------------------------------------
358+
359+
/**
360+
* Returns a URL to the "previous" page.
361+
*
362+
* You MUST call hasPreviousPage() first, or this value may be invalid.
363+
*
364+
* @return string|null
365+
*/
366+
public function getPreviousPage()
367+
{
368+
if (!$this->hasPreviousPage()) {
369+
return null;
370+
}
371+
372+
$uri = clone $this->uri;
373+
374+
if ($this->segment === 0) {
375+
$uri->addQuery($this->pageSelector, $this->current - 1);
376+
} else {
377+
$uri->setSegment($this->segment, $this->current - 1);
378+
}
379+
380+
return (string) $uri;
381+
}
382+
383+
//--------------------------------------------------------------------
384+
385+
/**
386+
* Checks to see if there is a "next" page after our "last" page.
387+
*
388+
* @return boolean
389+
*/
390+
public function hasNextPage(): bool
391+
{
392+
return $this->current < $this->last;
393+
}
394+
395+
//--------------------------------------------------------------------
396+
397+
/**
398+
* Returns a URL to the "next" page.
399+
*
400+
* You MUST call hasNextPage() first, or this value may be invalid.
401+
*
402+
* @return string|null
403+
*/
404+
public function getNextPage()
405+
{
406+
if (!$this->hasNextPage()) {
407+
return null;
408+
}
409+
410+
$uri = clone $this->uri;
411+
412+
if ($this->segment === 0) {
413+
$uri->addQuery($this->pageSelector, $this->current + 1);
414+
} else {
415+
$uri->setSegment($this->segment, $this->current + 1);
416+
}
417+
418+
return (string) $uri;
419+
}
346420
}

tests/system/Pager/PagerRendererTest.php

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
<?php namespace CodeIgniter\Pager;
1+
<?php
2+
3+
namespace CodeIgniter\Pager;
24

35
use CodeIgniter\HTTP\URI;
46

@@ -243,7 +245,7 @@ public function testSurroundCount()
243245
'uri' => $uri,
244246
'pageCount' => 10, // 10 pages
245247
'currentPage' => 4,
246-
'total' => 100,// 100 records, so 10 per page
248+
'total' => 100, // 100 records, so 10 per page
247249
];
248250

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

456+
//--------------------------------------------------------------------
457+
458+
public function testHasPreviousPageReturnsFalseWhenCurrentPageIsFirst()
459+
{
460+
$details = [
461+
'uri' => $this->uri,
462+
'pageCount' => 5,
463+
'currentPage' => 1,
464+
'total' => 100
465+
];
466+
467+
$pager = new PagerRenderer($details);
468+
469+
$this->assertNull($pager->getPreviousPage());
470+
$this->assertFalse($pager->hasPreviousPage());
471+
}
472+
473+
//--------------------------------------------------------------------
474+
475+
public function testHasNextPageReturnsFalseWhenCurrentPageIsLast()
476+
{
477+
$details = [
478+
'uri' => $this->uri,
479+
'pageCount' => 5,
480+
'currentPage' => 5,
481+
'total' => 100
482+
];
483+
484+
$pager = new PagerRenderer($details);
485+
486+
$this->assertNull($pager->getNextPage());
487+
$this->assertFalse($pager->hasNextPage());
488+
}
489+
490+
//--------------------------------------------------------------------
491+
492+
public function testHasPreviousPageReturnsTrueWhenFirstIsMoreThanCurrent()
493+
{
494+
$uri = $this->uri;
495+
496+
$details = [
497+
'uri' => $uri,
498+
'pageCount' => 10,
499+
'currentPage' => 3,
500+
'total' => 100
501+
];
502+
503+
$pager = new PagerRenderer($details);
504+
505+
$this->assertNotNull($pager->getPreviousPage());
506+
$this->assertTrue($pager->hasPreviousPage());
507+
$this->assertEquals('http://example.com/foo?page=2', $pager->getPreviousPage());
508+
}
509+
510+
//--------------------------------------------------------------------
511+
512+
public function testHasNextPageReturnsTrueWhenLastIsMoreThanCurrent()
513+
{
514+
$uri = $this->uri;
515+
516+
$details = [
517+
'uri' => $uri,
518+
'pageCount' => 10,
519+
'currentPage' => 3,
520+
'total' => 100
521+
];
522+
523+
$pager = new PagerRenderer($details);
524+
525+
$this->assertNotNull($pager->getNextPage());
526+
$this->assertTrue($pager->hasNextPage());
527+
$this->assertEquals('http://example.com/foo?page=4', $pager->getNextPage());
528+
}
454529
}

0 commit comments

Comments
 (0)