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
|
|
|
public function setClasses(array $classes) |
72
|
|
|
{ |
73
|
|
|
$this->classes = array_merge([ |
74
|
|
|
'ul' => '', |
75
|
|
|
'li_current' => '', |
76
|
|
|
'a' => '', |
77
|
|
|
'a_current' => '', |
78
|
|
|
], $classes); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function getClasses() |
82
|
|
|
{ |
83
|
|
|
return $this->classes; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function setNesting($nesting) |
87
|
|
|
{ |
88
|
|
|
$this->nesting = $nesting; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function getNesting() |
92
|
|
|
{ |
93
|
|
|
return $this->nesting; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Put value is not empty. |
98
|
|
|
* |
99
|
|
|
* @param string $attribute |
100
|
|
|
* @param string|array $value |
101
|
|
|
* |
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
|
View Code Duplication |
private function isAttribute($attribute, $value) |
|
|
|
|
105
|
|
|
{ |
106
|
|
|
if (is_array($value)) { |
107
|
|
|
array_filter($value); |
108
|
|
|
$value = trim(implode(' ', $value)); |
109
|
|
|
|
110
|
|
|
return !empty($value) ? ' '.$attribute.'="'.$value.'"' : ''; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return (isset($value) and !empty($value)) ? ' '.$attribute.'="'.trim($value).'"' : ''; |
|
|
|
|
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function create() |
117
|
|
|
{ |
118
|
|
|
$nav = $this->getInfo(); |
119
|
|
|
$path = $this->getPath(); |
120
|
|
|
$classes = $this->getClasses(); |
121
|
|
|
$nesting = $this->getNesting(); |
122
|
|
|
|
123
|
|
|
$html[] = sprintf('<ul%1$s>', $this->isAttribute('class', $classes['ul'])); |
|
|
|
|
124
|
|
|
|
125
|
|
|
$nest = str_repeat("\t", $nesting); |
126
|
|
|
$tab = str_repeat("\t", 1 + $nesting); |
127
|
|
|
|
128
|
|
|
if ($nav['page'] > 1) { |
129
|
|
|
$html[] = sprintf( |
130
|
|
|
'%1$s<li%2$s><a%3$s href="%4$s">«</a></li>', |
131
|
|
|
$tab, |
132
|
|
|
$this->isAttribute('class', $classes['li']), |
133
|
|
|
$this->isAttribute('class', $classes['a']), |
134
|
|
|
$path.'/page/'.$nav['prev'] |
135
|
|
|
); |
136
|
|
|
} |
137
|
|
View Code Duplication |
if ($nav['forstart'] > 1) { |
|
|
|
|
138
|
|
|
$html[] = sprintf( |
139
|
|
|
'%1$s<li%2$s><a%3$s href="%4$s">1</a></li>', |
140
|
|
|
$tab, |
141
|
|
|
$this->isAttribute('class', $classes['li']), |
142
|
|
|
$this->isAttribute('class', $classes['a']), |
143
|
|
|
$path.'/page/1' |
144
|
|
|
); |
145
|
|
|
} |
146
|
|
View Code Duplication |
if ($nav['forstart'] > 2) { |
|
|
|
|
147
|
|
|
$html[] = sprintf( |
148
|
|
|
'%1$s<li%2$s><a%3$s>...</a></li>', |
149
|
|
|
$tab, |
150
|
|
|
$this->isAttribute('class', $classes['li']), |
151
|
|
|
$this->isAttribute('class', $classes['a']) |
152
|
|
|
); |
153
|
|
|
} |
154
|
|
|
for ($nav['forstart']; $nav['forstart'] < $nav['forend']; ++$nav['forstart']) { |
155
|
|
|
if ((int) $nav['forstart'] === $nav['page']) { |
156
|
|
|
$html[] = sprintf( |
157
|
|
|
'%1$s<li%2$s><a%3$s href="%4$s">%5$s</a></li>', |
158
|
|
|
$tab, |
159
|
|
|
$this->isAttribute('class', [$classes['li'], $classes['li_current']]), |
160
|
|
|
$this->isAttribute('class', [$classes['a'], $classes['a_current']]), |
161
|
|
|
$path.'/page/'.$nav['forstart'], |
162
|
|
|
$nav['forstart'] |
163
|
|
|
); |
164
|
|
|
} |
165
|
|
View Code Duplication |
if ((int) $nav['forstart'] !== $nav['page']) { |
|
|
|
|
166
|
|
|
$html[] = sprintf( |
167
|
|
|
'%1$s<li%2$s><a%3$s href="%4$s">%5$s</a></li>', |
168
|
|
|
$tab, |
169
|
|
|
$this->isAttribute('class', $classes['li']), |
170
|
|
|
$this->isAttribute('class', $classes['a']), |
171
|
|
|
$path.'/page/'.$nav['forstart'], |
172
|
|
|
$nav['forstart'] |
173
|
|
|
); |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
View Code Duplication |
if ($nav['forstart'] < $nav['allpages']) { |
|
|
|
|
177
|
|
|
$html[] = sprintf( |
178
|
|
|
'%1$s<li%2$s><a%3$s>...</a></li>', |
179
|
|
|
$tab, |
180
|
|
|
$this->isAttribute('class', $classes['li']), |
181
|
|
|
$this->isAttribute('class', $classes['a']) |
182
|
|
|
); |
183
|
|
|
} |
184
|
|
View Code Duplication |
if ($nav['forstart'] - 1 < $nav['allpages']) { |
|
|
|
|
185
|
|
|
$html[] = sprintf( |
186
|
|
|
'%1$s<li%2$s><a%3$s href="%4$s">%5$s</a></li>', |
187
|
|
|
$tab, |
188
|
|
|
$this->isAttribute('class', $classes['li']), |
189
|
|
|
$this->isAttribute('class', $classes['a']), |
190
|
|
|
$path.'/page/'.$nav['allpages'], |
191
|
|
|
$nav['allpages'] |
192
|
|
|
); |
193
|
|
|
} |
194
|
|
View Code Duplication |
if ($nav['page'] < $nav['allpages']) { |
|
|
|
|
195
|
|
|
$html[] = sprintf( |
196
|
|
|
'%1$s<li%2$s><a%3$s href="%4$s">»</a></li>', |
197
|
|
|
$tab, |
198
|
|
|
$this->isAttribute('class', $classes['li']), |
199
|
|
|
$this->isAttribute('class', $classes['a']), |
200
|
|
|
$path.'/page/'.$nav['next'] |
201
|
|
|
); |
202
|
|
|
} |
203
|
|
|
$html[] = $nest.'</ul>'."\n"; |
204
|
|
|
|
205
|
|
|
return implode("\n", $html); |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|
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.