Completed
Push — master ( a046ab...1a416e )
by Бабичев
02:17
created

AHTMLNodeList::valid()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 2
rs 10
c 0
b 0
f 0
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 3
    public function __construct($nodeList, $doc)
30
    {
31 3
        $this->nodeList = $nodeList;
32 3
        $this->doc      = $doc;
33 3
    }
34
35
    /**
36
     * @see https://github.com/monkeysuffrage/advanced_html_dom/issues/19
37
     */
38 3
    public function __destruct()
39
    {
40 3
        $this->nodeList = $this->doc = null;
41 3
        unset($this->nodeList, $this->doc);
42 3
        Cleanup::all();
43 3
    }
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 2
    public function rewind()
101
    {
102 2
        $this->counter = 0;
103 2
    }
104
105
    /**
106
     * @return AHTMLNode
107
     */
108 2
    public function current()
109
    {
110 2
        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 2
    public function next()
125
    {
126 2
        $this->counter++;
127 2
    }
128
129
    /**
130
     * @return bool
131
     */
132 2
    public function valid()
133
    {
134 2
        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
        {
152
            $node->remove();
153
        }
154
155
        return $this;
156
    }
157
158
    /**
159
     * @param $c
160
     *
161
     * @return array
162
     */
163
    public function map($c)
164
    {
165
        $ret = array();
166
        foreach ($this as $node)
167
        {
168
            $ret[] = $c($node);
169
        }
170
171
        return $ret;
172
    }
173
174
175
    //math methods
176
177
    /**
178
     * @param        $nl
179
     * @param string $op
180
     *
181
     * @return AHTMLNodeList
182
     */
183
    public function doMath($nl, $op = 'plus')
184
    {
185
        $paths       = array();
186
        $other_paths = array();
187
188
        foreach ($this as $node)
189
        {
190
            $paths[] = $node->node->getNodePath();
191
        }
192
        foreach ($nl as $node)
193
        {
194
            $other_paths[] = $node->node->getNodePath();
195
        }
196
        switch ($op)
197
        {
198
            case 'plus':
199
                $new_paths = \array_unique(\array_merge($paths, $other_paths));
200
                break;
201
            case 'minus':
202
                $new_paths = \array_diff($paths, $other_paths);
203
                break;
204
            case 'intersect':
205
                $new_paths = \array_intersect($paths, $other_paths);
206
                break;
207
        }
208
209
        return new AHTMLNodeList($this->doc->xpath->query(implode('|', $new_paths)), $this->doc);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $new_paths does not seem to be defined for all execution paths leading up to this point.
Loading history...
210
    }
211
212
    /**
213
     * @param $nl
214
     *
215
     * @return AHTMLNodeList
216
     */
217
    public function minus($nl)
218
    {
219
        return $this->doMath($nl, 'minus');
220
    }
221
222
    /**
223
     * @param $nl
224
     *
225
     * @return AHTMLNodeList
226
     */
227
    public function plus($nl)
228
    {
229
        return $this->doMath($nl, 'plus');
230
    }
231
232
    /**
233
     * @param $nl
234
     *
235
     * @return AHTMLNodeList
236
     */
237
    public function intersect($nl)
238
    {
239
        return $this->doMath($nl, 'intersect');
240
    }
241
242
243
    // magic methods
244
245
    /**
246
     * @param $key
247
     * @param $values
248
     *
249
     * @return array|string
250
     */
251 2
    public function __call($key, $values)
252
    {
253 2
        $key = \strtolower(\str_replace('_', '', $key));
254
        switch ($key)
255
        {
256 2
            case 'to_a':
257
                $retval = array();
258
                foreach ($this as $node)
259
                {
260
                    $retval[] = new AHTMLNode($this->nodeList->item($this->counter), $this->doc);
261
                }
262
263
                return $retval;
264
        }
265
        // otherwise
266
267 2
        $retval = array();
268
269
        /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
71% 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...
270
            if(preg_match(TAGS_REGEX, $key, $m)) return $this->find($m[1]);
271
            if(preg_match(TAG_REGEX, $key, $m)) return $this->find($m[1], 0);
272
        */
273
274 2
        if (\preg_match(ATTRIBUTES_REGEX, $key, $m) || \preg_match('/^((clean|trim|str).*)s$/', $key, $m))
275
        {
276
            foreach ($this as $node)
277
            {
278
                $arg      = $m[1];
279
                $retval[] = $node->$arg;
280
            }
281
282
            return $retval;
283
        }
284
285 2
        if (\preg_match(ATTRIBUTE_REGEX, $key, $m))
286
        {
287 2
            foreach ($this as $node)
288
            {
289 2
                $arg      = $m[1];
290 2
                $retval[] = $node->$arg;
291
            }
292
293 2
            return \implode('', $retval);
294
        }
295
296
        // what now?
297 2
        foreach ($this as $node)
298
        {
299 2
            $retval[] = isset($values[0]) ? $node->$key($values[0]) : $node->$key();
300
        }
301
302 2
        return \implode('', $retval);
303
    }
304
305
    /**
306
     * @param $key
307
     *
308
     * @return mixed
309
     */
310 1
    public function __get($key)
311
    {
312 1
        return $this->$key();
313
    }
314
315
    /**
316
     * @param $name
317
     * @param $value
318
     */
319 1
    public function __set($name, $value)
320
    {
321 1
        throw new \InvalidArgumentException(__METHOD__);
322
    }
323
324
    /**
325
     * @param $name
326
     *
327
     * @return bool
328
     */
329
    public function __isset($name)
330
    {
331
        return true;
332
    }
333
334
    /**
335
     * @return string
336
     */
337 2
    public function __toString()
338
    {
339 2
        return (string)$this->html();
0 ignored issues
show
Bug introduced by
The method html() does not exist on Bavix\AdvancedHtmlDom\AHTMLNodeList. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

339
        return (string)$this->/** @scrutinizer ignore-call */ html();
Loading history...
340
    }
341
342
    /**
343
     * @return mixed
344
     */
345
    public function length()
346
    {
347
        return $this->nodeList->length;
348
    }
349
}
350