1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Niels A.D. |
4
|
|
|
* @author Todd Burry <[email protected]> |
5
|
|
|
* @copyright 2010 Niels A.D., 2014 Todd Burry |
6
|
|
|
* @license http://opensource.org/licenses/LGPL-2.1 LGPL-2.1 |
7
|
|
|
* @package pQuery |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
use pQuery\IQuery; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* A jQuery-like object for php. |
14
|
|
|
*/ |
15
|
|
|
class pQuery implements ArrayAccess, Countable, IteratorAggregate, IQuery { |
|
|
|
|
16
|
|
|
/// Properties /// |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var IQuery[] |
20
|
|
|
*/ |
21
|
|
|
protected $nodes = array(); |
22
|
|
|
|
23
|
|
|
/// Methods /// |
24
|
|
|
|
25
|
|
|
public function __construct($nodes = array()) { |
26
|
|
|
$this->nodes = $nodes; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function addClass($classname) { |
30
|
|
|
foreach ($this->nodes as $node) { |
31
|
|
|
$node->addClass($classname); |
32
|
|
|
} |
33
|
|
|
return $this; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function after($content) { |
37
|
|
|
foreach ($this->nodes as $node) { |
38
|
|
|
$node->after($content); |
39
|
|
|
} |
40
|
|
|
return $this; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function append($content) { |
44
|
|
|
foreach ($this->nodes as $node) { |
45
|
|
|
$node->append($content); |
46
|
|
|
} |
47
|
|
|
return $this; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function attr($name, $value = null) { |
51
|
|
|
if (empty($this->nodes) && $value === null) |
52
|
|
|
return ''; |
53
|
|
|
|
54
|
|
|
foreach ($this->nodes as $node) { |
55
|
|
|
if ($value === null) |
56
|
|
|
return $node->attr($name); |
57
|
|
|
$node->attr($name, $value); |
58
|
|
|
} |
59
|
|
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function before($content) { |
63
|
|
|
foreach ($this->nodes as $node) { |
64
|
|
|
$node->before($content); |
65
|
|
|
} |
66
|
|
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function clear() { |
70
|
|
|
foreach ($this->nodes as $node) { |
71
|
|
|
$node->clear(); |
72
|
|
|
} |
73
|
|
|
return $this; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function count() { |
77
|
|
|
return count($this->nodes); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Format/beautify a DOM. |
82
|
|
|
* |
83
|
|
|
* @param pQuery\DomNode $dom The dom to format. |
84
|
|
|
* @param array $options Extra formatting options. See {@link pQuery\HtmlFormatter::$options}. |
85
|
|
|
* @return bool Returns `true` on sucess and `false` on failure. |
86
|
|
|
*/ |
87
|
|
|
// public static function format($dom, $options = array()) { |
|
|
|
|
88
|
|
|
// $formatter = new pQuery\HtmlFormatter($options); |
89
|
|
|
// return $formatter->format($dom); |
90
|
|
|
// } |
91
|
|
|
|
92
|
|
|
public function getIterator() { |
93
|
|
|
return new ArrayIterator($this->nodes); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function hasClass($classname) { |
97
|
|
|
foreach ($this->nodes as $node) { |
98
|
|
|
if ($node->hasClass($classname)) |
99
|
|
|
return true; |
100
|
|
|
} |
101
|
|
|
return false; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function html($value = null) { |
105
|
|
|
if (empty($this->nodes) && $value === null) |
106
|
|
|
return ''; |
107
|
|
|
|
108
|
|
|
foreach ($this->nodes as $node) { |
109
|
|
|
if ($value === null) |
110
|
|
|
return $node->html(); |
111
|
|
|
$node->html($value); |
112
|
|
|
} |
113
|
|
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function offsetExists($offset) { |
117
|
|
|
return isset($this->nodes[$offset]); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function offsetGet($offset) { |
121
|
|
|
return isset($this->nodes[$offset]) ? $this->nodes[$offset] : null; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function offsetSet($offset, $value) { |
125
|
|
|
|
126
|
|
|
if (is_null($offset) || !isset($this->nodes[$offset])) { |
127
|
|
|
throw new \BadMethodCallException("You are not allowed to add new nodes to the pQuery object."); |
128
|
|
|
} else { |
129
|
|
|
$this->nodes[$offset]->replaceWith($value); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function offsetUnset($offset) { |
134
|
|
|
if (isset($this->nodes[$offset])) { |
135
|
|
|
$this->nodes[$offset]->remove(); |
136
|
|
|
unset($this->nodes[$offset]); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Query a file or url. |
142
|
|
|
* |
143
|
|
|
* @param string $path The path to the url. |
144
|
|
|
* @param resource $context A context suitable to be passed into {@link file_get_contents} |
145
|
|
|
* @return pQuery\DomNode Returns the root dom node for the html file. |
146
|
|
|
*/ |
147
|
|
|
public static function parseFile($path, $context = null) { |
148
|
|
|
$html_str = file_get_contents($path, false, $context); |
149
|
|
|
return static::parseStr($html_str); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Query a string of html. |
154
|
|
|
* |
155
|
|
|
* @param string $html |
156
|
|
|
* @return pQuery\DomNode Returns the root dom node for the html string. |
157
|
|
|
*/ |
158
|
|
|
public static function parseStr($html) { |
159
|
|
|
$parser = new pQuery\Html5Parser($html); |
160
|
|
|
return $parser->root; |
|
|
|
|
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
public function prepend($content = null) { |
164
|
|
|
foreach ($this->nodes as $node) { |
165
|
|
|
$node->prepend($content); |
166
|
|
|
} |
167
|
|
|
return $this; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
public function prop($name, $value = null) { |
171
|
|
|
if (empty($this->nodes) && $value === null) |
172
|
|
|
return ''; |
173
|
|
|
|
174
|
|
|
foreach ($this->nodes as $node) { |
175
|
|
|
if ($value === null) |
176
|
|
|
return $node->prop($name); |
177
|
|
|
$node->prop($name, $value); |
178
|
|
|
} |
179
|
|
|
return $this; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
public function remove($selector = null) { |
183
|
|
|
foreach ($this->nodes as $node) { |
184
|
|
|
$node->remove($selector); |
185
|
|
|
} |
186
|
|
|
if ($selector === null) |
187
|
|
|
$this->nodes = array(); |
188
|
|
|
|
189
|
|
|
return $this; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
public function removeAttr($name) { |
193
|
|
|
foreach ($this->nodes as $node) { |
194
|
|
|
$node->removeAttr($name); |
195
|
|
|
} |
196
|
|
|
return $this; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
public function removeClass($classname) { |
200
|
|
|
foreach ($this->nodes as $node) { |
201
|
|
|
$node->removeClass($classname); |
202
|
|
|
} |
203
|
|
|
return $this; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
public function replaceWith($content) { |
207
|
|
|
foreach ($this->nodes as &$node) { |
208
|
|
|
$node = $node->replaceWith($content); |
209
|
|
|
} |
210
|
|
|
return $this; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
public function tagName($value = null) { |
214
|
|
|
foreach ($this->nodes as $node) { |
215
|
|
|
if ($value === null) |
216
|
|
|
return $node->tagName(); |
217
|
|
|
$node->tagName($value); |
218
|
|
|
} |
219
|
|
|
return $this; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
public function text($value = null) { |
223
|
|
|
if (empty($this->nodes) && $value === null) |
224
|
|
|
return ''; |
225
|
|
|
|
226
|
|
|
foreach ($this->nodes as $node) { |
227
|
|
|
if ($value === null) |
228
|
|
|
return $node->text(); |
229
|
|
|
$node->text($value); |
230
|
|
|
} |
231
|
|
|
return $this; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
public function toggleClass($classname, $switch = null) { |
235
|
|
|
foreach ($this->nodes as $node) { |
236
|
|
|
$node->toggleClass($classname, $switch); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
return $this; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
public function unwrap() { |
243
|
|
|
foreach ($this->nodes as $node) { |
244
|
|
|
$node->unwrap(); |
245
|
|
|
} |
246
|
|
|
return $this; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
public function val($value = null) { |
250
|
|
|
if (empty($this->nodes) && $value === null) |
251
|
|
|
return ''; |
252
|
|
|
|
253
|
|
|
foreach ($this->nodes as $node) { |
254
|
|
|
if ($value === null) |
255
|
|
|
return $node->val(); |
256
|
|
|
$node->val($value); |
257
|
|
|
} |
258
|
|
|
return $this; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
public function wrap($wrapping_element) { |
262
|
|
|
foreach ($this->nodes as $node) { |
263
|
|
|
$node->wrap($wrapping_element); |
264
|
|
|
} |
265
|
|
|
return $this; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
public function wrapInner($wrapping_element) { |
269
|
|
|
foreach ($this->nodes as $node) { |
270
|
|
|
$node->wrapInner($wrapping_element); |
271
|
|
|
} |
272
|
|
|
return $this; |
273
|
|
|
} |
274
|
|
|
} |
275
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.