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

nette / utils / 20431395313

22 Dec 2025 12:06PM UTC coverage: 93.164% (+71.8%) from 21.324%
20431395313

push

github

dg
Html::addText() accepts int|null for back compatibility [Closes #332][Closes #333]

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

140 existing lines in 15 files now uncovered.

2058 of 2209 relevant lines covered (93.16%)

0.93 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
        private int $base = 1;
38

39
        /** @var positive-int */
40
        private int $itemsPerPage = 1;
41

42
        private int $page = 1;
43

44
        /** @var int<0, max>|null */
45
        private ?int $itemCount = null;
46

47

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

57

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

66

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

75

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

86

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

98

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

108

109
        /**
110
         * Sets first page (base) number.
111
         */
112
        public function setBase(int $base): static
1✔
113
        {
114
                $this->base = $base;
1✔
115
                return $this;
1✔
116
        }
117

118

119
        /**
120
         * Returns first page (base) number.
121
         */
122
        public function getBase(): int
123
        {
UNCOV
124
                return $this->base;
×
125
        }
126

127

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

140

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

149

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

160

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

172

173
        /**
174
         * Sets the number of items to display on a single page.
175
         */
176
        public function setItemsPerPage(int $itemsPerPage): static
1✔
177
        {
178
                $this->itemsPerPage = max(1, $itemsPerPage);
1✔
179
                return $this;
1✔
180
        }
181

182

183
        /**
184
         * Returns the number of items to display on a single page.
185
         * @return positive-int
186
         */
187
        public function getItemsPerPage(): int
188
        {
UNCOV
189
                return $this->itemsPerPage;
×
190
        }
191

192

193
        /**
194
         * Sets the total number of items.
195
         */
196
        public function setItemCount(?int $itemCount = null): static
1✔
197
        {
198
                $this->itemCount = $itemCount === null ? null : max(0, $itemCount);
1✔
199
                return $this;
1✔
200
        }
201

202

203
        /**
204
         * Returns the total number of items.
205
         * @return int<0, max>|null
206
         */
207
        public function getItemCount(): ?int
208
        {
UNCOV
209
                return $this->itemCount;
×
210
        }
211

212

213
        /**
214
         * Returns the absolute index of the first item on current page.
215
         * @return int<0, max>
216
         */
217
        public function getOffset(): int
218
        {
219
                return $this->getPageIndex() * $this->itemsPerPage;
1✔
220
        }
221

222

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

234

235
        /**
236
         * Returns the number of items on current page.
237
         * @return int<0, max>
238
         */
239
        public function getLength(): int
240
        {
241
                return $this->itemCount === null
1✔
242
                        ? $this->itemsPerPage
1✔
243
                        : min($this->itemsPerPage, $this->itemCount - $this->getPageIndex() * $this->itemsPerPage);
1✔
244
        }
245
}
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