Pagination   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 161
Duplicated Lines 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 7
eloc 112
c 3
b 1
f 0
dl 0
loc 161
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A script() 0 3 1
B render() 0 99 5
A getMetadata() 0 6 1
1
<?php declare(strict_types=1);
2
3
namespace Formularium\Frontend\Vue\Element;
4
5
use Formularium\Element;
6
use Formularium\Exception\ClassNotFoundException;
7
use Formularium\HTMLNode;
8
use Formularium\Framework;
9
use Formularium\Metadata;
10
use Formularium\MetadataParameter;
11
12
class Pagination extends Element
13
{
14
    public function render(array $parameters, HTMLNode $previous): HTMLNode
15
    {
16
        $node = HTMLNode::factory(
17
            'nav',
18
            ['class' => 'formularium-pagination-wrapper', 'aria-label' => "Page navigation"],
19
            HTMLNode::factory(
20
                'ul',
21
                [
22
                    'class' => 'formularium-pagination',
23
                    'v-if' => 'lastPage > 1'
24
                ],
25
                [
26
                    HTMLNode::factory(
27
                        'li',
28
                        [
29
                            "class" => "formularium-pagination-item",
30
                            "v-show" => "currentPage > pagesAround",
31
                            "@click" => "\$emit('page', 1)"
32
                        ],
33
                        HTMLNode::factory(
34
                            'a',
35
                            [
36
                                "class" => "formularium-pagination-link",
37
                                ":href" => "basePath + '/1'",
38
                                "@click.prevent" => ""
39
                            ],
40
                            "1",
41
                            true
42
                        )
43
                    ),
44
                    HTMLNode::factory(
45
                        'li',
46
                        [
47
                            "class" => "formularium-pagination-item",
48
                            "v-show" => "currentPage > pagesAround"
49
                        ],
50
                        "...",
51
                        true
52
                    ),
53
                    HTMLNode::factory(
54
                        'li',
55
                        [
56
                            "class" => "formularium-pagination-item",
57
                            "v-for" => "p in pages",
58
                            "v-bind:key" => "p.page",
59
                            "@click" => "\$emit('page', p.page)"
60
                        ],
61
                        [
62
                            HTMLNode::factory(
63
                                'a',
64
                                [
65
                                    "v-if" => "p.page == currentPage",
66
                                    "class" => ["formularium-pagination-link", "formularium-pagination-current"],
67
                                    ":href" => "baseLink + p.page",
68
                                    "@click.prevent" => ""
69
                                ],
70
                                "{{p.page}}"
71
                            ),
72
                            HTMLNode::factory(
73
                                'a',
74
                                [
75
                                    "v-else" => null,
76
                                    "class" => "formularium-pagination-link",
77
                                    ":href" => "baseLink + p.page",
78
                                    "@click.prevent" => ""
79
                                ],
80
                                "{{p.page}}"
81
                            )
82
                        ]
83
                    ),
84
                    HTMLNode::factory(
85
                        'li',
86
                        [
87
                            "class" => "formularium-pagination-item",
88
                            "v-show" => "lastPage > currentPage + pagesAround",
89
                        ],
90
                        "..."
91
                    )
92
                ]
93
            )
94
        );
95
96
        foreach ($this->composer->getFrameworks() as $framework) {
97
            /**
98
             * @var Framework $framework
99
             */
100
            $f =$framework->getName();
101
            if ($f === 'HTML' || $f === 'Vue') {
102
                continue;
103
            }
104
            try {
105
                $element = $framework->getElement($this->getName(), $this->composer);
106
                $node = $element->render($parameters, $node);
107
            } catch (ClassNotFoundException $e) {
108
                continue; // element default
109
            }
110
        }
111
112
        return $node;
113
    }
114
115
    public static function script(): string
116
    {
117
        return <<<EOF
118
export default {
119
    props: {
120
        basePath: {
121
            type: String,
122
            default: ""
123
        },
124
        currentPage: {
125
            type: Number,
126
            required: true
127
        },
128
        perPage: {
129
            type: Number,
130
            required: true
131
        },
132
        lastPage: {
133
            type: Number,
134
            required: true
135
        },
136
        total: {
137
            type: Number,
138
            required: true
139
        },
140
        pagesAround: {
141
            type: Number,
142
            default: 4
143
        },
144
    },
145
146
    computed: {
147
        pages() {
148
            let first = Math.max(1, this.currentPage - this.pagesAround + 1);
149
            let last = Math.min(this.lastPage, this.currentPage + this.pagesAround);
150
            let pages = [];
151
            for (let i = first; i <= last; i++) {
152
                pages.push({page: i});
153
            }
154
            return pages;
155
        },
156
        baseLink() {
157
            if (this.basePath.indexOf('?') === -1) {
158
                return this.basePath + '?page=';
159
            }
160
            return this.basePath + '&page=';
161
        }
162
    }
163
}
164
EOF;
165
    }
166
167
    public static function getMetadata(): Metadata
168
    {
169
        return new Metadata(
170
            'Pagination',
171
            'Creates a pagination element',
172
            [
173
            ]
174
        );
175
    }
176
}
177