1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Part of the Fusion.Collection package. |
5
|
|
|
* |
6
|
|
|
* @license MIT |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
declare(strict_types=1); |
10
|
|
|
|
11
|
|
|
namespace Fusion\Collection\Contracts; |
12
|
|
|
|
13
|
|
|
use Fusion\Collection\Exceptions\CollectionException; |
14
|
|
|
use ArrayAccess; |
15
|
|
|
use Iterator; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Implements core functionality for collections. |
19
|
|
|
* |
20
|
|
|
* @since 1.0.0 |
21
|
|
|
*/ |
22
|
|
|
abstract class AbstractCollection implements CollectionCoreInterface, ArrayAccess, Iterator |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* The internal collection container. |
26
|
|
|
* |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
protected $collection = []; |
30
|
|
|
|
31
|
|
|
/** {@inheritdoc} */ |
32
|
1 |
|
public function clear(): void |
33
|
|
|
{ |
34
|
1 |
|
$this->collection = []; |
35
|
1 |
|
} |
36
|
|
|
|
37
|
|
|
/** {@inheritdoc} */ |
38
|
3 |
|
public function remove($item): void |
39
|
|
|
{ |
40
|
3 |
|
foreach ($this->collection as $key => $value) |
41
|
|
|
{ |
42
|
3 |
|
if ($item === $value) |
43
|
|
|
{ |
44
|
3 |
|
$this->removeAt($key); |
45
|
|
|
} |
46
|
|
|
} |
47
|
3 |
|
} |
48
|
|
|
|
49
|
|
|
/** {@inheritdoc} */ |
50
|
6 |
|
public function removeAt($key): void |
51
|
|
|
{ |
52
|
6 |
|
if ($this->offsetExists($key)) |
53
|
|
|
{ |
54
|
6 |
|
if (is_int($key)) |
55
|
|
|
{ |
56
|
3 |
|
array_splice($this->collection, $key, 1); |
57
|
|
|
} |
58
|
3 |
|
else if (is_string($key)) |
59
|
|
|
{ |
60
|
3 |
|
unset($this->collection[$key]); |
61
|
|
|
} |
62
|
|
|
} |
63
|
6 |
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Checks if an offset exists in the collection. |
67
|
|
|
* |
68
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetexists.php |
69
|
|
|
* |
70
|
|
|
* @param mixed $offset |
71
|
|
|
* |
72
|
|
|
* @return bool |
73
|
|
|
*/ |
74
|
36 |
|
public function offsetExists($offset) |
75
|
|
|
{ |
76
|
36 |
|
return array_key_exists($offset, $this->collection); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Retrieves a value at the given offset. |
81
|
|
|
* |
82
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetget.php |
83
|
|
|
* |
84
|
|
|
* @param mixed $offset |
85
|
|
|
* |
86
|
|
|
* @return mixed |
87
|
|
|
*/ |
88
|
16 |
|
public function offsetGet($offset) |
89
|
|
|
{ |
90
|
16 |
|
return $this->collection[$offset]; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Sets a value at the given offset. |
95
|
|
|
* |
96
|
|
|
* This method will throw a `CollectionException` if the offset does not exist. |
97
|
|
|
* |
98
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetset.php |
99
|
|
|
* |
100
|
|
|
* @param mixed $offset |
101
|
|
|
* @param mixed $value |
102
|
|
|
* |
103
|
|
|
* @return void |
104
|
|
|
* |
105
|
|
|
* @throws \Fusion\Collection\Exceptions\CollectionException |
106
|
|
|
*/ |
107
|
26 |
|
public function offsetSet($offset, $value): void |
108
|
|
|
{ |
109
|
26 |
|
$this->throwExceptionIfValueIsNull($value); |
110
|
23 |
|
$this->collection[$offset] = $value; |
111
|
23 |
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Removes a value at the given offset. |
115
|
|
|
* |
116
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetunset.php |
117
|
|
|
* |
118
|
|
|
* @param mixed $offset |
119
|
|
|
* |
120
|
|
|
* @return void |
121
|
|
|
*/ |
122
|
3 |
|
public function offsetUnset($offset): void |
123
|
|
|
{ |
124
|
3 |
|
if ($this->offsetExists($offset)) |
125
|
|
|
{ |
126
|
3 |
|
$this->removeAt($offset); |
127
|
|
|
} |
128
|
3 |
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Checks if a given value is `null` and throws an exception if so. |
132
|
|
|
* |
133
|
|
|
* @param mixed $value |
134
|
|
|
* |
135
|
|
|
* @throws \Fusion\Collection\Exceptions\CollectionException |
136
|
|
|
* |
137
|
|
|
* @return void |
138
|
|
|
*/ |
139
|
65 |
|
protected function throwExceptionIfValueIsNull($value): void |
140
|
|
|
{ |
141
|
65 |
|
if ($value === null) |
142
|
|
|
{ |
143
|
6 |
|
throw new CollectionException('Collection operations will not accept null values.'); |
144
|
|
|
} |
145
|
64 |
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Checks if a given offset exists in the collection and throws and exception if it does not. |
149
|
|
|
* |
150
|
|
|
* @see \Fusion\Collection\Contracts\AbstractCollection::offsetExists() |
151
|
|
|
* |
152
|
|
|
* @param mixed $offset |
153
|
|
|
* |
154
|
|
|
* @throws \Fusion\Collection\Exceptions\CollectionException |
155
|
|
|
* |
156
|
|
|
* @return void |
157
|
|
|
*/ |
158
|
27 |
|
protected function throwExceptionIfOffsetDoesNotExist($offset): void |
159
|
|
|
{ |
160
|
27 |
|
if ($this->offsetExists($offset) === false) |
161
|
|
|
{ |
162
|
9 |
|
throw new CollectionException("The key '$offset' does not exist in the collection."); |
163
|
|
|
} |
164
|
18 |
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Returns the current element in the collection. |
168
|
|
|
* |
169
|
|
|
* @link http://php.net/manual/en/iterator.current.php |
170
|
|
|
* |
171
|
|
|
* @return mixed |
172
|
|
|
*/ |
173
|
4 |
|
public function current() |
174
|
|
|
{ |
175
|
4 |
|
return current($this->collection); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Move forward to the next element in the collection. |
180
|
|
|
* |
181
|
|
|
* @link http://php.net/manual/en/iterator.next.php |
182
|
|
|
* |
183
|
|
|
* @return void |
184
|
|
|
*/ |
185
|
4 |
|
public function next(): void |
186
|
|
|
{ |
187
|
4 |
|
next($this->collection); |
188
|
4 |
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Return the key of the current element in the collection. |
192
|
|
|
* |
193
|
|
|
* @link http://php.net/manual/en/iterator.key.php |
194
|
|
|
* |
195
|
|
|
* @return mixed |
196
|
|
|
*/ |
197
|
2 |
|
public function key() |
198
|
|
|
{ |
199
|
2 |
|
return key($this->collection); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Checks if the current element position is valid. |
204
|
|
|
* |
205
|
|
|
* @link http://php.net/manual/en/iterator.valid.php |
206
|
|
|
* |
207
|
|
|
* @return bool |
208
|
|
|
*/ |
209
|
3 |
|
public function valid(): bool |
210
|
|
|
{ |
211
|
3 |
|
return key($this->collection) !== null; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Rewind the collection's position to the first index. |
216
|
|
|
* |
217
|
|
|
* @link http://php.net/manual/en/iterator.rewind.php |
218
|
|
|
* |
219
|
|
|
* @return void |
220
|
|
|
*/ |
221
|
2 |
|
public function rewind(): void |
222
|
|
|
{ |
223
|
2 |
|
reset($this->collection); |
224
|
2 |
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Returns of count of the elements in a collection. |
228
|
|
|
* |
229
|
|
|
* @link http://php.net/manual/en/countable.count.php |
230
|
|
|
* |
231
|
|
|
* @return int |
232
|
|
|
*/ |
233
|
20 |
|
public function count(): int |
234
|
|
|
{ |
235
|
20 |
|
return count($this->collection); |
236
|
|
|
} |
237
|
|
|
} |