Total Complexity | 17 |
Total Lines | 64 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
9 | class HtmlHelper |
||
10 | { |
||
11 | const DATA_ATTRIBUTES = ['data', 'data-ng', 'ng']; |
||
12 | |||
13 | public static function buildHtmlAttributes(array $htmlAttributes, string $charset = 'UTF-8'): string |
||
14 | { |
||
15 | $html = ''; |
||
16 | |||
17 | foreach ($htmlAttributes as $name => $value) { |
||
18 | if (is_bool($value)) { |
||
19 | if ($value) { |
||
20 | $html .= " $name"; |
||
21 | } |
||
22 | continue; |
||
23 | } |
||
24 | |||
25 | if (is_array($value)) { |
||
26 | if (in_array($name, self::DATA_ATTRIBUTES)) { |
||
27 | foreach ($value as $n => $v) { |
||
28 | if (is_string($v)) { |
||
29 | $html .= " $name-$n=\"" . self::encode($v, $charset) . '"'; |
||
30 | } |
||
31 | } |
||
32 | continue; |
||
33 | } |
||
34 | |||
35 | if ($name === 'class') { |
||
36 | if (empty($value)) { |
||
37 | continue; |
||
38 | } |
||
39 | $html .= " $name=\"" . self::encode(implode(' ', $value), $charset) . '"'; |
||
40 | continue; |
||
41 | } |
||
42 | |||
43 | if ($name === 'style') { |
||
44 | if (empty($value)) { |
||
45 | continue; |
||
46 | } |
||
47 | $html .= " $name=\"" . self::encode(self::cssStyleFromArray($value), $charset) . '"'; |
||
48 | } |
||
49 | continue; |
||
50 | } |
||
51 | |||
52 | if ($value !== null) { |
||
53 | $html .= " $name=\"" . self::encode($value, $charset) . '"'; |
||
54 | } |
||
55 | } |
||
56 | |||
57 | return $html; |
||
58 | } |
||
59 | |||
60 | public static function encode(string $content, string $charset = 'UTF-8', bool $doubleEncode = true): string |
||
63 | } |
||
64 | |||
65 | public static function cssStyleFromArray(array $style): ?string |
||
75 |