Completed
Pull Request — master (#3)
by Cristiano
01:50
created

ArrayList::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
namespace phootwork\collection;
3
4
use \Iterator;
5
6
/**
7
 * Represents a List
8
 * 
9
 * @author Thomas Gossmann
10
 */
11
class ArrayList extends AbstractList {
12
	
13
	/**
14
	 * Creates a new ArrayList
15
	 * 
16
	 * @param array|Iterator $collection
17
	 */
18 24
	public function __construct($collection = []) {
19 24
		$this->addAll($collection);
20 24
	}
21
	
22
	/**
23
	 * Adds an element to that list
24
	 * 
25
	 * @param mixed $element
26
	 * @param int $index
27
	 * @return $this
28
	 */
29 24
	public function add($element, $index = null) {
30 24
		if ($index === null) {
31 24
			$this->collection[$this->size()] = $element;
32
		} else {
33 1
			array_splice($this->collection, $index, 0, $element);
34
		}
35
		
36 24
		return $this;
37
	}
38
	
39
	/**
40
	 * Adds all elements to the list
41
	 * 
42
	 * @param array|Iterator $collection
43
	 * @return $this
44
	 */
45 24
	public function addAll($collection) {
46 24
		foreach ($collection as $element) {
47 18
			$this->add($element);
48
		}
49
		
50 24
		return $this;
51
	}
52
53
	/**
54
	 * Removes an element from the list
55
	 * 
56
	 * @param mixed $element
57
	 * @return $this
58
	 */
59 2
	public function remove($element) {
60 2
		$index = array_search($element, $this->collection, true);
61 2
		if ($index !== null) {
62 2
			unset($this->collection[$index]);
63
		}
64
65 2
		return $this;
66
	}
67
68
	/**
69
	 * Removes all elements from the list
70
	 *
71
	 * @param array|Iterator $collection
72
	 * @return $this
73
	 */
74 2
	public function removeAll($collection) {
75 2
		foreach ($collection as $element) {
76 2
			$this->remove($element);
77
		}
78
		
79 2
		return $this;
80
	}
81
82
}
83