| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* This file is part of the Nette Framework (https://nette.org) |
| 5 |
* Copyright (c) 2004 David Grudl (https://davidgrudl.com) |
| 6 |
*/ |
| 7 |
declare (strict_types=1); |
| 8 |
namespace Packetery\Nette\Utils; |
| 9 |
|
| 10 |
use Packetery\Nette; |
| 11 |
/** |
| 12 |
* Paginating math. |
| 13 |
* |
| 14 |
* @property int $page |
| 15 |
* @property-read int $firstPage |
| 16 |
* @property-read int|null $lastPage |
| 17 |
* @property-read int $firstItemOnPage |
| 18 |
* @property-read int $lastItemOnPage |
| 19 |
* @property int $base |
| 20 |
* @property-read bool $first |
| 21 |
* @property-read bool $last |
| 22 |
* @property-read int|null $pageCount |
| 23 |
* @property int $itemsPerPage |
| 24 |
* @property int|null $itemCount |
| 25 |
* @property-read int $offset |
| 26 |
* @property-read int|null $countdownOffset |
| 27 |
* @property-read int $length |
| 28 |
*/ |
| 29 |
class Paginator |
| 30 |
{ |
| 31 |
use \Packetery\Nette\SmartObject; |
| 32 |
/** @var int */ |
| 33 |
private $base = 1; |
| 34 |
/** @var int */ |
| 35 |
private $itemsPerPage = 1; |
| 36 |
/** @var int */ |
| 37 |
private $page = 1; |
| 38 |
/** @var int|null */ |
| 39 |
private $itemCount; |
| 40 |
/** |
| 41 |
* Sets current page number. |
| 42 |
* @return static |
| 43 |
*/ |
| 44 |
public function setPage(int $page) |
| 45 |
{ |
| 46 |
$this->page = $page; |
| 47 |
return $this; |
| 48 |
} |
| 49 |
/** |
| 50 |
* Returns current page number. |
| 51 |
*/ |
| 52 |
public function getPage() : int |
| 53 |
{ |
| 54 |
return $this->base + $this->getPageIndex(); |
| 55 |
} |
| 56 |
/** |
| 57 |
* Returns first page number. |
| 58 |
*/ |
| 59 |
public function getFirstPage() : int |
| 60 |
{ |
| 61 |
return $this->base; |
| 62 |
} |
| 63 |
/** |
| 64 |
* Returns last page number. |
| 65 |
*/ |
| 66 |
public function getLastPage() : ?int |
| 67 |
{ |
| 68 |
return $this->itemCount === null ? null : $this->base + \max(0, $this->getPageCount() - 1); |
| 69 |
} |
| 70 |
/** |
| 71 |
* Returns the sequence number of the first element on the page |
| 72 |
*/ |
| 73 |
public function getFirstItemOnPage() : int |
| 74 |
{ |
| 75 |
return $this->itemCount !== 0 ? $this->offset + 1 : 0; |
| 76 |
} |
| 77 |
/** |
| 78 |
* Returns the sequence number of the last element on the page |
| 79 |
*/ |
| 80 |
public function getLastItemOnPage() : int |
| 81 |
{ |
| 82 |
return $this->offset + $this->length; |
| 83 |
} |
| 84 |
/** |
| 85 |
* Sets first page (base) number. |
| 86 |
* @return static |
| 87 |
*/ |
| 88 |
public function setBase(int $base) |
| 89 |
{ |
| 90 |
$this->base = $base; |
| 91 |
return $this; |
| 92 |
} |
| 93 |
/** |
| 94 |
* Returns first page (base) number. |
| 95 |
*/ |
| 96 |
public function getBase() : int |
| 97 |
{ |
| 98 |
return $this->base; |
| 99 |
} |
| 100 |
/** |
| 101 |
* Returns zero-based page number. |
| 102 |
*/ |
| 103 |
protected function getPageIndex() : int |
| 104 |
{ |
| 105 |
$index = \max(0, $this->page - $this->base); |
| 106 |
return $this->itemCount === null ? $index : \min($index, \max(0, $this->getPageCount() - 1)); |
| 107 |
} |
| 108 |
/** |
| 109 |
* Is the current page the first one? |
| 110 |
*/ |
| 111 |
public function isFirst() : bool |
| 112 |
{ |
| 113 |
return $this->getPageIndex() === 0; |
| 114 |
} |
| 115 |
/** |
| 116 |
* Is the current page the last one? |
| 117 |
*/ |
| 118 |
public function isLast() : bool |
| 119 |
{ |
| 120 |
return $this->itemCount === null ? \false : $this->getPageIndex() >= $this->getPageCount() - 1; |
| 121 |
} |
| 122 |
/** |
| 123 |
* Returns the total number of pages. |
| 124 |
*/ |
| 125 |
public function getPageCount() : ?int |
| 126 |
{ |
| 127 |
return $this->itemCount === null ? null : (int) \ceil($this->itemCount / $this->itemsPerPage); |
| 128 |
} |
| 129 |
/** |
| 130 |
* Sets the number of items to display on a single page. |
| 131 |
* @return static |
| 132 |
*/ |
| 133 |
public function setItemsPerPage(int $itemsPerPage) |
| 134 |
{ |
| 135 |
$this->itemsPerPage = \max(1, $itemsPerPage); |
| 136 |
return $this; |
| 137 |
} |
| 138 |
/** |
| 139 |
* Returns the number of items to display on a single page. |
| 140 |
*/ |
| 141 |
public function getItemsPerPage() : int |
| 142 |
{ |
| 143 |
return $this->itemsPerPage; |
| 144 |
} |
| 145 |
/** |
| 146 |
* Sets the total number of items. |
| 147 |
* @return static |
| 148 |
*/ |
| 149 |
public function setItemCount(?int $itemCount = null) |
| 150 |
{ |
| 151 |
$this->itemCount = $itemCount === null ? null : \max(0, $itemCount); |
| 152 |
return $this; |
| 153 |
} |
| 154 |
/** |
| 155 |
* Returns the total number of items. |
| 156 |
*/ |
| 157 |
public function getItemCount() : ?int |
| 158 |
{ |
| 159 |
return $this->itemCount; |
| 160 |
} |
| 161 |
/** |
| 162 |
* Returns the absolute index of the first item on current page. |
| 163 |
*/ |
| 164 |
public function getOffset() : int |
| 165 |
{ |
| 166 |
return $this->getPageIndex() * $this->itemsPerPage; |
| 167 |
} |
| 168 |
/** |
| 169 |
* Returns the absolute index of the first item on current page in countdown paging. |
| 170 |
*/ |
| 171 |
public function getCountdownOffset() : ?int |
| 172 |
{ |
| 173 |
return $this->itemCount === null ? null : \max(0, $this->itemCount - ($this->getPageIndex() + 1) * $this->itemsPerPage); |
| 174 |
} |
| 175 |
/** |
| 176 |
* Returns the number of items on current page. |
| 177 |
*/ |
| 178 |
public function getLength() : int |
| 179 |
{ |
| 180 |
return $this->itemCount === null ? $this->itemsPerPage : \min($this->itemsPerPage, $this->itemCount - $this->getPageIndex() * $this->itemsPerPage); |
| 181 |
} |
| 182 |
} |
| 183 |
|