|
1
|
|
|
<?php declare(strict_types = 1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Apicart\Utils\Arrays; |
|
4
|
|
|
|
|
5
|
|
|
use ArrayAccess; |
|
6
|
|
|
use Countable; |
|
7
|
|
|
use Iterator; |
|
8
|
|
|
use IteratorAggregate; |
|
9
|
|
|
use RecursiveArrayIterator; |
|
10
|
|
|
|
|
11
|
|
|
final class SimpleArrayCollection implements ArrayAccess, Countable, IteratorAggregate |
|
12
|
|
|
{ |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var array |
|
16
|
|
|
*/ |
|
17
|
|
|
private $elements = []; |
|
18
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
public function __construct(array $elements = []) |
|
21
|
|
|
{ |
|
22
|
|
|
$this->elements = $elements; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
public function createFrom(array $elements): self |
|
27
|
|
|
{ |
|
28
|
|
|
return new static($elements); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
public function toArray(): array |
|
33
|
|
|
{ |
|
34
|
|
|
return $this->elements; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @return mixed|null |
|
40
|
|
|
*/ |
|
41
|
|
|
public function column(int $index = 0) |
|
42
|
|
|
{ |
|
43
|
|
|
$firstRow = $this->first(); |
|
44
|
|
|
if ($firstRow !== null) { |
|
45
|
|
|
$values = array_values($firstRow); |
|
46
|
|
|
|
|
47
|
|
|
return $values[$index] ?? null; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
return null; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @return mixed|null |
|
56
|
|
|
*/ |
|
57
|
|
|
public function first() |
|
58
|
|
|
{ |
|
59
|
|
|
$first = reset($this->elements); |
|
60
|
|
|
|
|
61
|
|
|
return $first ?: null; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @return mixed|null |
|
67
|
|
|
*/ |
|
68
|
|
|
public function last() |
|
69
|
|
|
{ |
|
70
|
|
|
$last = end($this->elements); |
|
71
|
|
|
|
|
72
|
|
|
return $last ?: null; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @return int|string|null |
|
78
|
|
|
*/ |
|
79
|
|
|
public function key() |
|
80
|
|
|
{ |
|
81
|
|
|
return key($this->elements); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @return mixed|null |
|
87
|
|
|
*/ |
|
88
|
|
|
public function next() |
|
89
|
|
|
{ |
|
90
|
|
|
$next = next($this->elements); |
|
91
|
|
|
|
|
92
|
|
|
return $next ?: null; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @return mixed|null |
|
98
|
|
|
*/ |
|
99
|
|
|
public function current() |
|
100
|
|
|
{ |
|
101
|
|
|
$current = current($this->elements); |
|
102
|
|
|
|
|
103
|
|
|
return $current ?: null; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @param int|string $key |
|
109
|
|
|
* @return mixed|null |
|
110
|
|
|
*/ |
|
111
|
|
|
public function remove($key) |
|
112
|
|
|
{ |
|
113
|
|
|
if (! isset($this->elements[$key]) && ! $this->contains($key)) { |
|
114
|
|
|
return null; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
$removed = $this->elements[$key]; |
|
118
|
|
|
unset($this->elements[$key]); |
|
119
|
|
|
|
|
120
|
|
|
return $removed; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @param mixed $element |
|
126
|
|
|
* @return bool|int|string |
|
127
|
|
|
*/ |
|
128
|
|
|
public function indexOf($element) |
|
129
|
|
|
{ |
|
130
|
|
|
return array_search($element, $this->elements, true); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @param int|string $key |
|
136
|
|
|
* @return mixed|null |
|
137
|
|
|
*/ |
|
138
|
|
|
public function get($key) |
|
139
|
|
|
{ |
|
140
|
|
|
return $this->elements[$key] ?? null; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
|
|
144
|
|
|
public function getKeys(): array |
|
145
|
|
|
{ |
|
146
|
|
|
return array_keys($this->elements); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
|
|
150
|
|
|
public function getValues(): array |
|
151
|
|
|
{ |
|
152
|
|
|
return array_values($this->elements); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* {@inheritdoc} |
|
158
|
|
|
*/ |
|
159
|
|
|
public function count(): int |
|
160
|
|
|
{ |
|
161
|
|
|
return count($this->elements); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* @param int|string $key |
|
167
|
|
|
* @param mixed $value |
|
168
|
|
|
*/ |
|
169
|
|
|
public function set($key, $value): void |
|
170
|
|
|
{ |
|
171
|
|
|
$this->elements[$key] = $value; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* @param mixed $element |
|
177
|
|
|
*/ |
|
178
|
|
|
public function add($element): bool |
|
179
|
|
|
{ |
|
180
|
|
|
$this->elements[] = $element; |
|
181
|
|
|
|
|
182
|
|
|
return true; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
|
|
186
|
|
|
public function isEmpty(): bool |
|
187
|
|
|
{ |
|
188
|
|
|
return $this->elements === []; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
|
|
192
|
|
|
public function clear(): void |
|
193
|
|
|
{ |
|
194
|
|
|
$this->elements = []; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* {@inheritdoc} |
|
200
|
|
|
*/ |
|
201
|
|
|
public function getIterator(): Iterator |
|
202
|
|
|
{ |
|
203
|
|
|
return new RecursiveArrayIterator($this->toArray()); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* @param mixed $offset |
|
209
|
|
|
*/ |
|
210
|
|
|
public function offsetExists($offset): bool |
|
211
|
|
|
{ |
|
212
|
|
|
return $this->contains($offset); |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* @param mixed $offset |
|
218
|
|
|
* @return mixed |
|
219
|
|
|
*/ |
|
220
|
|
|
public function offsetGet($offset) |
|
221
|
|
|
{ |
|
222
|
|
|
return $this->get($offset); |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* @param mixed $offset |
|
228
|
|
|
* @param mixed $value |
|
229
|
|
|
*/ |
|
230
|
|
|
public function offsetSet($offset, $value): void |
|
231
|
|
|
{ |
|
232
|
|
|
$this->set($offset, $value); |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* @param mixed $offset |
|
238
|
|
|
*/ |
|
239
|
|
|
public function offsetUnset($offset): void |
|
240
|
|
|
{ |
|
241
|
|
|
$this->remove($offset); |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
|
|
245
|
|
|
/** |
|
246
|
|
|
* @param int|string $key |
|
247
|
|
|
*/ |
|
248
|
|
|
private function contains($key): bool |
|
249
|
|
|
{ |
|
250
|
|
|
return isset($this->elements[$key]) || array_key_exists($key, $this->elements); |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
} |
|
254
|
|
|
|