This repository has been archived by the owner on Jan 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #114 from spiral/develop
Develop
- Loading branch information
Showing
5 changed files
with
201 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
<?php | ||
/** | ||
* Spiral Framework. | ||
* | ||
* @license MIT | ||
* @author Anton Titov (Wolfy-J) | ||
*/ | ||
namespace Spiral\Pagination; | ||
|
||
class CountingPaginator implements PredictableInterface, \Countable | ||
{ | ||
/** | ||
* @var int | ||
*/ | ||
private $pageNumber = 1; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
private $countPages = 1; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
private $limit = 25; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
private $count = 0; | ||
|
||
/** | ||
* @param int $limit | ||
*/ | ||
public function __construct($limit = 25) | ||
{ | ||
$this->setLimit($limit); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function setPage($number) | ||
{ | ||
$this->pageNumber = abs(intval($number)); | ||
|
||
//Real page number | ||
return $this->getPage(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getPage() | ||
{ | ||
if ($this->pageNumber < 1) { | ||
return 1; | ||
} | ||
|
||
if ($this->pageNumber > $this->countPages) { | ||
return $this->countPages; | ||
} | ||
|
||
return $this->pageNumber; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function setLimit($limit) | ||
{ | ||
$this->limit = $limit; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getLimit() | ||
{ | ||
return $this->limit; | ||
} | ||
|
||
/** | ||
* Get pagination offset. | ||
* | ||
* @return int | ||
*/ | ||
public function getOffset() | ||
{ | ||
return ($this->getPage() - 1) * $this->limit; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function paginate(PaginableInterface $paginable) | ||
{ | ||
$this->setCount($paginable->count()); | ||
|
||
$paginable->offset($this->getOffset()); | ||
$paginable->limit($this->getLimit()); | ||
|
||
return $paginable; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function setCount($count) | ||
{ | ||
$this->count = abs(intval($count)); | ||
if ($this->count > 0) { | ||
$this->countPages = ceil($this->count / $this->limit); | ||
} else { | ||
$this->countPages = 1; | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @return int | ||
*/ | ||
public function count() | ||
{ | ||
return $this->count; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function countPages() | ||
{ | ||
return $this->countPages; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function countDisplayed() | ||
{ | ||
if ($this->getPage() == $this->countPages) { | ||
return $this->count - $this->getOffset(); | ||
} | ||
|
||
return $this->limit; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function isRequired() | ||
{ | ||
return ($this->countPages > 1); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function nextPage() | ||
{ | ||
if ($this->getPage() != $this->countPages) { | ||
return $this->getPage() + 1; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function previousPage() | ||
{ | ||
if ($this->getPage() > 1) { | ||
return $this->getPage() - 1; | ||
} | ||
|
||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.