Passed
Push — master ( fed342...00a718 )
by Bruno
09:40
created

Renderable_pagination   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 72
c 1
b 0
f 1
dl 0
loc 130
rs 9.84
wmc 32

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getItem() 0 9 1
F glue_url() 0 30 20
A editable() 0 3 1
B pagination() 0 57 9
A viewable() 0 3 1
1
<?php declare(strict_types=1);
2
3
namespace Formularium\Frontend\HTML\Renderable;
4
5
use Formularium\Field;
6
use Formularium\HTMLElement;
7
use PHP_CodeSniffer\Generators\HTML;
8
9
class Renderable_pagination extends Renderable_constant
10
{
11
    const BASE_URL = 'BASE_URL';
12
    const CURRENT = 'CURRENT';
13
    const INITIAL = 'INITIAL';
14
    const PAGES_AROUND = 'PAGES_AROUND';
15
    const PER_PAGE = 'PER_PAGE';
16
    const TOTAL_ITEMS = 'TOTAL_ITEMS';
17
18
    public function viewable($value, Field $field, HTMLElement $previous): HTMLElement
19
    {
20
        return $this->container($this->pagination($value, $field, $previous), $field);
21
    }
22
    
23
    public function editable($value, Field $field, HTMLElement $previous): HTMLElement
24
    {
25
        return $this->container($this->pagination($value, $field, $previous), $field);
26
    }
27
28
    protected function pagination($value, Field $field, HTMLElement $previous): HTMLElement
29
    {
30
        $pagesaround = intval($field->getExtension(self::PAGES_AROUND, 5));
31
        $perpage = intval($field->getExtension(self::PER_PAGE, 20));
32
        $baseurl = $field->getExtension(self::BASE_URL, '?');
33
        $numitems = $field->getExtension(self::TOTAL_ITEMS, 0);
34
        $currentitem = intval($field->getExtension(self::CURRENT, 1));
35
    
36
        // $firstindex => first id, same as 'begin'
37
        $firstindex = $currentitem - $pagesaround * $perpage;
38
        if ($firstindex < 0) {
39
            $firstindex = 0;
40
        }
41
    
42
        $maxindex = $currentitem + $pagesaround * $perpage;
43
        if ($maxindex > $numitems) {
44
            $maxindex = $numitems;
45
        }
46
    
47
        // only one page? don't show anything.
48
        if ($maxindex <= $perpage) {
49
            return HTMLElement::factory('');
50
        }
51
    
52
        $query = array();
53
        $parsed = parse_url($baseurl);
54
        if (isset($parsed['query'])) {
55
            mb_parse_str($parsed['query'], $query);
56
        }
57
58
        if ($firstindex > 0) {
59
            $pages[] = $this->getItem('...', '', 'formularium-disabled');
0 ignored issues
show
Comprehensibility Best Practice introduced by
$pages was never initialized. Although not strictly required by PHP, it is generally a good practice to add $pages = array(); before regardless.
Loading history...
60
        }
61
    
62
        for ($i = $firstindex; $i < $maxindex; $i += $perpage) {
63
            $currentpage = $i / $perpage + 1;
64
            if ($i == $currentitem) {
65
                $pages[] = $this->getItem((string)$currentpage, '', 'formularium-pagination-current');
66
            } else {
67
                $parsed['query'] = array_merge($query, array('begin' => $i, 'total' => $perpage));
68
                $baseurl = $this->glue_url($parsed);
69
                $pages[] = $this->getItem((string)$currentpage, $baseurl);
70
            }
71
        }
72
    
73
        if ($i < $numitems) {
74
            // disabled
75
            $pages[] = $this->getItem('...', '', 'formularium-disabled');
76
        }
77
    
78
        return HTMLElement::factory(
79
            'nav',
80
            ['class' => 'formularium-pagination-wrapper', 'aria-label' => "Page navigation"],
81
            HTMLElement::factory(
82
                'ul',
83
                ['class' => 'formularium-pagination'],
84
                $pages
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $pages does not seem to be defined for all execution paths leading up to this point.
Loading history...
85
            )
86
        );
87
    }
88
89
    protected function getItem(string $text, string $link, string $class = ''): HTMLElement
90
    {
91
        return HTMLElement::factory(
92
            'li',
93
            ['class' => ['formularium-pagination-item', $class]],
94
            HTMLElement::factory(
95
                'a',
96
                ['class' => 'formularium-pagination-link', 'href' => $link],
97
                $text
98
            )
99
        );
100
    }
101
102
103
    /**
104
     * Inverse of parse_url.
105
     * @param array parsed An array of fields, same ones as returned by parse_url.
0 ignored issues
show
Bug introduced by
The type Formularium\Frontend\HTML\Renderable\parsed was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
106
     * In addition, the 'query' field may be an associative array as well.
107
     * @return string The URL.
108
     */
109
    protected function glue_url($parsed)
110
    {
111
        $uri = '';
112
        if (isset($parsed['scheme'])) {
113
            $uri = $parsed['scheme'] ?
114
            $parsed['scheme'] . ':' . ((mb_strtolower($parsed['scheme']) == 'mailto') ? '' : '//') : '';
115
        }
116
        if (isset($parsed['user'])) {
117
            $uri .= $parsed['user'] ? $parsed['user'] . ($parsed['pass'] ? ':' . $parsed['pass'] : '') . '@' : '';
118
        }
119
        if (isset($parsed['host'])) {
120
            $uri .= $parsed['host'] ? $parsed['host'] : '';
121
        }
122
        if (isset($parsed['port'])) {
123
            $uri .= $parsed['port'] ? ':' . $parsed['port'] : '';
124
        }
125
        if (isset($parsed['path'])) {
126
            $uri .= $parsed['path'] ? $parsed['path'] : '';
127
        }
128
        if (isset($parsed['query'])) {
129
            if (is_array($parsed['query'])) {
130
                $uri .= $parsed['query'] ? '?' . http_build_query($parsed['query']) : '';
131
            } elseif (is_string($parsed['query'])) {
132
                $uri .= $parsed['query'] ? '?' . $parsed['query'] : '';
133
            }
134
        }
135
        if (isset($parsed['fragment'])) {
136
            $uri .= $parsed['fragment'] ? '#' . $parsed['fragment'] : '';
137
        }
138
        return $uri;
139
    }
140
}
141