1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Yiisoft\Yii\DataView; |
||
6 | |||
7 | use Yiisoft\Definitions\Exception\CircularReferenceException; |
||
8 | use Yiisoft\Definitions\Exception\InvalidConfigException; |
||
9 | use Yiisoft\Definitions\Exception\NotInstantiableException; |
||
10 | use Yiisoft\Factory\NotFoundException; |
||
11 | use Yiisoft\Html\Tag\Nav; |
||
12 | use Yiisoft\Yii\Widgets\Menu; |
||
13 | |||
14 | use function array_filter; |
||
15 | use function array_key_exists; |
||
16 | |||
17 | final class KeysetPagination extends BasePagination |
||
18 | { |
||
19 | /** |
||
20 | * @throws InvalidConfigException |
||
21 | * @throws NotFoundException |
||
22 | * @throws NotInstantiableException |
||
23 | * @throws CircularReferenceException |
||
24 | */ |
||
25 | 6 | public function render(): string |
|
26 | { |
||
27 | 6 | $attributes = $this->getAttributes(); |
|
28 | 6 | $items = []; |
|
29 | |||
30 | 6 | $items[] = $this->renderPreviousPageNavLink(); |
|
31 | 6 | $items[] = $this->renderNextPageNavLink(); |
|
32 | |||
33 | 6 | if (!array_key_exists('aria-label', $attributes)) { |
|
34 | 6 | $attributes['aria-label'] = 'Pagination'; |
|
35 | } |
||
36 | |||
37 | 6 | return |
|
38 | 6 | Nav::tag() |
|
39 | 6 | ->attributes($attributes) |
|
40 | 6 | ->content( |
|
41 | 6 | PHP_EOL . |
|
42 | 6 | Menu::widget() |
|
43 | 6 | ->class($this->getMenuClass()) |
|
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
44 | 6 | ->items(array_filter($items)) |
|
45 | 6 | ->itemsContainerClass($this->getMenuItemContainerClass()) |
|
46 | 6 | ->linkClass($this->getMenuItemLinkClass()) . |
|
47 | 6 | PHP_EOL |
|
48 | 6 | ) |
|
49 | 6 | ->encode(false) |
|
50 | 6 | ->render(); |
|
51 | } |
||
52 | |||
53 | 6 | private function renderPreviousPageNavLink(): array |
|
54 | { |
||
55 | 6 | $items = []; |
|
56 | 6 | $iconContainerAttributes = $this->getIconContainerAttributes(); |
|
57 | |||
58 | 6 | if (!array_key_exists('aria-hidden', $iconContainerAttributes)) { |
|
59 | 6 | $iconContainerAttributes['aria-hidden'] = 'true'; |
|
60 | } |
||
61 | |||
62 | if ( |
||
63 | 6 | $this->getLabelPreviousPage() !== '' || |
|
64 | 1 | $this->getIconPreviousPage() !== '' || |
|
65 | 6 | $this->getIconClassPreviousPage() !== '' |
|
66 | ) { |
||
67 | 6 | $paginator = $this->getPaginator(); |
|
68 | 6 | $token = (int) $paginator->getPreviousPageToken(); |
|
69 | |||
70 | 6 | $disabled = $token === 0; |
|
71 | |||
72 | 6 | if ($token > 0) { |
|
73 | 1 | $paginator = $paginator->withPreviousPageToken((string) ($token - 1)); |
|
74 | } |
||
75 | |||
76 | 6 | $items = [ |
|
77 | 6 | 'disabled' => $disabled, |
|
78 | 6 | 'icon' => $this->getIconPreviousPage(), |
|
79 | 6 | 'iconAttributes' => $this->getIconAttributes(), |
|
80 | 6 | 'iconClass' => $this->getIconClassPreviousPage(), |
|
81 | 6 | 'iconContainerAttributes' => $iconContainerAttributes, |
|
82 | 6 | 'label' => $this->getLabelPreviousPage(), |
|
83 | 6 | 'link' => $this->createUrl((int) $paginator->getPreviousPageToken()), |
|
84 | 6 | ]; |
|
85 | } |
||
86 | |||
87 | 6 | return $items; |
|
88 | } |
||
89 | |||
90 | 6 | private function renderNextPageNavLink(): array |
|
91 | { |
||
92 | 6 | $paginator = $this->getPaginator(); |
|
93 | |||
94 | 6 | $iconContainerAttributes = $this->getIconContainerAttributes(); |
|
95 | |||
96 | 6 | if (!array_key_exists('aria-hidden', $iconContainerAttributes)) { |
|
97 | 6 | $iconContainerAttributes['aria-hidden'] = 'true'; |
|
98 | } |
||
99 | |||
100 | 6 | return [ |
|
101 | 6 | 'disabled' => $paginator->getNextPageToken() === null, |
|
102 | 6 | 'icon' => $this->getIconNextPage(), |
|
103 | 6 | 'iconAttributes' => $this->getIconAttributes(), |
|
104 | 6 | 'iconClass' => $this->getIconClassNextPage(), |
|
105 | 6 | 'iconContainerAttributes' => $iconContainerAttributes, |
|
106 | 6 | 'label' => $this->getLabelNextPage(), |
|
107 | 6 | 'link' => $this->createUrl((int) $paginator->getNextPageToken()), |
|
108 | 6 | ]; |
|
109 | } |
||
110 | } |
||
111 |