NodeList::__construct()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.2
c 0
b 0
f 0
cc 4
eloc 7
nc 6
nop 1
1
<?php
2
3
namespace OpenMindParser\Models;
4
5
use \InvalidArgumentException;
6
use \ArrayIterator;
7
8
/*A class to store a collection of Node Objects as a Traversable entity.*/
9
class NodeList implements \IteratorAggregate
10
{
11
	/**
12
	 * @var Node[]
13
	 */
14
	private $list;
15
	
16
	public function __construct(array $list = null) {
17
		if(empty($list)) {
18
			$list = [];
19
		}
20
		
21
		foreach($list as $node) {
22
			if(!($node instanceof Node)) {
23
				throw new InvalidArgumentException('The array must contain only Node objects. "'.get_class($node).'" given.');
24
			}
25
		}
26
		
27
		$this->list = $list;
28
	}
29
	
30
	/**
31
	 * Method to retrieve the iterrator on the list property.
32
	 * 
33
	 * @return ArrayIterator : the ArrayInterrator instance with the current collection.
34
	 */
35
	public function getIterator() {
36
		return new ArrayIterator($this->list);
37
	}
38
	
39
	/**
40
	 * Add a Node object to the collection.
41
	 * 
42
	 * @param Node $node : The Node instance to add to the current collection.
43
	 */
44
	public function add(Node $node) {
45
		$this->list[] = $node;
46
	}
47
}