|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Extend \DomNode |
|
4
|
|
|
* |
|
5
|
|
|
* PHP version 5.4 |
|
6
|
|
|
* |
|
7
|
|
|
* @category GLICER |
|
8
|
|
|
* @package GlHtml |
|
9
|
|
|
* @author Emmanuel ROECKER |
|
10
|
|
|
* @author Rym BOUCHAGOUR |
|
11
|
|
|
* @copyright 2015 GLICER |
|
12
|
|
|
* @license MIT |
|
13
|
|
|
* @link http://dev.glicer.com/ |
|
14
|
|
|
* |
|
15
|
|
|
* Created : 19/02/15 |
|
16
|
|
|
* File : GlHtmlNode.php |
|
17
|
|
|
* |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
namespace GlHtml; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Class GlHtmlNode |
|
25
|
|
|
* @package GlHtml |
|
26
|
|
|
*/ |
|
27
|
|
|
class GlHtmlNode |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* @var \DOMNode |
|
31
|
|
|
*/ |
|
32
|
|
|
private $node; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param \DOMNode $node |
|
36
|
|
|
*/ |
|
37
|
|
|
public function __construct(\DOMNode $node) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->node = $node; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param array $attributes |
|
44
|
|
|
*/ |
|
45
|
|
|
public function setAttributes(array $attributes) |
|
46
|
|
|
{ |
|
47
|
|
|
foreach ($attributes as $name => $value) { |
|
48
|
|
|
$this->node->setAttribute($name, $value); |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param string $value |
|
54
|
|
|
*/ |
|
55
|
|
|
public function setValue($value) |
|
56
|
|
|
{ |
|
57
|
|
|
$this->node->nodeValue = $value; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param string $html |
|
62
|
|
|
*/ |
|
63
|
|
|
public |
|
64
|
|
|
function add( |
|
65
|
|
|
$html |
|
66
|
|
|
) { |
|
67
|
|
|
$frag = $this->node->ownerDocument->createDocumentFragment(); |
|
68
|
|
|
$frag->appendXML($html); |
|
69
|
|
|
$this->node->appendChild($frag); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @return GlHtmlNode |
|
74
|
|
|
*/ |
|
75
|
|
|
public function getParent() { |
|
76
|
|
|
return new GlHtmlNode($this->node->parentNode); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param string $html |
|
81
|
|
|
*/ |
|
82
|
|
|
public |
|
83
|
|
|
function replaceMe( |
|
84
|
|
|
$html |
|
85
|
|
|
) { |
|
86
|
|
|
$frag = $this->node->ownerDocument->createDocumentFragment(); |
|
87
|
|
|
$frag->appendXML($html); |
|
88
|
|
|
$this->node->parentNode->replaceChild($frag, $this->node); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @param string $html |
|
93
|
|
|
*/ |
|
94
|
|
|
public function replaceInner($html) |
|
95
|
|
|
{ |
|
96
|
|
|
$this->setValue(''); |
|
97
|
|
|
$this->add($html); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @return \DOMNode |
|
103
|
|
|
*/ |
|
104
|
|
|
public function getDOMNode() |
|
105
|
|
|
{ |
|
106
|
|
|
return $this->node; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @return string |
|
111
|
|
|
*/ |
|
112
|
|
|
public function getName() |
|
113
|
|
|
{ |
|
114
|
|
|
return strtolower($this->node->nodeName); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @param string $name |
|
119
|
|
|
* |
|
120
|
|
|
* @return string |
|
121
|
|
|
*/ |
|
122
|
|
|
public |
|
123
|
|
|
function getAttribute( |
|
124
|
|
|
$name |
|
125
|
|
|
) { |
|
126
|
|
|
return $this->node->getAttribute($name); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @return string |
|
131
|
|
|
*/ |
|
132
|
|
|
public |
|
133
|
|
|
function getText() |
|
134
|
|
|
{ |
|
135
|
|
|
return $this->node->nodeValue; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @return array |
|
141
|
|
|
*/ |
|
142
|
|
|
public function getSentences() |
|
143
|
|
|
{ |
|
144
|
|
|
$sentences = []; |
|
145
|
|
|
$sentence = ""; |
|
146
|
|
|
static::iterateSentencesOverNode($this->node, $sentence, $sentences); |
|
|
|
|
|
|
147
|
|
|
|
|
148
|
|
|
$sentence = trim($sentence); |
|
149
|
|
|
if (strlen($sentence) > 0) { |
|
150
|
|
|
$sentences[] = $sentence; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
return $sentences; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
public |
|
157
|
|
|
function delete() |
|
158
|
|
|
{ |
|
159
|
|
|
$this->node->parentNode->removeChild($this->node); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* @return string |
|
164
|
|
|
*/ |
|
165
|
|
|
public |
|
166
|
|
|
function getHtml() |
|
167
|
|
|
{ |
|
168
|
|
|
$innerHTML = ''; |
|
169
|
|
|
$children = $this->node->childNodes; |
|
170
|
|
|
foreach ($children as $child) { |
|
171
|
|
|
$innerHTML .= $child->ownerDocument->saveXML($child, LIBXML_NOEMPTYTAG); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
return $innerHTML; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* extract h tag from html |
|
179
|
|
|
* |
|
180
|
|
|
* @param \DOMNodeList $nodeList |
|
181
|
|
|
* @param callable $fct |
|
182
|
|
|
*/ |
|
183
|
|
|
private function recursiveCallChild(\DOMNodeList $nodeList, callable $fct) |
|
184
|
|
|
{ |
|
185
|
|
|
/** |
|
186
|
|
|
* @var \DOMNode $domNode |
|
187
|
|
|
*/ |
|
188
|
|
|
foreach ($nodeList as $domNode) { |
|
189
|
|
|
$fct(new GlHtmlNode($domNode)); |
|
190
|
|
|
if ($domNode->hasChildNodes()) { |
|
191
|
|
|
$this->recursiveCallChild($domNode->childNodes, $fct); |
|
192
|
|
|
} |
|
193
|
|
|
} |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* @param callable $fct |
|
198
|
|
|
*/ |
|
199
|
|
|
public function callChild(callable $fct) |
|
200
|
|
|
{ |
|
201
|
|
|
$this->recursiveCallChild($this->node->childNodes, $fct); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* @param \DOMNode $node |
|
206
|
|
|
* @param string $sentence |
|
207
|
|
|
* @param array $sentences |
|
208
|
|
|
* |
|
209
|
|
|
* @return int |
|
210
|
|
|
*/ |
|
211
|
|
|
private static function iterateSentencesOverNode(\DOMNode $node, &$sentence, array &$sentences) |
|
212
|
|
|
{ |
|
213
|
|
|
if ($node instanceof \DOMText) { |
|
214
|
|
|
$sentence .= preg_replace("/[\\t\\n\\f\\r ]+/im", " ", $node->wholeText); |
|
215
|
|
|
|
|
216
|
|
|
return 0; |
|
217
|
|
|
} |
|
218
|
|
|
if ($node instanceof \DOMDocumentType) { |
|
219
|
|
|
return 0; |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
$name = strtolower($node->nodeName); |
|
223
|
|
|
if (preg_match('/^h(\d+)$/', $name)) { |
|
224
|
|
|
$name = "hx"; |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
switch ($name) { |
|
228
|
|
|
case "hr": |
|
229
|
|
|
case "style": |
|
230
|
|
|
case "head": |
|
231
|
|
|
case "title": |
|
232
|
|
|
case "meta": |
|
233
|
|
|
case "script": |
|
234
|
|
|
case "pre": |
|
235
|
|
|
return 0; |
|
236
|
|
|
|
|
237
|
|
|
case "p": |
|
238
|
|
|
case "hx": |
|
239
|
|
|
case "th": |
|
240
|
|
|
case "td": |
|
241
|
|
|
case "li": |
|
242
|
|
|
case "label": |
|
243
|
|
View Code Duplication |
case "button": |
|
|
|
|
|
|
244
|
|
|
$sentence = trim($sentence); |
|
245
|
|
|
if (strlen($sentence) > 0) { |
|
246
|
|
|
$sentences[] = $sentence; |
|
247
|
|
|
} |
|
248
|
|
|
$sentence = ""; |
|
249
|
|
|
break; |
|
250
|
|
|
|
|
251
|
|
|
case "br": |
|
252
|
|
|
$sentence .= " "; |
|
253
|
|
|
break; |
|
254
|
|
|
default: |
|
255
|
|
|
break; |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
$childs = $node->childNodes; |
|
259
|
|
|
foreach ($childs as $child) { |
|
260
|
|
|
static::iterateSentencesOverNode($child, $sentence, $sentences); |
|
|
|
|
|
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
switch ($name) { |
|
264
|
|
|
case "style": |
|
265
|
|
|
case "head": |
|
266
|
|
|
case "title": |
|
267
|
|
|
case "meta": |
|
268
|
|
|
case "script": |
|
269
|
|
|
return ""; |
|
270
|
|
|
|
|
271
|
|
|
case "p": |
|
272
|
|
|
case "hx": |
|
273
|
|
|
case "th": |
|
274
|
|
|
case "td": |
|
275
|
|
|
case "li": |
|
276
|
|
|
case "label": |
|
277
|
|
View Code Duplication |
case "button": |
|
|
|
|
|
|
278
|
|
|
$sentence = trim($sentence); |
|
279
|
|
|
if (strlen($sentence) > 0) { |
|
280
|
|
|
$sentences[] = $sentence; |
|
281
|
|
|
} |
|
282
|
|
|
$sentence = ""; |
|
283
|
|
|
break; |
|
284
|
|
|
|
|
285
|
|
|
default: |
|
286
|
|
|
break; |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
|
|
return 0; |
|
290
|
|
|
} |
|
291
|
|
|
} |
|
292
|
|
|
|
Let’s assume you have a class which uses late-static binding:
}
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClassto useselfinstead: