Completed
Push — master ( 634c3a...7d6b1f )
by smiley
02:28
created

NodeList::rewind()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
c 0
b 0
f 0
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 *
4
 * @filesource   NodeList.php
5
 * @created      09.05.2017
6
 * @package      chillerlan\PrototypeDOM
7
 * @author       Smiley <[email protected]>
8
 * @copyright    2017 Smiley
9
 * @license      MIT
10
 */
11
12
namespace chillerlan\PrototypeDOM;
13
14
use DOMNode, DOMNodeList, Iterator, ArrayAccess, Countable;
15
16
/**
17
 * Class NodeList
18
 */
19
class NodeList implements Iterator, ArrayAccess, Countable{
20
21
	protected $nodelist = [];
22
	protected $key = 0;
23
24
	/**
25
	 * NodeList constructor.
26
	 *
27
	 * @param \DOMNodeList|\chillerlan\PrototypeDOM\NodeList|\chillerlan\PrototypeDOM\Element[] $content
28
	 *
29
	 * @throws \Exception
30
	 */
31 40
	public function __construct($content = null){
32
33 40
		if($content instanceof NodeList || $content instanceof DOMNodeList){
34 39
			$this->nodelist = iterator_to_array($content);
35
		}
36 39
		elseif(is_array($content)){
37 3
			$this->nodelist = $content;
38
		}
39
/*		else{
0 ignored issues
show
Unused Code Comprehensibility introduced by
57% 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...
40
			throw new \Exception('invalid content'); // @codeCoverageIgnore
41
		}*/
42
43 40
	}
44
45 1
	public function match(DOMNode $node):bool{
46
47 1
		foreach($this->nodelist as $element){
48
49 1
			if($element->isSameNode($node)){
50 1
				return true;
51
			}
52
53
		}
54
55 1
		return false;
56
	}
57
58 3
	public function item(int $index) {
59 3
		return $this->nodelist[$index] ?? null;
60
	}
61
62 3
	public function reverse():NodeList{
63 3
		$this->nodelist = array_reverse($this->nodelist);
64 3
		$this->rewind();
65
66 3
		return $this;
67
	}
68
69 2
	public function _toArray():array {
70 2
		return $this->nodelist;
71
	}
72
73 1
	public function merge(NodeList $nodelist):NodeList{
74 1
		$this->nodelist = array_merge($this->nodelist, $nodelist->_toArray());
75
76 1
		return $this;
77
	}
78
79
80 39
	public function current():DOMNode{
81 39
		return $this->nodelist[$this->key];
82
	}
83
84 4
	public function key():int{
85 4
		return $this->key;
86
	}
87
88 39
	public function valid():bool{
89 39
		return isset($this->nodelist[$this->key]);
90
	}
91
92 39
	public function next(){
93 39
		$this->key++;
94 39
	}
95
96 39
	public function rewind(){
97 39
		$this->key = 0;
98 39
	}
99
100
101 38
	public function offsetExists($offset):bool{
102 38
		return isset($this->nodelist[$offset]);
103
	}
104
105 38
	public function offsetGet($offset){
106 38
		return $this->nodelist[$offset] ?? null;
107
	}
108
109 38
	public function offsetSet($offset, $value){
110
111 38
		if($this->offsetExists($offset)){
112
			$this->nodelist[$offset] = $value;
113
		}
114
		else{
115 38
			$this->nodelist[] = $value;
116
		}
117 38
	}
118
119 1
	public function offsetUnset($offset){
120 1
		unset($this->nodelist[$offset]);
121 1
	}
122
123
124 5
	public function count():int{
125 5
		return count($this->nodelist);
126
	}
127
}
128