• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nette / utils / 5707056869

pending completion
5707056869

push

github

dg
support for PHP 8.3

3 of 3 new or added lines in 1 file covered. (100.0%)

1580 of 1753 relevant lines covered (90.13%)

0.9 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.48
/src/Utils/Paginator.php
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

8
declare(strict_types=1);
9

10
namespace Nette\Utils;
11

12
use Nette;
13

14

15
/**
16
 * Paginating math.
17
 *
18
 * @property   int $page
19
 * @property-read int $firstPage
20
 * @property-read int|null $lastPage
21
 * @property-read int<0,max> $firstItemOnPage
22
 * @property-read int<0,max> $lastItemOnPage
23
 * @property   int $base
24
 * @property-read bool $first
25
 * @property-read bool $last
26
 * @property-read int<0,max>|null $pageCount
27
 * @property   positive-int $itemsPerPage
28
 * @property   int<0,max>|null $itemCount
29
 * @property-read int<0,max> $offset
30
 * @property-read int<0,max>|null $countdownOffset
31
 * @property-read int<0,max> $length
32
 */
33
class Paginator
34
{
35
        use Nette\SmartObject;
36

37
        /** @var int */
38
        private $base = 1;
39

40
        /** @var int */
41
        private $itemsPerPage = 1;
42

43
        /** @var int */
44
        private $page = 1;
45

46
        /** @var int|null */
47
        private $itemCount;
48

49

50
        /**
51
         * Sets current page number.
52
         * @return static
53
         */
54
        public function setPage(int $page)
1✔
55
        {
56
                $this->page = $page;
1✔
57
                return $this;
1✔
58
        }
59

60

61
        /**
62
         * Returns current page number.
63
         */
64
        public function getPage(): int
65
        {
66
                return $this->base + $this->getPageIndex();
1✔
67
        }
68

69

70
        /**
71
         * Returns first page number.
72
         */
73
        public function getFirstPage(): int
74
        {
75
                return $this->base;
1✔
76
        }
77

78

79
        /**
80
         * Returns last page number.
81
         */
82
        public function getLastPage(): ?int
83
        {
84
                return $this->itemCount === null
1✔
85
                        ? null
1✔
86
                        : $this->base + max(0, $this->getPageCount() - 1);
1✔
87
        }
88

89

90
        /**
91
         * Returns the sequence number of the first element on the page
92
         * @return int<0, max>
93
         */
94
        public function getFirstItemOnPage(): int
95
        {
96
                return $this->itemCount !== 0
1✔
97
                        ? $this->offset + 1
1✔
98
                        : 0;
1✔
99
        }
100

101

102
        /**
103
         * Returns the sequence number of the last element on the page
104
         * @return int<0, max>
105
         */
106
        public function getLastItemOnPage(): int
107
        {
108
                return $this->offset + $this->length;
1✔
109
        }
110

111

112
        /**
113
         * Sets first page (base) number.
114
         * @return static
115
         */
116
        public function setBase(int $base)
1✔
117
        {
118
                $this->base = $base;
1✔
119
                return $this;
1✔
120
        }
121

122

123
        /**
124
         * Returns first page (base) number.
125
         */
126
        public function getBase(): int
127
        {
128
                return $this->base;
×
129
        }
130

131

132
        /**
133
         * Returns zero-based page number.
134
         * @return int<0, max>
135
         */
136
        protected function getPageIndex(): int
137
        {
138
                $index = max(0, $this->page - $this->base);
1✔
139
                return $this->itemCount === null
1✔
140
                        ? $index
1✔
141
                        : min($index, max(0, $this->getPageCount() - 1));
1✔
142
        }
143

144

145
        /**
146
         * Is the current page the first one?
147
         */
148
        public function isFirst(): bool
149
        {
150
                return $this->getPageIndex() === 0;
1✔
151
        }
152

153

154
        /**
155
         * Is the current page the last one?
156
         */
157
        public function isLast(): bool
158
        {
159
                return $this->itemCount === null
1✔
160
                        ? false
×
161
                        : $this->getPageIndex() >= $this->getPageCount() - 1;
1✔
162
        }
163

164

165
        /**
166
         * Returns the total number of pages.
167
         * @return int<0, max>|null
168
         */
169
        public function getPageCount(): ?int
170
        {
171
                return $this->itemCount === null
1✔
172
                        ? null
1✔
173
                        : (int) ceil($this->itemCount / $this->itemsPerPage);
1✔
174
        }
175

176

177
        /**
178
         * Sets the number of items to display on a single page.
179
         * @return static
180
         */
181
        public function setItemsPerPage(int $itemsPerPage)
1✔
182
        {
183
                $this->itemsPerPage = max(1, $itemsPerPage);
1✔
184
                return $this;
1✔
185
        }
186

187

188
        /**
189
         * Returns the number of items to display on a single page.
190
         * @return positive-int
191
         */
192
        public function getItemsPerPage(): int
193
        {
194
                return $this->itemsPerPage;
×
195
        }
196

197

198
        /**
199
         * Sets the total number of items.
200
         * @return static
201
         */
202
        public function setItemCount(?int $itemCount = null)
1✔
203
        {
204
                $this->itemCount = $itemCount === null ? null : max(0, $itemCount);
1✔
205
                return $this;
1✔
206
        }
207

208

209
        /**
210
         * Returns the total number of items.
211
         * @return int<0, max>|null
212
         */
213
        public function getItemCount(): ?int
214
        {
215
                return $this->itemCount;
×
216
        }
217

218

219
        /**
220
         * Returns the absolute index of the first item on current page.
221
         * @return int<0, max>
222
         */
223
        public function getOffset(): int
224
        {
225
                return $this->getPageIndex() * $this->itemsPerPage;
1✔
226
        }
227

228

229
        /**
230
         * Returns the absolute index of the first item on current page in countdown paging.
231
         * @return int<0, max>|null
232
         */
233
        public function getCountdownOffset(): ?int
234
        {
235
                return $this->itemCount === null
1✔
236
                        ? null
1✔
237
                        : max(0, $this->itemCount - ($this->getPageIndex() + 1) * $this->itemsPerPage);
1✔
238
        }
239

240

241
        /**
242
         * Returns the number of items on current page.
243
         * @return int<0, max>
244
         */
245
        public function getLength(): int
246
        {
247
                return $this->itemCount === null
1✔
248
                        ? $this->itemsPerPage
1✔
249
                        : min($this->itemsPerPage, $this->itemCount - $this->getPageIndex() * $this->itemsPerPage);
1✔
250
        }
251
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc