ResourceListNode::isEmpty()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
nc 1
cc 1
eloc 2
nop 0
crap 1
1
<?php
2
3
namespace PPP\DataModel;
4
5
use ArrayIterator;
6
use Countable;
7
use InvalidArgumentException;
8
use IteratorAggregate;
9
10
/**
11
 * A list of resources node.
12
 *
13
 * @licence AGPLv3+
14
 * @author Thomas Pellissier Tanon
15
 */
16
class ResourceListNode extends AbstractNode implements IteratorAggregate, Countable {
17
18
	/**
19
	 * @var ResourceNode[]
20
	 */
21
	private $resources = array();
22
23
	/**
24
	 * @param (ResourceNode|ResourceListNode)[] $resources
25
	 */
26 11
	public function __construct(array $resources = array()) {
27 11
		foreach($resources as $param) {
28 8
			if($param instanceof ResourceNode) {
29 8
				$this->appendResource($param);
30 8
			} else if($param instanceof ResourceListNode) {
31 1
				$this->appendResourceList($param);
32 1
			} else {
33
				throw new InvalidArgumentException('A ResourceListNode can only be build from ResourceNode and ResourceListNode');
34
			}
35 11
		}
36 11
	}
37
38 8
	private function appendResource(ResourceNode $resource) {
39 8
		if(!$this->hasResource($resource)) {
40 8
			$this->resources[] = $resource;
41 8
		}
42 8
	}
43
44 1
	private function appendResourceList(ResourceListNode $resourceList) {
45 1
		foreach($resourceList as $resource) {
46 1
			$this->appendResource($resource);
47 1
		}
48 1
	}
49
50
	/**
51
	 * @param ResourceNode $resource
52
	 * @return bool
53
	 */
54 9
	public function hasResource(ResourceNode $resource) {
55 9
		foreach($this->resources as $resource2) {
56 4
			if($resource->equals($resource2)) {
57 2
				return true;
58
			}
59 9
		}
60
61 9
		return false;
62
	}
63
64
	/**
65
	 * @return ResourceNode[]
66
	 */
67 1
	public function toArray() {
68 1
		return $this->resources;
69
	}
70
71
	/**
72
	 * @see AbstractNode::getType
73
	 */
74 1
	public function getType() {
75 1
		return 'list';
76
	}
77
78
	/**
79
	 * @see AbstractNode::equals
80
	 */
81 5
	public function equals($target) {
82 5
		if(!($target instanceof self) || count($this->resources) !== count($target->resources)) {
83 4
			return false;
84
		}
85
86 1
		$length = count($this->resources);
87 1
		for($i = 0; $i < $length; $i++) {
88 1
			if(!$target->resources[$i]->equals($this->resources[$i])) {
89
				return false;
90
			}
91 1
		}
92
93 1
		return true;
94
	}
95
96
	/**
97
	 * @see IteratorAggregate::getIterator
98
	 */
99 2
	public function getIterator() {
100 2
		return new ArrayIterator($this->resources);
101
	}
102
103
	/**
104
	 * @see Countable::count
105
	 */
106 3
	public function count() {
107 3
		return count($this->resources);
108
	}
109
110
	/**
111
	 * @return bool
112
	 */
113 2
	public function isEmpty() {
114 2
		return empty($this->resources);
115
	}
116
}
117