Completed
Pull Request — master (#9)
by Todd
13:46 queued 08:21
created

pQuery.php (3 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, Countable, 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 35
    public function __construct($nodes = array()) {
26 35
        $this->nodes = $nodes;
27 35
    }
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
        foreach ($this->nodes as $node) {
55 7
            if ($value === null)
56 7
                return $node->attr($name);
57 1
            $node->attr($name, $value);
58 1
        }
59 1
        return $this;
60
    }
61
62 1
    public function before($content) {
63 1
        foreach ($this->nodes as $node) {
64 1
            $node->before($content);
65 1
        }
66 1
        return $this;
67
    }
68
69 1
    public function clear() {
70 1
        foreach ($this->nodes as $node) {
71 1
            $node->clear();
72 1
        }
73 1
        return $this;
74
    }
75
76 7
    public function count() {
77 7
        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()) {
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...
88
//        $formatter = new pQuery\HtmlFormatter($options);
89
//        return $formatter->format($dom);
90
//    }
91
92 3
    public function getIterator() {
93 3
        return new ArrayIterator($this->nodes);
94
    }
95
96 2
    public function hasClass($classname) {
97 2
        foreach ($this->nodes as $node) {
98 2
            if ($node->hasClass($classname))
99 2
                return true;
100 2
        }
101 2
        return false;
102
    }
103
104 3
    public function html($value = null) {
105 3
        if (empty($this->nodes) && $value === null)
106 3
            return '';
107
108 3
        foreach ($this->nodes as $node) {
109 3
            if ($value === null)
110 3
                return $node->html();
111 1
            $node->html($value);
112 1
        }
113 1
        return $this;
114
    }
115
116 1
    public function offsetExists($offset) {
117 1
        return isset($this->nodes[$offset]);
118
    }
119
120 2
    public function offsetGet($offset) {
121 2
        return isset($this->nodes[$offset]) ? $this->nodes[$offset] : null;
122
    }
123
124 2
    public function offsetSet($offset, $value) {
125
126 2
        if (is_null($offset) || !isset($this->nodes[$offset])) {
127 1
            throw new \BadMethodCallException("You are not allowed to add new nodes to the pQuery object.");
128
        } else {
129 1
            $this->nodes[$offset]->replaceWith($value);
130
        }
131 1
    }
132
133 1
    public function offsetUnset($offset) {
134 1
        if (isset($this->nodes[$offset])) {
135 1
            $this->nodes[$offset]->remove();
136 1
            unset($this->nodes[$offset]);
137 1
        }
138 1
    }
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 9
    public static function parseFile($path, $context = null) {
148 9
        $html_str = file_get_contents($path, false, $context);
149 9
        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 36
    public static function parseStr($html) {
159 36
        $parser = new pQuery\Html5Parser($html);
160 36
        return $parser->root;
0 ignored issues
show
The property root cannot be accessed from this context as it is declared private in class pQuery\HtmlParser.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
161
    }
162
163 1
    public function prepend($content = null) {
164 1
        foreach ($this->nodes as $node) {
165 1
            $node->prepend($content);
166 1
        }
167 1
        return $this;
168
    }
169
170 4
    public function prop($name, $value = null) {
171 4
        if (empty($this->nodes) && $value === null)
172 4
            return '';
173
174 4
        foreach ($this->nodes as $node) {
175 4
            if ($value === null)
176 4
                return $node->prop($name);
177 3
            $node->prop($name, $value);
178 3
        }
179 3
        return $this;
180
    }
181
182 1
    public function remove($selector = null) {
183 1
        foreach ($this->nodes as $node) {
184 1
            $node->remove($selector);
185 1
        }
186 1
        if ($selector === null)
187 1
            $this->nodes = array();
188
189 1
        return $this;
190
    }
191
192 1
    public function removeAttr($name) {
193 1
        foreach ($this->nodes as $node) {
194 1
            $node->removeAttr($name);
195 1
        }
196 1
        return $this;
197
    }
198
199 2
    public function removeClass($classname) {
200 2
        foreach ($this->nodes as $node) {
201 2
            $node->removeClass($classname);
202 2
        }
203 2
        return $this;
204
    }
205
206 1
    public function replaceWith($content) {
207 1
        foreach ($this->nodes as &$node) {
208 1
            $node = $node->replaceWith($content);
209 1
        }
210 1
        return $this;
211
    }
212
213 1
    public function tagName($value = null) {
214 1
        foreach ($this->nodes as $node) {
215 1
            if ($value === null)
216 1
                return $node->tagName();
217 1
            $node->tagName($value);
218 1
        }
219 1
        return $this;
220
    }
221
222 3
    public function text($value = null) {
223 3
        if (empty($this->nodes) && $value === null)
224 3
            return '';
225
226 3
        foreach ($this->nodes as $node) {
227 3
            if ($value === null)
228 3
                return $node->text();
229 2
            $node->text($value);
230 2
        }
231 2
        return $this;
232
    }
233
234 1
    public function toggleClass($classname, $switch = null) {
235 1
        foreach ($this->nodes as $node) {
236 1
            $node->toggleClass($classname, $switch);
237 1
        }
238
239 1
        return $this;
240
    }
241
242 1
    public function unwrap() {
243 1
        foreach ($this->nodes as $node) {
244 1
            $node->unwrap();
245 1
        }
246 1
        return $this;
247
    }
248
249 5
    public function val($value = null) {
250 5
        if (empty($this->nodes) && $value === null)
251 5
            return '';
252
253 5
        foreach ($this->nodes as $node) {
254 5
            if ($value === null)
255 5
                return $node->val();
256 4
            $node->val($value);
257 4
        }
258 4
        return $this;
259
    }
260
261 2
    public function wrap($wrapping_element) {
262 2
        foreach ($this->nodes as $node) {
263 2
            $node->wrap($wrapping_element);
264 2
        }
265 2
        return $this;
266
    }
267
268 1
    public function wrapInner($wrapping_element) {
269 1
        foreach ($this->nodes as $node) {
270 1
            $node->wrapInner($wrapping_element);
271 1
        }
272 1
        return $this;
273
    }
274
}
275