Completed
Push — master ( e4761e...e4321c )
by Бабичев
03:02
created

AHTMLNodeList::__set()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
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
    abstract public boolean offsetExists ( mixed $offset )
37
    abstract public mixed offsetGet ( mixed $offset )
38
    abstract public void offsetSet ( mixed $offset , mixed $value )
39
    abstract public void offsetUnset ( mixed $offset )
40
    */
41
42
    /**
43
     * @param mixed $offset
44
     *
45
     * @return bool
46
     */
47
    public function offsetExists($offset)
48
    {
49
        return 0 <= $offset && $offset < $this->nodeList->length;
50
    }
51
52
    /**
53
     * @param mixed $offset
54
     *
55
     * @return AHTMLNode
56
     */
57
    public function offsetGet($offset)
58
    {
59
        return new AHTMLNode($this->nodeList->item($offset), $this->doc);
60
    }
61
62
    /**
63
     * @param mixed $offset
64
     * @param mixed $value
65
     */
66
    public function offsetSet($offset, $value)
67
    {
68
        \trigger_error('offsetSet not implemented', E_USER_WARNING);
69
    }
70
71
    /**
72
     * @param mixed $offset
73
     */
74
    public function offsetUnset($offset)
75
    {
76
        \trigger_error('offsetUnset not implemented', E_USER_WARNING);
77
    }
78
79
    /**
80
     * @return mixed
81
     */
82
    public function count()
83
    {
84
        return $this->nodeList->length;
85
    }
86
87
    /**
88
     *
89
     */
90 2
    public function rewind()
91
    {
92 2
        $this->counter = 0;
93 2
    }
94
95
    /**
96
     * @return AHTMLNode
97
     */
98 2
    public function current()
99
    {
100 2
        return new AHTMLNode($this->nodeList->item($this->counter), $this->doc);
101
    }
102
103
    /**
104
     * @return int
105
     */
106
    public function key()
107
    {
108
        return $this->counter;
109
    }
110
111
    /**
112
     *
113
     */
114 2
    public function next()
115
    {
116 2
        $this->counter++;
117 2
    }
118
119
    /**
120
     * @return bool
121
     */
122 2
    public function valid()
123
    {
124 2
        return $this->nodeList && $this->counter < $this->nodeList->length;
125
    }
126
127
    /**
128
     * @return AHTMLNode|null
129
     */
130
    public function last()
131
    {
132
        return ($this->nodeList->length > 0) ? new AHTMLNode($this->nodeList->item($this->nodeList->length - 1), $this->doc) : null;
133
    }
134
135
    /**
136
     * @return $this
137
     */
138
    public function remove()
139
    {
140
        foreach ($this as $node)
141
        {
142
            $node->remove();
143
        }
144
145
        return $this;
146
    }
147
148
    /**
149
     * @param $c
150
     *
151
     * @return array
152
     */
153
    public function map($c)
154
    {
155
        $ret = array();
156
        foreach ($this as $node)
157
        {
158
            $ret[] = $c($node);
159
        }
160
161
        return $ret;
162
    }
163
164
165
    //math methods
166
167
    /**
168
     * @param        $nl
169
     * @param string $op
170
     *
171
     * @return AHTMLNodeList
172
     */
173
    public function doMath($nl, $op = 'plus')
174
    {
175
        $paths       = array();
176
        $other_paths = array();
177
178
        foreach ($this as $node)
179
        {
180
            $paths[] = $node->node->getNodePath();
181
        }
182
        foreach ($nl as $node)
183
        {
184
            $other_paths[] = $node->node->getNodePath();
185
        }
186
        switch ($op)
187
        {
188
            case 'plus':
189
                $new_paths = \array_unique(\array_merge($paths, $other_paths));
190
                break;
191
            case 'minus':
192
                $new_paths = \array_diff($paths, $other_paths);
193
                break;
194
            case 'intersect':
195
                $new_paths = \array_intersect($paths, $other_paths);
196
                break;
197
        }
198
199
        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...
200
    }
201
202
    /**
203
     * @param $nl
204
     *
205
     * @return AHTMLNodeList
206
     */
207
    public function minus($nl)
208
    {
209
        return $this->doMath($nl, 'minus');
210
    }
211
212
    /**
213
     * @param $nl
214
     *
215
     * @return AHTMLNodeList
216
     */
217
    public function plus($nl)
218
    {
219
        return $this->doMath($nl, 'plus');
220
    }
221
222
    /**
223
     * @param $nl
224
     *
225
     * @return AHTMLNodeList
226
     */
227
    public function intersect($nl)
228
    {
229
        return $this->doMath($nl, 'intersect');
230
    }
231
232
233
    // magic methods
234
235
    /**
236
     * @param $key
237
     * @param $values
238
     *
239
     * @return array|string
240
     */
241 2
    public function __call($key, $values)
242
    {
243 2
        $key = \strtolower(\str_replace('_', '', $key));
244
        switch ($key)
245
        {
246 2
            case 'to_a':
247
                $retval = array();
248
                foreach ($this as $node)
249
                {
250
                    $retval[] = new AHTMLNode($this->nodeList->item($this->counter), $this->doc);
251
                }
252
253
                return $retval;
254
        }
255
        // otherwise
256
257 2
        $retval = array();
258
259
        /*
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...
260
            if(preg_match(TAGS_REGEX, $key, $m)) return $this->find($m[1]);
261
            if(preg_match(TAG_REGEX, $key, $m)) return $this->find($m[1], 0);
262
        */
263
264 2 View Code Duplication
        if (\preg_match(ATTRIBUTES_REGEX, $key, $m) || \preg_match('/^((clean|trim|str).*)s$/', $key, $m))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
265
        {
266
            foreach ($this as $node)
267
            {
268
                $arg      = $m[1];
269
                $retval[] = $node->$arg;
270
            }
271
272
            return $retval;
273
        }
274
275 2 View Code Duplication
        if (\preg_match(ATTRIBUTE_REGEX, $key, $m))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
276
        {
277 2
            foreach ($this as $node)
278
            {
279 2
                $arg      = $m[1];
280 2
                $retval[] = $node->$arg;
281
            }
282
283 2
            return \implode('', $retval);
284
        }
285
286
        // what now?
287 2
        foreach ($this as $node)
288
        {
289 2
            $retval[] = isset($values[0]) ? $node->$key($values[0]) : $node->$key();
290
        }
291
292 2
        return \implode('', $retval);
293
    }
294
295
    /**
296
     * @param $key
297
     *
298
     * @return mixed
299
     */
300 1
    public function __get($key)
301
    {
302 1
        return $this->$key();
303
    }
304
305
    /**
306
     * @param $name
307
     * @param $value
308
     */
309 1
    public function __set($name, $value)
310
    {
311 1
        throw new \InvalidArgumentException(__METHOD__);
312
    }
313
314
    /**
315
     * @param $name
316
     *
317
     * @return bool
318
     */
319
    public function __isset($name)
320
    {
321
        return true;
322
    }
323
324
    /**
325
     * @return string
326
     */
327 2
    public function __toString()
328
    {
329 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

329
        return (string)$this->/** @scrutinizer ignore-call */ html();
Loading history...
330
    }
331
332
    /**
333
     * @return mixed
334
     */
335
    public function length()
336
    {
337
        return $this->nodeList->length;
338
    }
339
}
340