|
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
|
|
|
public function __construct($collection = []) { |
|
19
|
|
|
$this->addAll($collection); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Adds an element to that list |
|
24
|
|
|
* |
|
25
|
|
|
* @param mixed $element |
|
26
|
|
|
* @param int $index |
|
27
|
|
|
* @return $this |
|
28
|
|
|
*/ |
|
29
|
|
|
public function add($element, $index = null) { |
|
30
|
|
|
if ($index === null) { |
|
31
|
|
|
$this->collection[$this->size()] = $element; |
|
32
|
|
|
} else { |
|
33
|
|
|
array_splice($this->collection, $index, 0, $element); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
return $this; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Adds all elements to the list |
|
41
|
|
|
* |
|
42
|
|
|
* @param array|Iterator $collection |
|
43
|
|
|
* @return $this |
|
44
|
|
|
*/ |
|
45
|
|
|
public function addAll($collection) { |
|
46
|
|
|
foreach ($collection as $element) { |
|
47
|
|
|
$this->add($element); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
return $this; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Returns the element at the given index (or nothing if the index isn't present) |
|
55
|
|
|
* |
|
56
|
|
|
* @param int $index |
|
57
|
|
|
* @return mixed |
|
58
|
|
|
*/ |
|
59
|
|
|
public function get($index) { |
|
60
|
|
|
if (isset($this->collection[$index])) { |
|
61
|
|
|
return $this->collection[$index]; |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Returns the index of the given element or FALSE if the element can't be found |
|
67
|
|
|
* |
|
68
|
|
|
* @param mixed $element |
|
69
|
|
|
* @return int the index for the given element |
|
70
|
|
|
*/ |
|
71
|
|
|
public function indexOf($element) { |
|
72
|
|
|
return array_search($element, $this->collection, true); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Removes an element from the list |
|
77
|
|
|
* |
|
78
|
|
|
* @param mixed $element |
|
79
|
|
|
* @return $this |
|
80
|
|
|
*/ |
|
81
|
|
|
public function remove($element) { |
|
82
|
|
|
$index = array_search($element, $this->collection, true); |
|
83
|
|
|
if ($index !== null) { |
|
84
|
|
|
unset($this->collection[$index]); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
return $this; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Removes all elements from the list |
|
92
|
|
|
* |
|
93
|
|
|
* @param array|Iterator $collection |
|
94
|
|
|
* @return $this |
|
95
|
|
|
*/ |
|
96
|
|
|
public function removeAll($collection) { |
|
97
|
|
|
foreach ($collection as $element) { |
|
98
|
|
|
$this->remove($element); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return $this; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
} |
|
105
|
|
|
|