Merge pull request #2039 from nemo9l/improve/page-handler-compatible-with-iterator

PageHandler의 Iterator 구현
This commit is contained in:
Kijin Sung 2022-12-15 22:56:20 +09:00 committed by GitHub
commit 11df9cf9ad
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -10,16 +10,28 @@
* @remarks Getting total counts, number of pages, current page number, number of items per page, * @remarks Getting total counts, number of pages, current page number, number of items per page,
* this class implements methods and contains variables for page navigation * this class implements methods and contains variables for page navigation
*/ */
class PageHandler extends Handler class PageHandler extends Handler implements Iterator
{ {
/** @var int Number of total items */
var $total_count = 0; ///< number of total items public $total_count = 0;
var $total_page = 0; ///< number of total pages
var $cur_page = 0; ///< current page number /** @var int Number of total pages */
var $page_count = 10; ///< number of page links displayed at one time public $total_page = 0;
var $first_page = 1; ///< first page number
var $last_page = 1; ///< last page number /** @var int Current page number */
var $point = 0; ///< increments per getNextPage() public $cur_page = 0;
/** @var int Number of page links displayed at one time. */
public $page_count = 10;
/** @var int First page number */
public $first_page = 1;
/** @var int Last page number, same as $total_page */
public $last_page = 1;
/** @var int Iterator stepper */
var $point = 0;
/** /**
* constructor * constructor
@ -30,7 +42,7 @@ class PageHandler extends Handler
* @return void * @return void
*/ */
function __construct($total_count, $total_page, $cur_page, $page_count = 10) public function __construct(int $total_count, int $total_page, int $cur_page, int $page_count = 10)
{ {
$this->total_count = $total_count; $this->total_count = $total_count;
$this->total_page = $total_page; $this->total_page = $total_page;
@ -49,14 +61,8 @@ class PageHandler extends Handler
$first_page -= $first_page + $page_count - 1 - $total_page; $first_page -= $first_page + $page_count - 1 - $total_page;
} }
$last_page = $total_page;
if($last_page > $total_page)
{
$last_page = $total_page;
}
$this->first_page = $first_page; $this->first_page = $first_page;
$this->last_page = $last_page; $this->last_page = $total_page;
if($total_page < $this->page_count) if($total_page < $this->page_count)
{ {
@ -68,7 +74,7 @@ class PageHandler extends Handler
* request next page * request next page
* @return int next page number * @return int next page number
*/ */
function getNextPage() public function getNextPage(): int
{ {
$page = $this->first_page + $this->point++; $page = $this->first_page + $this->point++;
if($this->point > $this->page_count || $page > $this->last_page) if($this->point > $this->page_count || $page > $this->last_page)
@ -83,11 +89,56 @@ class PageHandler extends Handler
* @param int $offset * @param int $offset
* @return int * @return int
*/ */
function getPage($offset) public function getPage(int $offset): int
{ {
return max(min($this->cur_page + $offset, $this->total_page), ''); return max(min($this->cur_page + $offset, $this->total_page), '');
} }
/**
* Rewind iterator stepper.
* @return void
*/
public function rewind ()
{
$this->point = 0;
}
/**
* Determine if a current iterated item is valid.
* @return bool
*/
public function valid (): bool
{
$page = $this->first_page + $this->point;
return $this->point <= $this->page_count && $page <= $this->last_page;
}
/**
* Get a current iterated page number.
* @return int
*/
public function current (): int
{
return $this->first_page + $this->point;
}
/**
* Get a current iterator stepper.
* @return int
*/
public function key (): int
{
return $this->point;
}
/**
* Step up the iterator.
* @return void
*/
public function next ()
{
$this->point++;
}
} }
/* End of file PageHandler.class.php */ /* End of file PageHandler.class.php */
/* Location: ./classes/page/PageHandler.class.php */ /* Location: ./classes/page/PageHandler.class.php */