Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Details | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 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 $firstItemOnPage
22
 * @property-read int $lastItemOnPage
23
 * @property   int $base
24
 * @property-read bool $first
25
 * @property-read bool $last
26
 * @property-read int|null $pageCount
27
 * @property   int $itemsPerPage
28
 * @property   int|null $itemCount
29
 * @property-read int $offset
30
 * @property-read int|null $countdownOffset
31
 * @property-read int $length
32
 */
33
class Paginator
34
{
35
	use Nette\SmartObject;
36
 
399 lars 37
	private int $base = 1;
38
	private int $itemsPerPage = 1;
39
	private int $page = 1;
40
	private ?int $itemCount = null;
148 lars 41
 
42
 
43
	/**
44
	 * Sets current page number.
45
	 */
399 lars 46
	public function setPage(int $page): static
148 lars 47
	{
48
		$this->page = $page;
49
		return $this;
50
	}
51
 
52
 
53
	/**
54
	 * Returns current page number.
55
	 */
56
	public function getPage(): int
57
	{
58
		return $this->base + $this->getPageIndex();
59
	}
60
 
61
 
62
	/**
63
	 * Returns first page number.
64
	 */
65
	public function getFirstPage(): int
66
	{
67
		return $this->base;
68
	}
69
 
70
 
71
	/**
72
	 * Returns last page number.
73
	 */
74
	public function getLastPage(): ?int
75
	{
76
		return $this->itemCount === null
77
			? null
78
			: $this->base + max(0, $this->getPageCount() - 1);
79
	}
80
 
81
 
82
	/**
83
	 * Returns the sequence number of the first element on the page
84
	 */
85
	public function getFirstItemOnPage(): int
86
	{
87
		return $this->itemCount !== 0
88
			? $this->offset + 1
89
			: 0;
90
	}
91
 
92
 
93
	/**
94
	 * Returns the sequence number of the last element on the page
95
	 */
96
	public function getLastItemOnPage(): int
97
	{
98
		return $this->offset + $this->length;
99
	}
100
 
101
 
102
	/**
103
	 * Sets first page (base) number.
104
	 */
399 lars 105
	public function setBase(int $base): static
148 lars 106
	{
107
		$this->base = $base;
108
		return $this;
109
	}
110
 
111
 
112
	/**
113
	 * Returns first page (base) number.
114
	 */
115
	public function getBase(): int
116
	{
117
		return $this->base;
118
	}
119
 
120
 
121
	/**
122
	 * Returns zero-based page number.
123
	 */
124
	protected function getPageIndex(): int
125
	{
126
		$index = max(0, $this->page - $this->base);
127
		return $this->itemCount === null
128
			? $index
129
			: min($index, max(0, $this->getPageCount() - 1));
130
	}
131
 
132
 
133
	/**
134
	 * Is the current page the first one?
135
	 */
136
	public function isFirst(): bool
137
	{
138
		return $this->getPageIndex() === 0;
139
	}
140
 
141
 
142
	/**
143
	 * Is the current page the last one?
144
	 */
145
	public function isLast(): bool
146
	{
147
		return $this->itemCount === null
148
			? false
149
			: $this->getPageIndex() >= $this->getPageCount() - 1;
150
	}
151
 
152
 
153
	/**
154
	 * Returns the total number of pages.
155
	 */
156
	public function getPageCount(): ?int
157
	{
158
		return $this->itemCount === null
159
			? null
160
			: (int) ceil($this->itemCount / $this->itemsPerPage);
161
	}
162
 
163
 
164
	/**
165
	 * Sets the number of items to display on a single page.
166
	 */
399 lars 167
	public function setItemsPerPage(int $itemsPerPage): static
148 lars 168
	{
169
		$this->itemsPerPage = max(1, $itemsPerPage);
170
		return $this;
171
	}
172
 
173
 
174
	/**
175
	 * Returns the number of items to display on a single page.
176
	 */
177
	public function getItemsPerPage(): int
178
	{
179
		return $this->itemsPerPage;
180
	}
181
 
182
 
183
	/**
184
	 * Sets the total number of items.
185
	 */
399 lars 186
	public function setItemCount(?int $itemCount = null): static
148 lars 187
	{
188
		$this->itemCount = $itemCount === null ? null : max(0, $itemCount);
189
		return $this;
190
	}
191
 
192
 
193
	/**
194
	 * Returns the total number of items.
195
	 */
196
	public function getItemCount(): ?int
197
	{
198
		return $this->itemCount;
199
	}
200
 
201
 
202
	/**
203
	 * Returns the absolute index of the first item on current page.
204
	 */
205
	public function getOffset(): int
206
	{
207
		return $this->getPageIndex() * $this->itemsPerPage;
208
	}
209
 
210
 
211
	/**
212
	 * Returns the absolute index of the first item on current page in countdown paging.
213
	 */
214
	public function getCountdownOffset(): ?int
215
	{
216
		return $this->itemCount === null
217
			? null
218
			: max(0, $this->itemCount - ($this->getPageIndex() + 1) * $this->itemsPerPage);
219
	}
220
 
221
 
222
	/**
223
	 * Returns the number of items on current page.
224
	 */
225
	public function getLength(): int
226
	{
227
		return $this->itemCount === null
228
			? $this->itemsPerPage
229
			: min($this->itemsPerPage, $this->itemCount - $this->getPageIndex() * $this->itemsPerPage);
230
	}
231
}