1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WebTheory\Html; |
4
|
|
|
|
5
|
|
|
use JsonSerializable; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @deprecated version 0.2.0 |
9
|
|
|
*/ |
10
|
|
|
class HtmlMap implements JsonSerializable |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* |
14
|
|
|
*/ |
15
|
|
|
public $map; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* |
19
|
|
|
*/ |
20
|
|
|
public function __construct(array $map) |
21
|
|
|
{ |
22
|
|
|
$this->map = $map; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Generates an html string from array of element definitions |
27
|
|
|
* |
28
|
|
|
* 'tag' => string |
29
|
|
|
* 'attributes' => array || string |
30
|
|
|
* 'content' => string |
31
|
|
|
* 'children' => array |
32
|
|
|
* |
33
|
|
|
* @param array $html_map |
34
|
|
|
* @param bool $recall |
35
|
|
|
* |
36
|
|
|
* @return string |
37
|
|
|
*/ |
38
|
|
|
protected function constructHtml($map = null, $recall = false) |
39
|
|
|
{ |
40
|
|
|
static $markedUp; |
41
|
|
|
|
42
|
|
|
$html = ''; |
43
|
|
|
|
44
|
|
|
if (!$recall) { |
45
|
|
|
$markedUp = []; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
foreach ($map ?? $this->map as $currentElement => $definition) { |
49
|
|
|
|
50
|
|
|
if (in_array($currentElement, $markedUp)) { |
51
|
|
|
continue; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
// add values already existing as strings to $html as they may already exist as markup |
55
|
|
|
if (is_object($definition) && method_exists($definition, '__toString') || is_string($definition)) { |
|
|
|
|
56
|
|
|
$html .= $definition; |
57
|
|
|
$markedUp[] = $currentElement; |
58
|
|
|
continue; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$html .= Html::open($definition['tag'], $definition['attributes'] ?? ''); |
|
|
|
|
62
|
|
|
$html .= $definition['content'] ?? ''; |
63
|
|
|
|
64
|
|
|
// store children in array to be passed as $html_map argument in recursive call |
65
|
|
|
if (!empty($children = $definition['children'] ?? null)) { |
66
|
|
|
foreach ($children as $child) { |
67
|
|
|
$childMap[$child] = $this->map[$child]; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$html .= $this->constructHtml($childMap, true); |
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$html .= Html::maybeClose($definition['tag']); |
74
|
|
|
$markedUp[] = $currentElement; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
// reset static variables if in initial call stack |
78
|
|
|
if (!$recall) { |
79
|
|
|
$markedUp = null; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $html; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* |
87
|
|
|
*/ |
88
|
|
|
public function toHtml() |
89
|
|
|
{ |
90
|
|
|
return $this->constructHtml(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* |
95
|
|
|
*/ |
96
|
|
|
public function toJson() |
97
|
|
|
{ |
98
|
|
|
return json_encode($this); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* |
103
|
|
|
*/ |
104
|
|
|
public function jsonSerialize() |
105
|
|
|
{ |
106
|
|
|
return $this->map; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* |
111
|
|
|
*/ |
112
|
|
|
public function __toString() |
113
|
|
|
{ |
114
|
|
|
return $this->toHtml(); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|