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

nette / utils / 22290136219

23 Feb 2026 01:47AM UTC coverage: 93.125% (-0.003%) from 93.128%
22290136219

push

github

dg
added CLAUDE.md

2086 of 2240 relevant lines covered (93.13%)

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 declare(strict_types=1);
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
namespace Nette\Utils;
9

10
use Nette;
11

12

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

35
        private int $base = 1;
36

37
        /** @var positive-int */
38
        private int $itemsPerPage = 1;
39

40
        private int $page = 1;
41

42
        /** @var ?int<0, max> */
43
        private ?int $itemCount = null;
44

45

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

52

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

58

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

64

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

72

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

84

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

94

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

101

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

107

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

120

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

126

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

134

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

145

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

152

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

161

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

168

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

177

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

187

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

199

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