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