Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
4.35% covered (danger)
4.35%
2 / 46
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
PagerFunction
4.35% covered (danger)
4.35%
2 / 46
66.67% covered (warning)
66.67%
2 / 3
138.02
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getFunctionName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 functionBodyImpl
0.00% covered (danger)
0.00%
0 / 44
0.00% covered (danger)
0.00%
0 / 1
110
1<?php
2
3declare(strict_types=1);
4
5namespace PeServer\Core\Mvc\Template\Plugin;
6
7use Exception;
8use PeServer\Core\Code;
9use PeServer\Core\Collection\Arr;
10use PeServer\Core\Html\HtmlDocument;
11use PeServer\Core\Mvc\PageShortcut;
12use PeServer\Core\Mvc\PageShortcutKind;
13use PeServer\Core\Mvc\Pagination;
14use PeServer\Core\Mvc\Template\Plugin\TemplateFunctionBase;
15use PeServer\Core\Text;
16
17class PagerFunction extends TemplateFunctionBase
18{
19    public function __construct(TemplatePluginArgument $argument)
20    {
21        parent::__construct($argument);
22    }
23
24    #region TemplateFunctionBase
25
26    public function getFunctionName(): string
27    {
28        return 'pager';
29    }
30
31    protected function functionBodyImpl(): string
32    {
33        /** @var Pagination */
34        $pagination = $this->params['data'];
35
36        $shortcuts = $pagination->getShortcuts();
37        if (empty($shortcuts)) {
38            return '';
39        }
40
41        /** @var string */
42        $href = $this->params['href'];
43        if (Text::isNullOrWhiteSpace($href)) {
44            throw new Exception('href');
45        }
46        $href = Code::toLiteralString($href);
47
48        $jumpHead = $this->params['jump-head'] ?? '<<';
49        $jumpTail = $this->params['jump-tail'] ?? '>>';
50        $jumpPrev = $this->params['jump-prev'] ?? '<';
51        $jumpNext = $this->params['jump-next'] ?? '>';
52
53        $dom = new HtmlDocument();
54
55        $parent = $dom->addTagElement('ul');
56        $parent->addClass('pagination');
57
58        foreach ($shortcuts as $index => $shortcut) {
59            $item = $parent->addTagElement('li');
60            $link = $item->addTagElement('a');
61
62            $item->addClass($shortcut->kind->value);
63
64            if ($shortcut->enabled) {
65                $link->setAttribute('href', Text::replaceMap(
66                    $href,
67                    [
68                        'page_number' => (string)$shortcut->pageNumber,
69                    ],
70                    '<',
71                    '>'
72                ));
73            } else {
74                $item->addClass('disabled');
75                $link->addClass('disabled');
76                $link->setAttribute('tabindex', '-1');
77            }
78
79            if ($shortcut->kind === PageShortcutKind::Long) {
80                if ($index === 0) {
81                    $link->addText($jumpHead);
82                } else {
83                    $link->addText($jumpTail);
84                }
85            } elseif ($shortcut->kind === PageShortcutKind::Short) {
86                if ($index <= 1) {
87                    $link->addText($jumpPrev);
88                } else {
89                    $link->addText($jumpNext);
90                }
91            } else {
92                $link->addText((string)$shortcut->pageNumber);
93                if ($shortcut->current) {
94                    $item->addClass('current');
95                    $link->addClass('current');
96                }
97            }
98        }
99
100
101        return $dom->build();
102    }
103
104    #endregion
105}