1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bavix\AdvancedHtmlDom; |
4
|
|
|
|
5
|
|
|
class AHTMLNodeList implements \Iterator, \Countable, \ArrayAccess |
6
|
|
|
{ |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @var |
10
|
|
|
*/ |
11
|
|
|
private $nodeList; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @var |
15
|
|
|
*/ |
16
|
|
|
private $doc; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var int |
20
|
|
|
*/ |
21
|
|
|
private $counter = 0; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* AHTMLNodeList constructor. |
25
|
|
|
* |
26
|
|
|
* @param $nodeList |
27
|
|
|
* @param $doc |
28
|
|
|
*/ |
29
|
5 |
|
public function __construct($nodeList, $doc) |
30
|
|
|
{ |
31
|
5 |
|
$this->nodeList = $nodeList; |
32
|
5 |
|
$this->doc = $doc; |
33
|
5 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @see https://github.com/monkeysuffrage/advanced_html_dom/issues/19 |
37
|
|
|
*/ |
38
|
5 |
|
public function __destruct() |
39
|
|
|
{ |
40
|
5 |
|
$this->nodeList = $this->doc = null; |
41
|
5 |
|
unset($this->nodeList, $this->doc); |
42
|
5 |
|
Cleanup::all(); |
43
|
5 |
|
} |
44
|
|
|
|
45
|
|
|
/* |
46
|
|
|
abstract public boolean offsetExists ( mixed $offset ) |
47
|
|
|
abstract public mixed offsetGet ( mixed $offset ) |
48
|
|
|
abstract public void offsetSet ( mixed $offset , mixed $value ) |
49
|
|
|
abstract public void offsetUnset ( mixed $offset ) |
50
|
|
|
*/ |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param mixed $offset |
54
|
|
|
* |
55
|
|
|
* @return bool |
56
|
|
|
*/ |
57
|
|
|
public function offsetExists($offset) |
58
|
|
|
{ |
59
|
|
|
return 0 <= $offset && $offset < $this->nodeList->length; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param mixed $offset |
64
|
|
|
* |
65
|
|
|
* @return AHTMLNode |
66
|
|
|
*/ |
67
|
|
|
public function offsetGet($offset) |
68
|
|
|
{ |
69
|
|
|
return new AHTMLNode($this->nodeList->item($offset), $this->doc); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param mixed $offset |
74
|
|
|
* @param mixed $value |
75
|
|
|
*/ |
76
|
|
|
public function offsetSet($offset, $value) |
77
|
|
|
{ |
78
|
|
|
\trigger_error('offsetSet not implemented', E_USER_WARNING); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param mixed $offset |
83
|
|
|
*/ |
84
|
|
|
public function offsetUnset($offset) |
85
|
|
|
{ |
86
|
|
|
\trigger_error('offsetUnset not implemented', E_USER_WARNING); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return mixed |
91
|
|
|
*/ |
92
|
|
|
public function count() |
93
|
|
|
{ |
94
|
|
|
return $this->nodeList->length; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* |
99
|
|
|
*/ |
100
|
4 |
|
public function rewind() |
101
|
|
|
{ |
102
|
4 |
|
$this->counter = 0; |
103
|
4 |
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return AHTMLNode |
107
|
|
|
*/ |
108
|
4 |
|
public function current() |
109
|
|
|
{ |
110
|
4 |
|
return new AHTMLNode($this->nodeList->item($this->counter), $this->doc); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return int |
115
|
|
|
*/ |
116
|
|
|
public function key() |
117
|
|
|
{ |
118
|
|
|
return $this->counter; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* |
123
|
|
|
*/ |
124
|
4 |
|
public function next() |
125
|
|
|
{ |
126
|
4 |
|
$this->counter++; |
127
|
4 |
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return bool |
131
|
|
|
*/ |
132
|
4 |
|
public function valid() |
133
|
|
|
{ |
134
|
4 |
|
return $this->nodeList && $this->counter < $this->nodeList->length; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @return AHTMLNode|null |
139
|
|
|
*/ |
140
|
|
|
public function last() |
141
|
|
|
{ |
142
|
|
|
return ($this->nodeList->length > 0) ? new AHTMLNode($this->nodeList->item($this->nodeList->length - 1), $this->doc) : null; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @return $this |
147
|
|
|
*/ |
148
|
|
|
public function remove() |
149
|
|
|
{ |
150
|
|
|
foreach ($this as $node) { |
151
|
|
|
$node->remove(); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return $this; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @param $c |
159
|
|
|
* |
160
|
|
|
* @return array |
161
|
|
|
*/ |
162
|
|
|
public function map($c) |
163
|
|
|
{ |
164
|
|
|
$ret = array(); |
165
|
|
|
foreach ($this as $node) { |
166
|
|
|
$ret[] = $c($node); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
return $ret; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
|
173
|
|
|
//math methods |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @param $nl |
177
|
|
|
* |
178
|
|
|
* @return AHTMLNodeList |
179
|
|
|
*/ |
180
|
|
|
public function minus($nl) |
181
|
|
|
{ |
182
|
|
|
return $this->doMath($nl, 'minus'); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @param $nl |
187
|
|
|
* @param string $op |
188
|
|
|
* |
189
|
|
|
* @return AHTMLNodeList |
190
|
|
|
*/ |
191
|
|
|
public function doMath($nl, $op = 'plus') |
192
|
|
|
{ |
193
|
|
|
$paths = array(); |
194
|
|
|
$other_paths = array(); |
195
|
|
|
|
196
|
|
|
foreach ($this as $node) { |
197
|
|
|
$paths[] = $node->node->getNodePath(); |
198
|
|
|
} |
199
|
|
|
foreach ($nl as $node) { |
200
|
|
|
$other_paths[] = $node->node->getNodePath(); |
201
|
|
|
} |
202
|
|
|
switch ($op) { |
203
|
|
|
case 'plus': |
204
|
|
|
$new_paths = \array_unique(\array_merge($paths, $other_paths)); |
205
|
|
|
break; |
206
|
|
|
case 'minus': |
207
|
|
|
$new_paths = \array_diff($paths, $other_paths); |
208
|
|
|
break; |
209
|
|
|
case 'intersect': |
210
|
|
|
$new_paths = \array_intersect($paths, $other_paths); |
211
|
|
|
break; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
return new AHTMLNodeList($this->doc->xpath->query(implode('|', $new_paths)), $this->doc); |
|
|
|
|
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* @param $nl |
219
|
|
|
* |
220
|
|
|
* @return AHTMLNodeList |
221
|
|
|
*/ |
222
|
|
|
public function plus($nl) |
223
|
|
|
{ |
224
|
|
|
return $this->doMath($nl, 'plus'); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* @param $nl |
229
|
|
|
* |
230
|
|
|
* @return AHTMLNodeList |
231
|
|
|
*/ |
232
|
|
|
public function intersect($nl) |
233
|
|
|
{ |
234
|
|
|
return $this->doMath($nl, 'intersect'); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
|
238
|
|
|
// magic methods |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* @param $key |
242
|
|
|
* @param $values |
243
|
|
|
* |
244
|
|
|
* @return array|string |
245
|
|
|
*/ |
246
|
2 |
|
public function __call($key, $values) |
247
|
|
|
{ |
248
|
2 |
|
$key = \strtolower(\str_replace('_', '', $key)); |
249
|
|
|
switch ($key) { |
250
|
2 |
|
case 'to_a': |
251
|
|
|
$retval = array(); |
252
|
|
|
foreach ($this as $node) { |
253
|
|
|
$retval[] = new AHTMLNode($this->nodeList->item($this->counter), $this->doc); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
return $retval; |
257
|
|
|
} |
258
|
|
|
// otherwise |
259
|
|
|
|
260
|
2 |
|
$retval = array(); |
261
|
|
|
|
262
|
|
|
/* |
263
|
|
|
if(preg_match(TAGS_REGEX, $key, $m)) return $this->find($m[1]); |
264
|
|
|
if(preg_match(TAG_REGEX, $key, $m)) return $this->find($m[1], 0); |
265
|
|
|
*/ |
266
|
|
|
|
267
|
2 |
|
if (\preg_match(ATTRIBUTES_REGEX, $key, $m) || \preg_match('/^((clean|trim|str).*)s$/', $key, $m)) { |
268
|
|
|
foreach ($this as $node) { |
269
|
|
|
$arg = $m[1]; |
270
|
|
|
$retval[] = $node->$arg; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
return $retval; |
274
|
|
|
} |
275
|
|
|
|
276
|
2 |
|
if (\preg_match(ATTRIBUTE_REGEX, $key, $m)) { |
277
|
2 |
|
foreach ($this as $node) { |
278
|
2 |
|
$arg = $m[1]; |
279
|
2 |
|
$retval[] = $node->$arg; |
280
|
|
|
} |
281
|
|
|
|
282
|
2 |
|
return \implode('', $retval); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
// what now? |
286
|
2 |
|
foreach ($this as $node) { |
287
|
2 |
|
$retval[] = isset($values[0]) ? $node->$key($values[0]) : $node->$key(); |
288
|
|
|
} |
289
|
|
|
|
290
|
2 |
|
return \implode('', $retval); |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* @param $key |
295
|
|
|
* |
296
|
|
|
* @return mixed |
297
|
|
|
*/ |
298
|
2 |
|
public function __get($key) |
299
|
|
|
{ |
300
|
2 |
|
return $this->$key(); |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* @param $name |
305
|
|
|
* @param $value |
306
|
|
|
*/ |
307
|
1 |
|
public function __set($name, $value) |
308
|
|
|
{ |
309
|
1 |
|
throw new \InvalidArgumentException(__METHOD__); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* @param $name |
314
|
|
|
* |
315
|
|
|
* @return bool |
316
|
|
|
*/ |
317
|
|
|
public function __isset($name) |
318
|
|
|
{ |
319
|
|
|
return true; |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* @return string |
324
|
|
|
*/ |
325
|
2 |
|
public function __toString() |
326
|
|
|
{ |
327
|
2 |
|
return (string)$this->html(); |
|
|
|
|
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
/** |
331
|
|
|
* @return mixed |
332
|
|
|
*/ |
333
|
1 |
|
public function length() |
334
|
|
|
{ |
335
|
1 |
|
return $this->nodeList->length; |
336
|
|
|
} |
337
|
|
|
} |
338
|
|
|
|