1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Phalcon Framework. |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE.md |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Phalcon\Html\Helper; |
13
|
|
|
|
14
|
|
|
use Phalcon\Html\Escaper; |
15
|
|
|
use Phalcon\Html\Exception; |
16
|
|
|
|
17
|
|
|
use function call_user_func_array; |
18
|
|
|
use function str_repeat; |
19
|
|
|
|
20
|
|
|
use const PHP_EOL; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class AbstractHelper |
24
|
|
|
* |
25
|
|
|
* @property string $delimiter |
26
|
|
|
* @property Escaper $escaper |
27
|
|
|
* @property string $indent |
28
|
|
|
* @property int $indentLevel |
29
|
|
|
*/ |
30
|
|
|
abstract class AbstractHelper |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
protected $delimiter = PHP_EOL; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var Escaper |
39
|
|
|
*/ |
40
|
|
|
protected $escaper; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
protected $indent = " "; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var int |
49
|
|
|
*/ |
50
|
|
|
protected $indentLevel = 1; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* AbstractHelper constructor. |
54
|
|
|
* |
55
|
|
|
* @param Escaper $escaper |
56
|
|
|
*/ |
57
|
27 |
|
public function __construct(Escaper $escaper) |
58
|
|
|
{ |
59
|
27 |
|
$this->escaper = $escaper; |
60
|
27 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Produces a closing tag |
64
|
|
|
* |
65
|
|
|
* @param string $tag |
66
|
|
|
* @param bool $raw |
67
|
|
|
* |
68
|
|
|
* @return string |
69
|
|
|
*/ |
70
|
11 |
|
protected function close(string $tag, bool $raw = false): string |
71
|
|
|
{ |
72
|
11 |
|
$tag = $raw ? $tag : $this->escaper->html($tag); |
73
|
|
|
|
74
|
11 |
|
return "</" . $tag . ">"; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Replicates the indent x times as per indentLevel |
79
|
|
|
* |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
7 |
|
protected function indent(): string |
83
|
|
|
{ |
84
|
7 |
|
return str_repeat($this->indent, $this->indentLevel); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Keeps all the attributes sorted - same order all the tome |
89
|
|
|
* |
90
|
|
|
* @param array $overrides |
91
|
|
|
* @param array $attributes |
92
|
|
|
* |
93
|
|
|
* @return array |
94
|
|
|
*/ |
95
|
20 |
|
protected function orderAttributes(array $overrides, array $attributes): array |
96
|
|
|
{ |
97
|
|
|
$order = [ |
98
|
20 |
|
"rel" => null, |
99
|
|
|
"type" => null, |
100
|
|
|
"for" => null, |
101
|
|
|
"src" => null, |
102
|
|
|
"href" => null, |
103
|
|
|
"action" => null, |
104
|
|
|
"id" => null, |
105
|
|
|
"name" => null, |
106
|
|
|
"value" => null, |
107
|
|
|
"class" => null, |
108
|
|
|
]; |
109
|
|
|
|
110
|
20 |
|
$intersect = array_intersect_key($order, $attributes); |
111
|
20 |
|
$results = array_merge($intersect, $attributes); |
112
|
20 |
|
$results = array_merge($overrides, $results); |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Just in case remove the "escape" attribute |
116
|
|
|
*/ |
117
|
20 |
|
unset($results["escape"]); |
118
|
|
|
|
119
|
20 |
|
return $results; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Traverses an array and calls the method defined in the first element |
124
|
|
|
* with attributes as the second, returning the resulting string |
125
|
|
|
* |
126
|
|
|
* @param array $elements |
127
|
|
|
* @param string $delimiter |
128
|
|
|
* |
129
|
|
|
* @return string |
130
|
|
|
*/ |
131
|
11 |
|
protected function renderArrayElements( |
132
|
|
|
array $elements, |
133
|
|
|
string $delimiter |
134
|
|
|
): string { |
135
|
11 |
|
$result = ""; |
136
|
11 |
|
foreach ($elements as $item) { |
137
|
7 |
|
$result .= $item[2] |
138
|
7 |
|
. call_user_func_array([$this, $item[0]], $item[1]) |
139
|
7 |
|
. $delimiter; |
140
|
|
|
} |
141
|
|
|
|
142
|
11 |
|
return $result; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Renders all the attributes |
147
|
|
|
* |
148
|
|
|
* @param array $attributes |
149
|
|
|
* |
150
|
|
|
* @return string |
151
|
|
|
*/ |
152
|
20 |
|
protected function renderAttributes(array $attributes): string |
153
|
|
|
{ |
154
|
20 |
|
$result = ""; |
155
|
20 |
|
foreach ($attributes as $key => $value) { |
156
|
20 |
|
if (is_string($key) && null !== $value) { |
157
|
20 |
|
$result .= $key . "=\"" . $this->escaper->attributes($value) . "\" "; |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
161
|
20 |
|
return $result; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Renders an element |
166
|
|
|
* |
167
|
|
|
* @param string $tag |
168
|
|
|
* @param array $attributes |
169
|
|
|
* |
170
|
|
|
* @return string |
171
|
|
|
* @throws Exception |
172
|
|
|
*/ |
173
|
14 |
|
protected function renderElement(string $tag, array $attributes = []): string |
174
|
|
|
{ |
175
|
14 |
|
return $this->renderTag($tag, $attributes); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Renders an element |
180
|
|
|
* |
181
|
|
|
* @param string $tag |
182
|
|
|
* @param string $text |
183
|
|
|
* @param array $attributes |
184
|
|
|
* @param bool $raw |
185
|
|
|
* |
186
|
|
|
* @return string |
187
|
|
|
* @throws Exception |
188
|
|
|
*/ |
189
|
10 |
|
protected function renderFullElement( |
190
|
|
|
string $tag, |
191
|
|
|
string $text, |
192
|
|
|
array $attributes = [], |
193
|
|
|
bool $raw = false |
194
|
|
|
): string { |
195
|
10 |
|
$content = $raw ? $text : $this->escaper->html($text); |
196
|
|
|
|
197
|
10 |
|
return $this->renderElement($tag, $attributes) . $content . $this->close($tag, $raw); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Renders a tag |
202
|
|
|
* |
203
|
|
|
* @param string $tag |
204
|
|
|
* @param array $attributes |
205
|
|
|
* @param string $close |
206
|
|
|
* |
207
|
|
|
* @return string |
208
|
|
|
*/ |
209
|
21 |
|
protected function renderTag(string $tag, array $attributes = [], string $close = ""): string |
210
|
|
|
{ |
211
|
21 |
|
$escapedAttrs = ""; |
212
|
21 |
|
if (count($attributes) > 0) { |
213
|
20 |
|
$attrs = $this->orderAttributes([], $attributes); |
214
|
20 |
|
$escapedAttrs = " " . rtrim($this->renderAttributes($attrs)); |
215
|
|
|
} |
216
|
|
|
|
217
|
21 |
|
$close = empty(trim($close)) ? '' : " " . trim($close); |
218
|
|
|
|
219
|
21 |
|
return "<" . $tag . $escapedAttrs . $close . ">"; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Produces a self close tag i.e. <img /> |
224
|
|
|
* |
225
|
|
|
* @param string $tag |
226
|
|
|
* @param array $attributes |
227
|
|
|
* |
228
|
|
|
* @return string |
229
|
|
|
*/ |
230
|
1 |
|
protected function selfClose(string $tag, array $attributes = []): string |
231
|
|
|
{ |
232
|
1 |
|
return $this->renderTag($tag, $attributes, "/"); |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
|