Subversion-Projekte lars-tiefland.laravel_shop

Revision

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