PluginProbe ʕ •ᴥ•ʔ
FAPI Member / trunk
FAPI Member vtrunk
2.2.33 2.2.32 trunk 1.9.47 2.1.18 2.2.24 2.2.25 2.2.26 2.2.28 2.2.29 2.2.30 2.2.31
fapi-member / libs / nette / utils / src / Utils / Paginator.php
fapi-member / libs / nette / utils / src / Utils Last commit date
ArrayHash.php 2 years ago ArrayList.php 2 years ago Arrays.php 2 years ago Callback.php 2 years ago DateTime.php 2 years ago FileSystem.php 2 years ago Floats.php 2 years ago Helpers.php 2 years ago Html.php 2 years ago Image.php 2 years ago Json.php 2 years ago ObjectHelpers.php 2 years ago ObjectMixin.php 2 years ago Paginator.php 2 years ago Random.php 2 years ago Reflection.php 2 years ago Strings.php 2 years ago Type.php 2 years ago Validators.php 2 years ago exceptions.php 2 years ago
Paginator.php
192 lines
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 FapiMember\Library\Nette\Utils;
9
10 use FapiMember\Library\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<0,max> $firstItemOnPage
18 * @property-read int<0,max> $lastItemOnPage
19 * @property int $base
20 * @property-read bool $first
21 * @property-read bool $last
22 * @property-read int<0,max>|null $pageCount
23 * @property positive-int $itemsPerPage
24 * @property int<0,max>|null $itemCount
25 * @property-read int<0,max> $offset
26 * @property-read int<0,max>|null $countdownOffset
27 * @property-read int<0,max> $length
28 */
29 class Paginator
30 {
31 use 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 * @return int<0, max>
73 */
74 public function getFirstItemOnPage(): int
75 {
76 return ($this->itemCount !== 0) ? $this->offset + 1 : 0;
77 }
78 /**
79 * Returns the sequence number of the last element on the page
80 * @return int<0, max>
81 */
82 public function getLastItemOnPage(): int
83 {
84 return $this->offset + $this->length;
85 }
86 /**
87 * Sets first page (base) number.
88 * @return static
89 */
90 public function setBase(int $base)
91 {
92 $this->base = $base;
93 return $this;
94 }
95 /**
96 * Returns first page (base) number.
97 */
98 public function getBase(): int
99 {
100 return $this->base;
101 }
102 /**
103 * Returns zero-based page number.
104 * @return int<0, max>
105 */
106 protected function getPageIndex(): int
107 {
108 $index = max(0, $this->page - $this->base);
109 return ($this->itemCount === null) ? $index : min($index, max(0, $this->getPageCount() - 1));
110 }
111 /**
112 * Is the current page the first one?
113 */
114 public function isFirst(): bool
115 {
116 return $this->getPageIndex() === 0;
117 }
118 /**
119 * Is the current page the last one?
120 */
121 public function isLast(): bool
122 {
123 return ($this->itemCount === null) ? \false : ($this->getPageIndex() >= $this->getPageCount() - 1);
124 }
125 /**
126 * Returns the total number of pages.
127 * @return int<0, max>|null
128 */
129 public function getPageCount(): ?int
130 {
131 return ($this->itemCount === null) ? null : (int) ceil($this->itemCount / $this->itemsPerPage);
132 }
133 /**
134 * Sets the number of items to display on a single page.
135 * @return static
136 */
137 public function setItemsPerPage(int $itemsPerPage)
138 {
139 $this->itemsPerPage = max(1, $itemsPerPage);
140 return $this;
141 }
142 /**
143 * Returns the number of items to display on a single page.
144 * @return positive-int
145 */
146 public function getItemsPerPage(): int
147 {
148 return $this->itemsPerPage;
149 }
150 /**
151 * Sets the total number of items.
152 * @return static
153 */
154 public function setItemCount(?int $itemCount = null)
155 {
156 $this->itemCount = ($itemCount === null) ? null : max(0, $itemCount);
157 return $this;
158 }
159 /**
160 * Returns the total number of items.
161 * @return int<0, max>|null
162 */
163 public function getItemCount(): ?int
164 {
165 return $this->itemCount;
166 }
167 /**
168 * Returns the absolute index of the first item on current page.
169 * @return int<0, max>
170 */
171 public function getOffset(): int
172 {
173 return $this->getPageIndex() * $this->itemsPerPage;
174 }
175 /**
176 * Returns the absolute index of the first item on current page in countdown paging.
177 * @return int<0, max>|null
178 */
179 public function getCountdownOffset(): ?int
180 {
181 return ($this->itemCount === null) ? null : max(0, $this->itemCount - ($this->getPageIndex() + 1) * $this->itemsPerPage);
182 }
183 /**
184 * Returns the number of items on current page.
185 * @return int<0, max>
186 */
187 public function getLength(): int
188 {
189 return ($this->itemCount === null) ? $this->itemsPerPage : min($this->itemsPerPage, $this->itemCount - $this->getPageIndex() * $this->itemsPerPage);
190 }
191 }
192