1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the O2System Framework package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
* |
8
|
|
|
* @author Steeve Andrian Salim |
9
|
|
|
* @copyright Copyright (c) Steeve Andrian Salim |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
// ------------------------------------------------------------------------ |
13
|
|
|
|
14
|
|
|
namespace O2System\Html; |
15
|
|
|
|
16
|
|
|
// ------------------------------------------------------------------------ |
17
|
|
|
|
18
|
|
|
use O2System\Spl\Iterators\ArrayIterator; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class Element |
22
|
|
|
* |
23
|
|
|
* @package O2System\Html |
24
|
|
|
*/ |
25
|
|
|
class Element |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* Element::$tagName |
29
|
|
|
* |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
public $tagName; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Element::$entity |
36
|
|
|
* |
37
|
|
|
* @var \O2System\Html\Element\Entity |
38
|
|
|
*/ |
39
|
|
|
public $entity; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Element::$attributes |
43
|
|
|
* |
44
|
|
|
* @var \O2System\Html\Element\Attribute |
45
|
|
|
*/ |
46
|
|
|
public $attributes; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Element::$textContent |
50
|
|
|
* |
51
|
|
|
* @var \O2System\Html\Element\TextContent |
52
|
|
|
*/ |
53
|
|
|
public $textContent; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Element::$childNodes |
57
|
|
|
* |
58
|
|
|
* @var \O2System\Html\Element\Nodes |
59
|
|
|
*/ |
60
|
|
|
public $childNodes; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Element::$metadata |
64
|
|
|
* |
65
|
|
|
* @var \O2System\Html\Element\Metadata |
66
|
|
|
*/ |
67
|
|
|
public $metadata; |
68
|
|
|
|
69
|
|
|
// ------------------------------------------------------------------------ |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Element::__construct |
73
|
|
|
* |
74
|
|
|
* @param string $tagName |
75
|
|
|
* @param string|null $entityName |
76
|
|
|
*/ |
77
|
|
|
public function __construct($tagName, $entityName = null) |
78
|
|
|
{ |
79
|
|
|
$this->tagName = trim($tagName); |
80
|
|
|
|
81
|
|
|
$this->entity = new Element\Entity(); |
82
|
|
|
$this->entity->setEntityName($entityName); |
83
|
|
|
|
84
|
|
|
$this->attributes = new Element\Attributes(); |
|
|
|
|
85
|
|
|
$this->textContent = new Element\TextContent(); |
86
|
|
|
$this->childNodes = new Element\Nodes(); |
87
|
|
|
$this->metadata = new Element\Metadata(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
// ------------------------------------------------------------------------ |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Element::__clone |
94
|
|
|
* |
95
|
|
|
* @return \O2System\Html\Element |
96
|
|
|
* @throws \ReflectionException |
97
|
|
|
*/ |
98
|
|
|
public function __clone() |
99
|
|
|
{ |
100
|
|
|
$newElement = $this; |
101
|
|
|
$reflection = new \ReflectionClass($this); |
102
|
|
|
|
103
|
|
|
foreach ($reflection->getProperties(\ReflectionProperty::IS_PUBLIC) as $property) { |
104
|
|
|
$value = $property->getValue($newElement); |
105
|
|
|
|
106
|
|
|
if (is_object($value)) { |
107
|
|
|
if ($value instanceof ArrayIterator) { |
108
|
|
|
$value = new ArrayIterator($value->getArrayCopy()); |
109
|
|
|
$property->setValue($newElement, $value); |
110
|
|
|
} else { |
111
|
|
|
$property->setValue($newElement, clone $value); |
112
|
|
|
} |
113
|
|
|
} else { |
114
|
|
|
$property->setValue($newElement, $value); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return $newElement; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
// ------------------------------------------------------------------------ |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Element::__toString |
125
|
|
|
* |
126
|
|
|
* @return string |
127
|
|
|
*/ |
128
|
|
|
public function __toString() |
129
|
|
|
{ |
130
|
|
|
return $this->render(); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
// ------------------------------------------------------------------------ |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Element::render |
137
|
|
|
* |
138
|
|
|
* @return string |
139
|
|
|
*/ |
140
|
|
|
public function render() |
141
|
|
|
{ |
142
|
|
|
$selfClosingTags = [ |
143
|
|
|
'area', |
144
|
|
|
'base', |
145
|
|
|
'br', |
146
|
|
|
'col', |
147
|
|
|
'command', |
148
|
|
|
'embed', |
149
|
|
|
'hr', |
150
|
|
|
'img', |
151
|
|
|
'input', |
152
|
|
|
'keygen', |
153
|
|
|
'link', |
154
|
|
|
'meta', |
155
|
|
|
'param', |
156
|
|
|
'source', |
157
|
|
|
'track', |
158
|
|
|
'wbr', |
159
|
|
|
]; |
160
|
|
|
|
161
|
|
|
if (in_array($this->tagName, $selfClosingTags)) { |
162
|
|
|
$attr = $this->attributes; |
163
|
|
|
unset($attr[ 'realpath' ]); |
164
|
|
|
|
165
|
|
|
if ($this->hasAttributes()) { |
166
|
|
|
return '<' . $this->tagName . ' ' . trim($this->attributes->__toString()) . '>'; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
return '<' . $this->tagName . '>'; |
170
|
|
|
} else { |
171
|
|
|
$output[] = $this->open(); |
|
|
|
|
172
|
|
|
|
173
|
|
|
if ($this->hasTextContent()) { |
174
|
|
|
$output[] = PHP_EOL . implode('', $this->textContent->getArrayCopy()) . PHP_EOL; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
if ($this->hasChildNodes()) { |
178
|
|
|
if ( ! $this->hasTextContent()) { |
179
|
|
|
$output[] = PHP_EOL; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
foreach ($this->childNodes as $childNode) { |
183
|
|
|
$output[] = $childNode . PHP_EOL; |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
$output[] = $this->close(); |
189
|
|
|
|
190
|
|
|
return implode('', $output); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
// ------------------------------------------------------------------------ |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Element::hasAttributes |
197
|
|
|
* |
198
|
|
|
* @return bool |
199
|
|
|
*/ |
200
|
|
|
public function hasAttributes() |
201
|
|
|
{ |
202
|
|
|
return (bool)($this->attributes->count() == 0 ? false : true); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
// ------------------------------------------------------------------------ |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Element::open |
209
|
|
|
* |
210
|
|
|
* @return string |
211
|
|
|
*/ |
212
|
|
|
public function open() |
213
|
|
|
{ |
214
|
|
|
$attr = $this->attributes; |
215
|
|
|
unset($attr[ 'realpath' ]); |
216
|
|
|
|
217
|
|
|
if ($this->hasAttributes()) { |
218
|
|
|
return '<' . $this->tagName . ' ' . trim($this->attributes->__toString()) . '>'; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
return '<' . $this->tagName . '>'; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
// ------------------------------------------------------------------------ |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Element::hasTextContent |
228
|
|
|
* |
229
|
|
|
* @return bool |
230
|
|
|
*/ |
231
|
|
|
public function hasTextContent() |
232
|
|
|
{ |
233
|
|
|
return (bool)($this->textContent->count() == 0 ? false : true); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
// ------------------------------------------------------------------------ |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Element::hasChildNodes |
240
|
|
|
* |
241
|
|
|
* @return bool |
242
|
|
|
*/ |
243
|
|
|
public function hasChildNodes() |
244
|
|
|
{ |
245
|
|
|
return (bool)($this->childNodes->count() == 0 ? false : true); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
// ------------------------------------------------------------------------ |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Element::close |
252
|
|
|
* |
253
|
|
|
* @return string |
254
|
|
|
*/ |
255
|
|
|
public function close() |
256
|
|
|
{ |
257
|
|
|
return '</' . $this->tagName . '>'; |
258
|
|
|
} |
259
|
|
|
} |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..