1
|
|
|
<?php |
2
|
|
|
namespace nochso\Omni; |
3
|
|
|
|
4
|
|
|
use Traversable; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* ArrayCollection wraps an array, providing common collection methods. |
8
|
|
|
*/ |
9
|
|
|
class ArrayCollection implements \Countable, \IteratorAggregate, \ArrayAccess{ |
10
|
|
|
/** |
11
|
|
|
* @var array |
12
|
|
|
*/ |
13
|
|
|
protected $list; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @param array $list |
17
|
|
|
*/ |
18
|
|
|
public function __construct(array $list = []) { |
19
|
|
|
$this->list = $list; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param mixed $element |
24
|
|
|
* |
25
|
|
|
* @return $this |
26
|
|
|
*/ |
27
|
|
|
public function add($element) { |
28
|
|
|
$this->list[] = $element; |
29
|
|
|
return $this; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Set or replace the element at a specific index. |
34
|
|
|
* |
35
|
|
|
* @param mixed $index |
36
|
|
|
* @param mixed $element |
37
|
|
|
* |
38
|
|
|
* @return $this |
39
|
|
|
*/ |
40
|
|
|
public function set($index, $element) { |
41
|
|
|
$this[$index] = $element; |
42
|
|
|
return $this; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Remove and return the element at the zero based index. |
47
|
|
|
* |
48
|
|
|
* @param int $index |
49
|
|
|
* |
50
|
|
|
* @return null|mixed Null if the element didn't exist. |
51
|
|
|
*/ |
52
|
|
|
public function remove($index) { |
53
|
|
|
if (!array_key_exists($index, $this->list)) { |
54
|
|
|
return null; |
55
|
|
|
} |
56
|
|
|
$removed = $this->list[$index]; |
57
|
|
|
unset($this->list[$index]); |
58
|
|
|
return $removed; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* First sets the internal pointer to the first element and returns it. |
63
|
|
|
* |
64
|
|
|
* @link http://php.net/manual/en/function.reset.php |
65
|
|
|
* |
66
|
|
|
* @return mixed the value of the first array element, or false if the array is empty. |
67
|
|
|
*/ |
68
|
|
|
public function first() { |
69
|
|
|
return reset($this->list); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Last sets the internal pointer to the last element and returns it. |
74
|
|
|
* |
75
|
|
|
* @link http://php.net/manual/en/function.end.php |
76
|
|
|
* |
77
|
|
|
* @return mixed the value of the last element or false for empty array. |
78
|
|
|
*/ |
79
|
|
|
public function last() { |
80
|
|
|
return end($this->list); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* toArray returns a plain old array. |
85
|
|
|
* |
86
|
|
|
* @return array |
87
|
|
|
*/ |
88
|
|
|
public function toArray() { |
89
|
|
|
return $this->list; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Apply a callable to every element. |
94
|
|
|
* |
95
|
|
|
* @param callable $callable |
96
|
|
|
* |
97
|
|
|
* @return $this |
98
|
|
|
*/ |
99
|
|
|
public function apply(callable $callable) { |
100
|
|
|
$this->list = array_map($callable, $this->list); |
101
|
|
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Count elements of an object. |
106
|
|
|
* |
107
|
|
|
* @link http://php.net/manual/en/countable.count.php |
108
|
|
|
* |
109
|
|
|
* @return int The custom count as an integer. |
110
|
|
|
*/ |
111
|
|
|
public function count() { |
112
|
|
|
return count($this->list); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* getIterator returns an external iterator. |
117
|
|
|
* |
118
|
|
|
* @link http://php.net/manual/en/iteratoraggregate.getiterator.php |
119
|
|
|
* |
120
|
|
|
* @return Traversable An instance of an object implementing <b>Iterator</b> or <b>Traversable</b> |
121
|
|
|
*/ |
122
|
|
|
public function getIterator() { |
123
|
|
|
return new \ArrayIterator($this->list); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* offsetExists allows using `isset`. |
128
|
|
|
* |
129
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetexists.php |
130
|
|
|
* |
131
|
|
|
* @param mixed $offset An offset to check for. |
132
|
|
|
* |
133
|
|
|
* @return bool true on success or false on failure. |
134
|
|
|
*/ |
135
|
|
|
public function offsetExists($offset) { |
136
|
|
|
return array_key_exists($offset, $this->list); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* offsetGet allows array access, e.g. `$list[2]`. |
141
|
|
|
* |
142
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetget.php |
143
|
|
|
* |
144
|
|
|
* @param mixed $offset The offset to retrieve. |
145
|
|
|
* |
146
|
|
|
* @return mixed Can return all value types. |
147
|
|
|
*/ |
148
|
|
|
public function offsetGet($offset) { |
149
|
|
|
return isset($this->list[$offset]) ? $this->list[$offset] : null; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* offsetSet allows writing to arrays `$list[2] = 'foo'`. |
154
|
|
|
* |
155
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetset.php |
156
|
|
|
* |
157
|
|
|
* @param mixed $offset The offset to assign the value to. |
158
|
|
|
* @param mixed $value The value to set. |
159
|
|
|
* |
160
|
|
|
* @return void |
161
|
|
|
*/ |
162
|
|
|
public function offsetSet($offset, $value) { |
163
|
|
|
if ($offset === null) { |
164
|
|
|
$this->add($value); |
165
|
|
|
return; |
166
|
|
|
} |
167
|
|
|
$this->list[$offset] = $value; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* offsetUnset allows using `unset`. |
172
|
|
|
* |
173
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetunset.php |
174
|
|
|
* |
175
|
|
|
* @param mixed $offset The offset to unset. |
176
|
|
|
* |
177
|
|
|
* @return void |
178
|
|
|
*/ |
179
|
|
|
public function offsetUnset($offset) { |
180
|
|
|
$this->remove($offset); |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|