Completed
Pull Request — master (#16)
by
unknown
10:12
created

pQuery.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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, IteratorAggregate, IQuery {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
16
    /// Properties ///
17
18
    /**
19
     * @var IQuery[]
20
     */
21
    protected $nodes = array();
22
23
    /// Methods ///
24
25 36
    public function __construct($nodes = array()) {
26 36
        $this->nodes = $nodes;
27 36
    }
28
29 3
    public function addClass($classname) {
30 3
        foreach ($this->nodes as $node) {
31 3
            $node->addClass($classname);
32 3
        }
33 3
        return $this;
34
    }
35
36 1
    public function after($content) {
37 1
        foreach ($this->nodes as $node) {
38 1
            $node->after($content);
39 1
        }
40 1
        return $this;
41
    }
42
43 1
    public function append($content) {
44 1
        foreach ($this->nodes as $node) {
45 1
            $node->append($content);
46 1
        }
47 1
        return $this;
48
    }
49
50 7
    public function attr($name, $value = null) {
51 7
        if (empty($this->nodes) && $value === null){
52 7
            return '';
53
        }
54 7
55 7
        foreach ($this->nodes as $node) {
56 7
            if ($value === null){
57 1
                return $node->attr($name);
58 1
            }     
59 1
            $node->attr($name, $value);
60
        }
61
        return $this;
62 1
    }
63 1
64 1
    public function before($content) {
65 1
        foreach ($this->nodes as $node) {
66 1
            $node->before($content);
67
        }
68
        return $this;
69 1
    }
70 1
71 1
    public function clear() {
72 1
        foreach ($this->nodes as $node) {
73 1
            $node->clear();
74
        }
75
        return $this;
76
    }
77
78
    /**
79
     * Get the count of matched elements.
80
     *
81 8
     * @return int Returns the count of matched elements.
82 8
     */
83
    public function count() {
84
        return count($this->nodes);
85
    }
86
87
    /**
88
     * Format/beautify a DOM.
89
     *
90
     * @param pQuery\DomNode $dom The dom to format.
91
     * @param array $options Extra formatting options. See {@link pQuery\HtmlFormatter::$options}.
92
     * @return bool Returns `true` on sucess and `false` on failure.
93
     */
94
//    public static function format($dom, $options = array()) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
54% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
95
//        $formatter = new pQuery\HtmlFormatter($options);
96
//        return $formatter->format($dom);
97 3
//    }
98 3
99
    public function getIterator() {
100
        return new ArrayIterator($this->nodes);
101 2
    }
102 2
103 2
    public function hasClass($classname) {
104 2
        foreach ($this->nodes as $node) {
105 2
            if ($node->hasClass($classname)){
106 2
                return true;
107
            }
108
        }
109 3
        return false;
110 3
    }
111 3
112
    public function html($value = null) {
113 3
        if (empty($this->nodes) && $value === null){
114 3
            return '';
115 3
        }
116 1
117 1
        foreach ($this->nodes as $node) {
118 1
            if ($value === null){
119
                return $node->html();
120
            }
121 1
            $node->html($value);
122 1
        }
123
        return $this;
124
    }
125 2
126 2
    public function offsetExists($offset) {
127
        return isset($this->nodes[$offset]);
128
    }
129 2
130
    public function offsetGet($offset) {
131 2
        return isset($this->nodes[$offset]) ? $this->nodes[$offset] : null;
132 1
    }
133
134 1
    public function offsetSet($offset, $value) {
135
136 1
        if (is_null($offset) || !isset($this->nodes[$offset])) {
137
            throw new \BadMethodCallException("You are not allowed to add new nodes to the pQuery object.");
138 1
        } else {
139 1
            $this->nodes[$offset]->replaceWith($value);
140 1
        }
141 1
    }
142 1
143 1
    public function offsetUnset($offset) {
144
        if (isset($this->nodes[$offset])) {
145
            $this->nodes[$offset]->remove();
146
            unset($this->nodes[$offset]);
147
        }
148
    }
149
150
    /**
151
     * Query a file or url.
152 9
     *
153 9
     * @param string $path The path to the url.
154 9
     * @param resource $context A context suitable to be passed into {@link file_get_contents}
155
     * @return pQuery\DomNode Returns the root dom node for the html file.
156
     */
157
    public static function parseFile($path, $context = null) {
158
        $html_str = file_get_contents($path, false, $context);
159
        return static::parseStr($html_str);
160
    }
161
162
    /**
163 37
     * Query a string of html.
164 37
     *
165 37
     * @param string $html
166
     * @return pQuery\DomNode Returns the root dom node for the html string.
167
     */
168 1
    public static function parseStr($html) {
169 1
        $parser = new pQuery\Html5Parser($html);
170 1
        return $parser->root;
171 1
    }
172 1
173
    public function prepend($content = null) {
174
        foreach ($this->nodes as $node) {
175 4
            $node->prepend($content);
176 4
        }
177 4
        return $this;
178
    }
179 4
180 4
    public function prop($name, $value = null) {
181 4
        if (empty($this->nodes) && $value === null){
182 3
            return '';
183 3
        }
184 3
185
        foreach ($this->nodes as $node) {
186
            if ($value === null){
187 1
                return $node->prop($name);
188 1
            }
189 1
            $node->prop($name, $value);
190 1
        }
191 1
        return $this;
192 1
    }
193
194 1
    public function remove($selector = null) {
195
        foreach ($this->nodes as $node) {
196
            $node->remove($selector);
197 1
        }
198 1
        if ($selector === null){
199 1
            $this->nodes = array();
200 1
        }
201 1
202
        return $this;
203
    }
204 2
205 2
    public function removeAttr($name) {
206 2
        foreach ($this->nodes as $node) {
207 2
            $node->removeAttr($name);
208 2
        }
209
        return $this;
210
    }
211 1
212 1
    public function removeClass($classname) {
213 1
        foreach ($this->nodes as $node) {
214 1
            $node->removeClass($classname);
215 1
        }
216
        return $this;
217
    }
218 1
219 1
    public function replaceWith($content) {
220 1
        foreach ($this->nodes as &$node) {
221 1
            $node = $node->replaceWith($content);
222 1
        }
223 1
        return $this;
224 1
    }
225
226
    public function tagName($value = null) {
227 3
        foreach ($this->nodes as $node) {
228 3
            if ($value === null){
229 3
                return $node->tagName();
230
            }
231 3
            $node->tagName($value);
232 3
        }
233 3
        return $this;
234 2
    }
235 2
236 2
    public function text($value = null) {
237
        if (empty($this->nodes) && $value === null){
238
            return '';
239 1
        }
240 1
241 1
        foreach ($this->nodes as $node) {
242 1
            if ($value === null){
243
                return $node->text();
244 1
            }
245
            $node->text($value);
246
        }
247 1
        return $this;
248 1
    }
249 1
250 1
    public function toggleClass($classname, $switch = null) {
251 1
        foreach ($this->nodes as $node) {
252
            $node->toggleClass($classname, $switch);
253
        }
254 5
255 5
        return $this;
256 5
    }
257
258 5
    public function unwrap() {
259 5
        foreach ($this->nodes as $node) {
260 5
            $node->unwrap();
261 4
        }
262 4
        return $this;
263 4
    }
264
265
    public function val($value = null) {
266 2
        if (empty($this->nodes) && $value === null){
267 2
            return '';
268 2
        }
269 2
270 2
        foreach ($this->nodes as $node) {
271
            if ($value === null){
272
                return $node->val();
273 1
            }
274 1
            $node->val($value);
275 1
        }
276 1
        return $this;
277 1
    }
278
279
    public function wrap($wrapping_element) {
280
        foreach ($this->nodes as $node) {
281
            $node->wrap($wrapping_element);
282
        }
283
        return $this;
284
    }
285
286
    public function wrapInner($wrapping_element) {
287
        foreach ($this->nodes as $node) {
288
            $node->wrapInner($wrapping_element);
289
        }
290
        return $this;
291
    }
292
}
293