Paging::isAttribute()   A
last analyzed

Complexity

Conditions 5
Paths 6

Size

Total Lines 11

Duplication

Lines 11
Ratio 100 %

Importance

Changes 0
Metric Value
cc 5
nc 6
nop 2
dl 11
loc 11
rs 9.6111
c 0
b 0
f 0
1
<?php
2
3
namespace Rudolf\Component\Html;
4
5
class Paging
6
{
7
    /**
8
     * @var array
9
     */
10
    private $info;
11
12
    /**
13
     * @var string
14
     */
15
    private $path;
16
17
    /**
18
     * @var array
19
     */
20
    private $classes;
21
22
    /**
23
     * @var int
24
     */
25
    private $nesting;
26
27
    /**
28
     * Constructor.
29
     *
30
     * @param array  $info     with data for loop
31
     *                        <page> - current page
32
     *                        <forstart> -
33
     *                        <forend> -
34
     *                        <allpages> - all pages
35
     *                        <prev> - prev page
36
     *                        <next> - next page
37
     * @param string $path    path with a slash at the beginning and at the end without him, like: '/kg'
38
     * @param array  $classes Specifies a pagination appearance
39
     *                        <ul> - main ul class
40
     *                        <current> - current active li class
41
     * @param int    $nesting
42
     */
43
    public function __construct(array $info = [], $path = '', array $classes = [], $nesting = 0)
44
    {
45
        $this->setInfo($info);
46
        $this->setPath($path);
47
        $this->setClasses($classes);
48
        $this->setNesting($nesting);
49
    }
50
51
    public function setInfo(array $info)
52
    {
53
        $this->info = $info;
54
    }
55
56
    public function getInfo()
57
    {
58
        return $this->info;
59
    }
60
61
    public function setPath($path)
62
    {
63
        $this->path = $path;
64
    }
65
66
    public function getPath()
67
    {
68
        return DIR.$this->path;
69
    }
70
71 View Code Duplication
    public function setClasses(array $classes)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
72
    {
73
        $this->classes = array_merge([
74
            'ul' => '',
75
            'li' => '',
76
            'li_current' => '',
77
            'a' => '',
78
            'a_current' => '',
79
        ], $classes);
80
    }
81
82
    public function getClasses()
83
    {
84
        return $this->classes;
85
    }
86
87
    public function setNesting($nesting)
88
    {
89
        $this->nesting = $nesting;
90
    }
91
92
    public function getNesting()
93
    {
94
        return $this->nesting;
95
    }
96
97
    /**
98
     * Put value is not empty.
99
     *
100
     * @param string       $attribute
101
     * @param string|array $value
102
     *
103
     * @return string
104
     */
105 View Code Duplication
    private function isAttribute($attribute, $value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
106
    {
107
        if (is_array($value)) {
108
            array_filter($value);
109
            $value = trim(implode(' ', $value));
110
111
            return !empty($value) ? ' '.$attribute.'="'.$value.'"' : '';
112
        }
113
114
        return (isset($value) and !empty($value)) ? ' '.$attribute.'="'.trim($value).'"' : '';
115
    }
116
117
    public function create()
118
    {
119
        $nav = $this->getInfo();
120
        $path = $this->getPath();
121
        $classes = $this->getClasses();
122
        $nesting = $this->getNesting();
123
124
        $html[] = sprintf('<ul%1$s>', $this->isAttribute('class', $classes['ul']));
0 ignored issues
show
Coding Style Comprehensibility introduced by
$html was never initialized. Although not strictly required by PHP, it is generally a good practice to add $html = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
125
126
        $nest = str_repeat("\t", $nesting);
127
        $tab = str_repeat("\t", 1 + $nesting);
128
129
        if ($nav['page'] > 1) {
130
            $html[] = sprintf(
131
                '%1$s<li%2$s><a%3$s href="%4$s">«</a></li>',
132
                $tab,
133
                $this->isAttribute('class', $classes['li']),
134
                $this->isAttribute('class', $classes['a']),
135
                $path.'/page/'.$nav['prev']
136
            );
137
        }
138 View Code Duplication
        if ($nav['forstart'] > 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
139
            $html[] = sprintf(
140
                '%1$s<li%2$s><a%3$s href="%4$s">1</a></li>',
141
                $tab,
142
                $this->isAttribute('class', $classes['li']),
143
                $this->isAttribute('class', $classes['a']),
144
                $path.'/page/1'
145
            );
146
        }
147 View Code Duplication
        if ($nav['forstart'] > 2) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
148
            $html[] = sprintf(
149
                '%1$s<li%2$s><a%3$s>...</a></li>',
150
                $tab,
151
                $this->isAttribute('class', $classes['li']),
152
                $this->isAttribute('class', $classes['a'])
153
            );
154
        }
155
        for ($nav['forstart']; $nav['forstart'] < $nav['forend']; ++$nav['forstart']) {
156
            if ((int) $nav['forstart'] === $nav['page']) {
157
                $html[] = sprintf(
158
                    '%1$s<li%2$s><a%3$s href="%4$s">%5$s</a></li>',
159
                    $tab,
160
                    $this->isAttribute('class', [$classes['li'], $classes['li_current']]),
161
                    $this->isAttribute('class', [$classes['a'], $classes['a_current']]),
162
                    $path.'/page/'.$nav['forstart'],
163
                    $nav['forstart']
164
                );
165
            }
166 View Code Duplication
            if ((int) $nav['forstart'] !== $nav['page']) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
167
                $html[] = sprintf(
168
                    '%1$s<li%2$s><a%3$s href="%4$s">%5$s</a></li>',
169
                    $tab,
170
                    $this->isAttribute('class', $classes['li']),
171
                    $this->isAttribute('class', $classes['a']),
172
                    $path.'/page/'.$nav['forstart'],
173
                    $nav['forstart']
174
                );
175
            }
176
        }
177 View Code Duplication
        if ($nav['forstart'] < $nav['allpages']) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
178
            $html[] = sprintf(
179
                '%1$s<li%2$s><a%3$s>...</a></li>',
180
                $tab,
181
                $this->isAttribute('class', $classes['li']),
182
                $this->isAttribute('class', $classes['a'])
183
            );
184
        }
185 View Code Duplication
        if ($nav['forstart'] - 1 < $nav['allpages']) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
186
            $html[] = sprintf(
187
                '%1$s<li%2$s><a%3$s href="%4$s">%5$s</a></li>',
188
                $tab,
189
                $this->isAttribute('class', $classes['li']),
190
                $this->isAttribute('class', $classes['a']),
191
                $path.'/page/'.$nav['allpages'],
192
                $nav['allpages']
193
            );
194
        }
195 View Code Duplication
        if ($nav['page'] < $nav['allpages']) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
196
            $html[] = sprintf(
197
                '%1$s<li%2$s><a%3$s href="%4$s">»</a></li>',
198
                $tab,
199
                $this->isAttribute('class', $classes['li']),
200
                $this->isAttribute('class', $classes['a']),
201
                $path.'/page/'.$nav['next']
202
            );
203
        }
204
        $html[] = $nest.'</ul>'."\n";
205
206
        return implode("\n", $html);
207
    }
208
}
209