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

nette / utils / 21636962765

03 Feb 2026 03:39PM UTC coverage: 93.312% (+0.05%) from 93.264%
21636962765

push

github

dg
added CLAUDE.md

2065 of 2213 relevant lines covered (93.31%)

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 $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> $pageCount
27
 * @property   positive-int $itemsPerPage
28
 * @property   ?int<0,max> $itemCount
29
 * @property-read int<0,max> $offset
30
 * @property-read ?int<0,max> $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> */
45
        private ?int $itemCount = null;
46

47

48
        public function setPage(int $page): static
1✔
49
        {
50
                $this->page = $page;
1✔
51
                return $this;
1✔
52
        }
53

54

55
        public function getPage(): int
56
        {
57
                return $this->base + $this->getPageIndex();
1✔
58
        }
59

60

61
        public function getFirstPage(): int
62
        {
63
                return $this->base;
1✔
64
        }
65

66

67
        public function getLastPage(): ?int
68
        {
69
                return $this->itemCount === null
1✔
70
                        ? null
1✔
71
                        : $this->base + max(0, $this->getPageCount() - 1);
1✔
72
        }
73

74

75
        /**
76
         * Returns the sequence number of the first element on the page
77
         * @return int<0, max>
78
         */
79
        public function getFirstItemOnPage(): int
80
        {
81
                return $this->itemCount !== 0
1✔
82
                        ? $this->offset + 1
1✔
83
                        : 0;
1✔
84
        }
85

86

87
        /**
88
         * Returns the sequence number of the last element on the page
89
         * @return int<0, max>
90
         */
91
        public function getLastItemOnPage(): int
92
        {
93
                return $this->offset + $this->length;
1✔
94
        }
95

96

97
        public function setBase(int $base): static
1✔
98
        {
99
                $this->base = $base;
1✔
100
                return $this;
1✔
101
        }
102

103

104
        public function getBase(): int
105
        {
106
                return $this->base;
×
107
        }
108

109

110
        /**
111
         * Returns zero-based page number.
112
         * @return int<0, max>
113
         */
114
        protected function getPageIndex(): int
115
        {
116
                $index = max(0, $this->page - $this->base);
1✔
117
                return $this->itemCount === null
1✔
118
                        ? $index
1✔
119
                        : min($index, max(0, $this->getPageCount() - 1));
1✔
120
        }
121

122

123
        public function isFirst(): bool
124
        {
125
                return $this->getPageIndex() === 0;
1✔
126
        }
127

128

129
        public function isLast(): bool
130
        {
131
                return $this->itemCount === null
1✔
132
                        ? false
×
133
                        : $this->getPageIndex() >= $this->getPageCount() - 1;
1✔
134
        }
135

136

137
        /**
138
         * @return ?int<0, max>
139
         */
140
        public function getPageCount(): ?int
141
        {
142
                return $this->itemCount === null
1✔
143
                        ? null
1✔
144
                        : (int) ceil($this->itemCount / $this->itemsPerPage);
1✔
145
        }
146

147

148
        public function setItemsPerPage(int $itemsPerPage): static
1✔
149
        {
150
                $this->itemsPerPage = max(1, $itemsPerPage);
1✔
151
                return $this;
1✔
152
        }
153

154

155
        /**
156
         * @return positive-int
157
         */
158
        public function getItemsPerPage(): int
159
        {
160
                return $this->itemsPerPage;
×
161
        }
162

163

164
        public function setItemCount(?int $itemCount = null): static
1✔
165
        {
166
                $this->itemCount = $itemCount === null ? null : max(0, $itemCount);
1✔
167
                return $this;
1✔
168
        }
169

170

171
        /**
172
         * @return ?int<0, max>
173
         */
174
        public function getItemCount(): ?int
175
        {
176
                return $this->itemCount;
×
177
        }
178

179

180
        /**
181
         * Returns the absolute index of the first item on current page.
182
         * @return int<0, max>
183
         */
184
        public function getOffset(): int
185
        {
186
                return $this->getPageIndex() * $this->itemsPerPage;
1✔
187
        }
188

189

190
        /**
191
         * Returns the absolute index of the first item on current page in countdown paging.
192
         * @return ?int<0, max>
193
         */
194
        public function getCountdownOffset(): ?int
195
        {
196
                return $this->itemCount === null
1✔
197
                        ? null
1✔
198
                        : max(0, $this->itemCount - ($this->getPageIndex() + 1) * $this->itemsPerPage);
1✔
199
        }
200

201

202
        /**
203
         * Returns the number of items on current page.
204
         * @return int<0, max>
205
         */
206
        public function getLength(): int
207
        {
208
                return $this->itemCount === null
1✔
209
                        ? $this->itemsPerPage
1✔
210
                        : min($this->itemsPerPage, $this->itemCount - $this->getPageIndex() * $this->itemsPerPage);
1✔
211
        }
212
}
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