1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\DataView; |
6
|
|
|
|
7
|
|
|
use Yiisoft\Data\Paginator\PaginatorInterface; |
8
|
|
|
use Yiisoft\Router\CurrentRoute; |
9
|
|
|
use Yiisoft\Router\UrlGeneratorInterface; |
10
|
|
|
use Yiisoft\Widget\Widget; |
11
|
|
|
|
12
|
|
|
use function array_merge; |
13
|
|
|
use function http_build_query; |
14
|
|
|
|
15
|
|
|
abstract class BasePagination extends Widget |
16
|
|
|
{ |
17
|
|
|
private array $attributes = []; |
18
|
|
|
private bool $disabledNextPage = false; |
19
|
|
|
private bool $disabledPreviousPage = false; |
20
|
|
|
private bool $hideOnSinglePage = true; |
21
|
|
|
private array $iconAttributes = []; |
22
|
|
|
private string $iconClassNextPage = ''; |
23
|
|
|
private string $iconClassPreviousPage = ''; |
24
|
|
|
private array $iconContainerAttributes = []; |
25
|
|
|
private string $iconNextPage = ''; |
26
|
|
|
private string $iconPreviousPage = ''; |
27
|
|
|
private string $labelNextPage = 'Next Page'; |
28
|
|
|
private string $labelPreviousPage = 'Previous'; |
29
|
|
|
private string $menuClass = 'pagination'; |
30
|
|
|
private string $menuItemContainerClass = 'page-item'; |
31
|
|
|
private string $menuItemLinkClass = 'page-link'; |
32
|
|
|
private array $pageConfig = []; |
33
|
|
|
private string $pageName = 'page'; |
34
|
|
|
private string $pageSizeName = 'pagesize'; |
35
|
|
|
private PaginatorInterface|null $paginator = null; |
36
|
|
|
private ?array $urlArguments = null; |
37
|
|
|
private array $urlQueryParameters = []; |
38
|
|
|
|
39
|
24 |
|
public function __construct( |
40
|
|
|
private CurrentRoute $currentRoute, |
41
|
|
|
private UrlGeneratorInterface|null $urlGenerator = null |
42
|
|
|
) { |
43
|
24 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Returns a new instance with the HTML attributes. The following special options are recognized. |
47
|
|
|
* |
48
|
|
|
* @param array $values Attribute values indexed by attribute names. |
49
|
|
|
*/ |
50
|
1 |
|
public function attributes(array $values): static |
51
|
|
|
{ |
52
|
1 |
|
$new = clone $this; |
53
|
1 |
|
$new->attributes = $values; |
54
|
|
|
|
55
|
1 |
|
return $new; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Return a new instance with disabled next page. |
60
|
|
|
* |
61
|
|
|
* @param bool $value Whether to disable next page. |
62
|
|
|
*/ |
63
|
1 |
|
public function disabledNextPage(bool $value): static |
64
|
|
|
{ |
65
|
1 |
|
$new = clone $this; |
66
|
1 |
|
$new->disabledNextPage = $value; |
67
|
|
|
|
68
|
1 |
|
return $new; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Return a new instance with disabled previous page. |
73
|
|
|
* |
74
|
|
|
* @param bool $value Whether to disable previous page. |
75
|
|
|
*/ |
76
|
3 |
|
public function disabledPreviousPage(bool $value): static |
77
|
|
|
{ |
78
|
3 |
|
$new = clone $this; |
79
|
3 |
|
$new->disabledPreviousPage = $value; |
80
|
|
|
|
81
|
3 |
|
return $new; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Return a new instance with hide on single page. |
86
|
|
|
* |
87
|
|
|
* @param bool $value The value indicating whether to hide the widget when there is only one page. |
88
|
|
|
*/ |
89
|
1 |
|
public function hideOnSinglePage(bool $value): static |
90
|
|
|
{ |
91
|
1 |
|
$new = clone $this; |
92
|
1 |
|
$new->hideOnSinglePage = $value; |
93
|
|
|
|
94
|
1 |
|
return $new; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Returns a new instance with the HTML attributes for icon attributes `<i>`. |
99
|
|
|
* |
100
|
|
|
* @param array $values Attribute values indexed by attribute names. |
101
|
|
|
*/ |
102
|
1 |
|
public function iconAttributes(array $values): static |
103
|
|
|
{ |
104
|
1 |
|
$new = clone $this; |
105
|
1 |
|
$new->iconAttributes = $values; |
106
|
|
|
|
107
|
1 |
|
return $new; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Returns a new instance with the icon class for icon attributes `<i>` for link next page. |
112
|
|
|
* |
113
|
|
|
* @param string $value The icon class. |
114
|
|
|
*/ |
115
|
3 |
|
public function iconClassNextPage(string $value): static |
116
|
|
|
{ |
117
|
3 |
|
$new = clone $this; |
118
|
3 |
|
$new->iconClassNextPage = $value; |
119
|
3 |
|
$new->labelNextPage = ''; |
120
|
|
|
|
121
|
3 |
|
return $new; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Returns a new instance with the icon class for icon attributes `<i>` for link previous page. |
126
|
|
|
* |
127
|
|
|
* @param string $value The icon class. |
128
|
|
|
*/ |
129
|
3 |
|
public function iconClassPreviousPage(string $value): static |
130
|
|
|
{ |
131
|
3 |
|
$new = clone $this; |
132
|
3 |
|
$new->iconClassPreviousPage = $value; |
133
|
3 |
|
$new->labelPreviousPage = ''; |
134
|
|
|
|
135
|
3 |
|
return $new; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Returns a new instance with the HTML attributes for icon container attributes `<span>`. |
140
|
|
|
* |
141
|
|
|
* @param array $values Attribute values indexed by attribute names. |
142
|
|
|
*/ |
143
|
1 |
|
public function iconContainerAttributes(array $values): static |
144
|
|
|
{ |
145
|
1 |
|
$new = clone $this; |
146
|
1 |
|
$new->iconContainerAttributes = $values; |
147
|
|
|
|
148
|
1 |
|
return $new; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Return a new instance with icon next page. |
153
|
|
|
* |
154
|
|
|
* @param string $value The icon next page. |
155
|
|
|
*/ |
156
|
3 |
|
public function iconNextPage(string $value): static |
157
|
|
|
{ |
158
|
3 |
|
$new = clone $this; |
159
|
3 |
|
$new->iconNextPage = $value; |
160
|
3 |
|
$new->labelNextPage = ''; |
161
|
|
|
|
162
|
3 |
|
return $new; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Return a new instance with icon previous page. |
167
|
|
|
* |
168
|
|
|
* @param string $value The icon previous page. |
169
|
|
|
*/ |
170
|
3 |
|
public function iconPreviousPage(string $value): static |
171
|
|
|
{ |
172
|
3 |
|
$new = clone $this; |
173
|
3 |
|
$new->iconPreviousPage = $value; |
174
|
3 |
|
$new->labelPreviousPage = ''; |
175
|
|
|
|
176
|
3 |
|
return $new; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Return a new instance with label for next page. |
181
|
|
|
* |
182
|
|
|
* @param string $value The label for next page. |
183
|
|
|
*/ |
184
|
2 |
|
public function labelNextPage(string $value = ''): static |
185
|
|
|
{ |
186
|
2 |
|
$new = clone $this; |
187
|
2 |
|
$new->labelNextPage = $value; |
188
|
|
|
|
189
|
2 |
|
return $new; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Return a new instance with label for previous page. |
194
|
|
|
* |
195
|
|
|
* @param string $value The label for previous page. |
196
|
|
|
*/ |
197
|
2 |
|
public function labelPreviousPage(string $value = ''): static |
198
|
|
|
{ |
199
|
2 |
|
$new = clone $this; |
200
|
2 |
|
$new->labelPreviousPage = $value; |
201
|
|
|
|
202
|
2 |
|
return $new; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Return a new instance with class css for menu tag `<ul>`. |
207
|
|
|
* |
208
|
|
|
* @param string $value The class css for menu tag `<ul>`. |
209
|
|
|
*/ |
210
|
4 |
|
public function menuClass(string $value): static |
211
|
|
|
{ |
212
|
4 |
|
$new = clone $this; |
213
|
4 |
|
$new->menuClass = $value; |
214
|
|
|
|
215
|
4 |
|
return $new; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Return a new instance with class css for menu item tag `<li>`. |
220
|
|
|
* |
221
|
|
|
* @param string $value The class css for menu item tag `<li>`. |
222
|
|
|
*/ |
223
|
1 |
|
public function menuItemContainerClass(string $value): static |
224
|
|
|
{ |
225
|
1 |
|
$new = clone $this; |
226
|
1 |
|
$new->menuItemContainerClass = $value; |
227
|
|
|
|
228
|
1 |
|
return $new; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Return a new instance with page config for arguments or query parameters in url. |
233
|
|
|
* |
234
|
|
|
* @param array $value The page config for arguments or query parameters in url. |
235
|
|
|
*/ |
236
|
1 |
|
public function pageConfig(array $value): static |
237
|
|
|
{ |
238
|
1 |
|
$new = clone $this; |
239
|
1 |
|
$new->pageConfig = $value; |
240
|
|
|
|
241
|
1 |
|
return $new; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Return a new instance with name of argument or query parameter for page. |
246
|
|
|
* |
247
|
|
|
* @param string $value The name of argument or query parameter for page. |
248
|
|
|
*/ |
249
|
1 |
|
public function pageName(string $value): static |
250
|
|
|
{ |
251
|
1 |
|
$new = clone $this; |
252
|
1 |
|
$new->pageName = $value; |
253
|
|
|
|
254
|
1 |
|
return $new; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Return a new instance with name of argument or query parameter for page size. |
259
|
|
|
* |
260
|
|
|
* @param string $value The name of argument or query parameter for page size. |
261
|
|
|
*/ |
262
|
1 |
|
public function pageSizeName(string $value): static |
263
|
|
|
{ |
264
|
1 |
|
$new = clone $this; |
265
|
1 |
|
$new->pageSizeName = $value; |
266
|
|
|
|
267
|
1 |
|
return $new; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Returns a new instance with the paginator interface of the grid view, detail view, or list view. |
272
|
|
|
* |
273
|
|
|
* @param PaginatorInterface $value The paginator interface of the grid view, detail view, or list view. |
274
|
|
|
*/ |
275
|
22 |
|
public function paginator(PaginatorInterface $value): static |
276
|
|
|
{ |
277
|
22 |
|
$new = clone $this; |
278
|
22 |
|
$new->paginator = $value; |
279
|
|
|
|
280
|
22 |
|
return $new; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* Return a new instance with arguments of the route. |
285
|
|
|
* |
286
|
|
|
* @param array $value Arguments of the route. |
287
|
|
|
*/ |
288
|
11 |
|
public function urlArguments(array $value): static |
289
|
|
|
{ |
290
|
11 |
|
$new = clone $this; |
291
|
11 |
|
$new->urlArguments = $value; |
292
|
|
|
|
293
|
11 |
|
return $new; |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* Return a new instance with query parameters of the route. |
298
|
|
|
* |
299
|
|
|
* @param array $value The query parameters of the route. |
300
|
|
|
*/ |
301
|
4 |
|
public function urlQueryParameters(array $value): static |
302
|
|
|
{ |
303
|
4 |
|
$new = clone $this; |
304
|
4 |
|
$new->urlQueryParameters = $value; |
305
|
|
|
|
306
|
4 |
|
return $new; |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* Creates the URL suitable for pagination with the specified page number. This method is mainly called by pagers |
311
|
|
|
* when creating URLs used to perform pagination. |
312
|
|
|
* |
313
|
|
|
* @param int $page the zero-based page number that the URL should point to. |
314
|
|
|
*/ |
315
|
18 |
|
protected function createUrl(int $page): string |
316
|
|
|
{ |
317
|
18 |
|
if ($this->urlGenerator === null) { |
318
|
1 |
|
throw new Exception\UrlGeneratorNotSetException(); |
319
|
|
|
} |
320
|
|
|
|
321
|
17 |
|
$pageConfig = $this->pageConfig; |
322
|
17 |
|
$urlQueryParameters = $this->urlQueryParameters; |
323
|
|
|
|
324
|
17 |
|
if ($pageConfig === []) { |
325
|
17 |
|
$pageConfig = [ |
326
|
17 |
|
$this->pageName => $page, |
327
|
17 |
|
$this->pageSizeName => $this->getPaginator()->getPageSize(), |
328
|
17 |
|
]; |
329
|
|
|
} |
330
|
|
|
|
331
|
17 |
|
if ($this->urlArguments !== null) { |
332
|
|
|
/** @psalm-var array<string,string> */ |
333
|
9 |
|
$urlArguments = array_merge($pageConfig, $this->urlArguments); |
334
|
|
|
} else { |
335
|
8 |
|
$urlArguments = []; |
336
|
8 |
|
$urlQueryParameters = array_merge($pageConfig, $this->urlQueryParameters); |
337
|
|
|
} |
338
|
|
|
|
339
|
17 |
|
$urlName = $this->currentRoute->getName(); |
340
|
|
|
|
341
|
17 |
|
return match ($urlName) { |
342
|
17 |
|
null => $urlQueryParameters ? '?' . http_build_query($urlQueryParameters) : '', |
343
|
17 |
|
default => $this->urlGenerator->generate($urlName, $urlArguments, $urlQueryParameters), |
|
|
|
|
344
|
17 |
|
}; |
345
|
|
|
} |
346
|
|
|
|
347
|
21 |
|
protected function getAttributes(): array |
348
|
|
|
{ |
349
|
21 |
|
return $this->attributes; |
350
|
|
|
} |
351
|
|
|
|
352
|
10 |
|
protected function getDisabledNextPage(): bool |
353
|
|
|
{ |
354
|
10 |
|
return $this->disabledNextPage; |
355
|
|
|
} |
356
|
|
|
|
357
|
1 |
|
protected function getDisabledPreviousPage(): bool |
358
|
|
|
{ |
359
|
1 |
|
return $this->disabledPreviousPage; |
360
|
|
|
} |
361
|
|
|
|
362
|
14 |
|
protected function getHideOnSinglePage(): bool |
363
|
|
|
{ |
364
|
14 |
|
return $this->hideOnSinglePage; |
365
|
|
|
} |
366
|
|
|
|
367
|
17 |
|
protected function getIconAttributes(): array |
368
|
|
|
{ |
369
|
17 |
|
return $this->iconAttributes; |
370
|
|
|
} |
371
|
|
|
|
372
|
17 |
|
protected function getIconClassNextPage(): string |
373
|
|
|
{ |
374
|
17 |
|
return $this->iconClassNextPage; |
375
|
|
|
} |
376
|
|
|
|
377
|
18 |
|
protected function getIconClassPreviousPage(): string |
378
|
|
|
{ |
379
|
18 |
|
return $this->iconClassPreviousPage; |
380
|
|
|
} |
381
|
|
|
|
382
|
18 |
|
protected function getIconContainerAttributes(): array |
383
|
|
|
{ |
384
|
18 |
|
return $this->iconContainerAttributes; |
385
|
|
|
} |
386
|
|
|
|
387
|
17 |
|
protected function getIconNextPage(): string |
388
|
|
|
{ |
389
|
17 |
|
return $this->iconNextPage; |
390
|
|
|
} |
391
|
|
|
|
392
|
18 |
|
protected function getIconPreviousPage(): string |
393
|
|
|
{ |
394
|
18 |
|
return $this->iconPreviousPage; |
395
|
|
|
} |
396
|
|
|
|
397
|
18 |
|
protected function getLabelPreviousPage(): string |
398
|
|
|
{ |
399
|
18 |
|
return $this->labelPreviousPage; |
400
|
|
|
} |
401
|
|
|
|
402
|
17 |
|
protected function getLabelNextPage(): string |
403
|
|
|
{ |
404
|
17 |
|
return $this->labelNextPage; |
405
|
|
|
} |
406
|
|
|
|
407
|
17 |
|
protected function getMenuClass(): string |
408
|
|
|
{ |
409
|
17 |
|
return $this->menuClass; |
410
|
|
|
} |
411
|
|
|
|
412
|
17 |
|
protected function getMenuItemContainerClass(): string |
413
|
|
|
{ |
414
|
17 |
|
return $this->menuItemContainerClass; |
415
|
|
|
} |
416
|
|
|
|
417
|
17 |
|
protected function getMenuItemLinkClass(): string |
418
|
|
|
{ |
419
|
17 |
|
return $this->menuItemLinkClass; |
420
|
|
|
} |
421
|
|
|
|
422
|
22 |
|
protected function getPaginator(): PaginatorInterface |
423
|
|
|
{ |
424
|
22 |
|
if ($this->paginator === null) { |
425
|
1 |
|
throw new Exception\PaginatorNotSetException(); |
426
|
|
|
} |
427
|
|
|
|
428
|
21 |
|
return $this->paginator; |
429
|
|
|
} |
430
|
|
|
} |
431
|
|
|
|