1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the sauls/collections package. |
4
|
|
|
* |
5
|
|
|
* @author Saulius Vaičeliūnas <[email protected]> |
6
|
|
|
* @link http://saulius.vaiceliunas.lt |
7
|
|
|
* @copyright 2018 |
8
|
|
|
* |
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
10
|
|
|
* file that was distributed with this source code. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Sauls\Component\Collection; |
14
|
|
|
|
15
|
|
|
use function Sauls\Component\Helper\array_deep_search; |
16
|
|
|
use function Sauls\Component\Helper\array_get_value; |
17
|
|
|
use function Sauls\Component\Helper\array_remove_key; |
18
|
|
|
use function Sauls\Component\Helper\array_remove_value; |
19
|
|
|
use function Sauls\Component\Helper\array_merge; |
20
|
|
|
use function \Sauls\Component\Helper\array_key_exists; |
21
|
|
|
use function Sauls\Component\Helper\array_set_value; |
22
|
|
|
use Sauls\Component\Helper\Exception\PropertyNotAccessibleException; |
23
|
|
|
|
24
|
|
|
class ArrayCollection implements Collection, \Serializable |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
private $elements = []; |
30
|
|
|
|
31
|
51 |
|
public function __construct($elements = null) |
32
|
|
|
{ |
33
|
51 |
|
$this->add($this->assureArray($elements)); |
34
|
40 |
|
} |
35
|
|
|
|
36
|
51 |
|
private function assureArray($elements) |
37
|
|
|
{ |
38
|
51 |
|
if (\is_array($elements)) { |
39
|
51 |
|
return $elements; |
40
|
|
|
} |
41
|
|
|
|
42
|
2 |
|
if ($elements instanceof \Traversable) { |
43
|
1 |
|
return iterator_to_array($elements); |
44
|
|
|
} |
45
|
|
|
|
46
|
2 |
|
return (array) $elements; |
47
|
|
|
} |
48
|
|
|
|
49
|
11 |
|
public function create(array $elements): Collection |
50
|
|
|
{ |
51
|
11 |
|
return new static($elements); |
52
|
|
|
} |
53
|
|
|
|
54
|
40 |
|
public function set($key, $value): void |
55
|
|
|
{ |
56
|
40 |
|
array_set_value($this->elements, $key, $value); |
57
|
40 |
|
} |
58
|
|
|
|
59
|
40 |
|
public function add(array $elements): void |
60
|
|
|
{ |
61
|
40 |
|
foreach($elements as $key => $element) { |
62
|
39 |
|
$this->set($key, $element); |
63
|
|
|
} |
64
|
40 |
|
} |
65
|
|
|
|
66
|
1 |
|
public function merge(array $elements): void |
67
|
|
|
{ |
68
|
1 |
|
$this->elements = array_merge($this->elements, $elements); |
69
|
1 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @throws PropertyNotAccessibleException |
73
|
|
|
*/ |
74
|
8 |
|
public function get($key, $default = null) |
75
|
|
|
{ |
76
|
8 |
|
return array_get_value($this->elements, $key, $default); |
77
|
|
|
} |
78
|
|
|
|
79
|
1 |
|
public function replace(array $elements): void |
80
|
|
|
{ |
81
|
1 |
|
$this->clear(); |
82
|
1 |
|
$this->add($elements); |
83
|
1 |
|
} |
84
|
|
|
|
85
|
2 |
|
public function removeKey($key) |
86
|
|
|
{ |
87
|
2 |
|
return array_remove_key($this->elements, $key, false); |
88
|
|
|
} |
89
|
|
|
|
90
|
1 |
|
public function removeValue($element) |
91
|
|
|
{ |
92
|
1 |
|
return array_remove_value($this->elements, $element); |
93
|
|
|
} |
94
|
|
|
|
95
|
1 |
|
public function slice($offset, $length = null): array |
96
|
|
|
{ |
97
|
1 |
|
return \array_slice($this->elements, $offset, $length, true); |
98
|
|
|
} |
99
|
|
|
|
100
|
2 |
|
public function clear(): void |
101
|
|
|
{ |
102
|
2 |
|
$this->elements = []; |
103
|
2 |
|
} |
104
|
|
|
|
105
|
14 |
|
public function all(): array |
106
|
|
|
{ |
107
|
14 |
|
return $this->elements; |
108
|
|
|
} |
109
|
|
|
|
110
|
1 |
|
public function filter(\Closure $function) |
111
|
|
|
{ |
112
|
1 |
|
return \array_filter($this->elements, $function, ARRAY_FILTER_USE_BOTH); |
113
|
|
|
} |
114
|
|
|
|
115
|
1 |
|
public function map(\Closure $function) |
116
|
|
|
{ |
117
|
1 |
|
return \array_map($function, $this->elements); |
118
|
|
|
} |
119
|
|
|
|
120
|
2 |
|
public function keyOrValueExists($keyOrValue): bool |
121
|
|
|
{ |
122
|
2 |
|
if ($this->keyExists($keyOrValue)) { |
123
|
1 |
|
return true; |
124
|
|
|
} |
125
|
|
|
|
126
|
2 |
|
return $this->valueExists($keyOrValue); |
127
|
|
|
} |
128
|
|
|
|
129
|
1 |
|
public function keyOrValueDoesNotExists($keyOrValue): bool |
130
|
|
|
{ |
131
|
1 |
|
return false === $this->keyOrValueExists($keyOrValue); |
132
|
|
|
} |
133
|
|
|
|
134
|
10 |
|
public function keyExists($key): bool |
135
|
|
|
{ |
136
|
10 |
|
return array_key_exists($this->elements, $key); |
137
|
|
|
} |
138
|
|
|
|
139
|
3 |
|
public function keyDoesNotExists($key): bool |
140
|
|
|
{ |
141
|
3 |
|
return false === $this->keyExists($key); |
142
|
|
|
} |
143
|
|
|
|
144
|
4 |
|
public function valueExists($value): bool |
145
|
|
|
{ |
146
|
4 |
|
return empty(array_deep_search($this->elements, $value)) ? false : true; |
147
|
|
|
} |
148
|
|
|
|
149
|
1 |
|
public function valueDoesNotExists($value): bool |
150
|
|
|
{ |
151
|
1 |
|
return false === $this->valueExists($value); |
152
|
|
|
} |
153
|
|
|
|
154
|
1 |
|
public function valueIsNull($key): bool |
155
|
|
|
{ |
156
|
1 |
|
return null === $this->get($key); |
157
|
|
|
} |
158
|
|
|
|
159
|
1 |
|
public function valueIsNotNull($key): bool |
160
|
|
|
{ |
161
|
1 |
|
return false === $this->valueIsNull($key); |
162
|
|
|
} |
163
|
|
|
|
164
|
1 |
|
public function isEmpty(): bool |
165
|
|
|
{ |
166
|
1 |
|
return empty($this->elements); |
167
|
|
|
} |
168
|
|
|
|
169
|
1 |
|
public function __toString(): string |
170
|
|
|
{ |
171
|
1 |
|
return __CLASS__ . '#' . $this->getHash(); |
172
|
|
|
} |
173
|
|
|
|
174
|
2 |
|
public function getHash(): string |
175
|
|
|
{ |
176
|
2 |
|
return md5($this->serialize()); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @return string |
181
|
|
|
*/ |
182
|
1 |
|
public function getSplHash(): string |
183
|
|
|
{ |
184
|
1 |
|
return \spl_object_hash($this); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
|
188
|
1 |
|
public function getIterator() |
189
|
|
|
{ |
190
|
1 |
|
return new \ArrayIterator($this->elements); |
191
|
|
|
} |
192
|
|
|
|
193
|
1 |
|
public function offsetExists($offset) |
194
|
|
|
{ |
195
|
1 |
|
return $this->keyExists($offset); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @throws PropertyNotAccessibleException |
200
|
|
|
*/ |
201
|
2 |
|
public function offsetGet($offset) |
202
|
|
|
{ |
203
|
2 |
|
return $this->get($offset); |
204
|
|
|
} |
205
|
|
|
|
206
|
1 |
|
public function offsetSet($offset, $value) |
207
|
|
|
{ |
208
|
1 |
|
$this->set($offset, $value); |
209
|
1 |
|
} |
210
|
|
|
|
211
|
1 |
|
public function offsetUnset($offset) |
212
|
|
|
{ |
213
|
1 |
|
$this->removeKey($offset); |
214
|
1 |
|
} |
215
|
|
|
|
216
|
1 |
|
public function count(): int |
217
|
|
|
{ |
218
|
1 |
|
return \count($this->elements); |
219
|
|
|
} |
220
|
|
|
|
221
|
3 |
|
public function serialize(): string |
222
|
|
|
{ |
223
|
3 |
|
return \serialize($this->elements); |
224
|
|
|
} |
225
|
|
|
|
226
|
1 |
|
public function unserialize($value): void |
227
|
|
|
{ |
228
|
1 |
|
$this->elements = \unserialize($value); |
229
|
1 |
|
} |
230
|
|
|
|
231
|
2 |
|
public function sort(\Closure $function = null): Collection |
232
|
|
|
{ |
233
|
2 |
|
$elements = $this->elements; |
234
|
|
|
|
235
|
2 |
|
$function |
236
|
1 |
|
? \uasort($elements, $function) |
237
|
1 |
|
: \sort($elements); |
238
|
|
|
|
239
|
2 |
|
return $this->create($elements); |
240
|
|
|
} |
241
|
|
|
|
242
|
2 |
|
public function sortKeys(\Closure $function = null): Collection |
243
|
|
|
{ |
244
|
2 |
|
$elements = $this->elements; |
245
|
|
|
|
246
|
2 |
|
$function |
247
|
1 |
|
? \uksort($elements, $function) |
248
|
1 |
|
: \ksort($elements); |
249
|
|
|
|
250
|
2 |
|
return $this->create($elements); |
251
|
|
|
} |
252
|
|
|
|
253
|
2 |
|
public function diff(array $elements, \Closure $function = null): Collection |
254
|
|
|
{ |
255
|
2 |
|
$difference = $function |
256
|
1 |
|
? \array_udiff($this->elements, $elements, $function) |
257
|
2 |
|
: \array_diff($this->elements, $elements); |
258
|
|
|
|
259
|
2 |
|
return $this->create($difference); |
260
|
|
|
} |
261
|
|
|
|
262
|
2 |
|
public function diffKeys(array $elements, \Closure $function = null): Collection |
263
|
|
|
{ |
264
|
2 |
|
$keys = $function |
265
|
1 |
|
? \array_diff_ukey($this->elements, $elements, $function) |
266
|
2 |
|
: \array_diff_key($this->elements, $elements); |
267
|
|
|
|
268
|
2 |
|
return $this->create($keys); |
269
|
|
|
} |
270
|
|
|
|
271
|
2 |
|
public function diffAssoc(array $elements, \Closure $function = null): Collection |
272
|
|
|
{ |
273
|
2 |
|
$difference = $function |
274
|
1 |
|
? \array_diff_uassoc($this->elements, $elements, $function) |
275
|
2 |
|
: \array_diff_assoc($this->elements, $elements); |
276
|
|
|
|
277
|
2 |
|
return $this->create($difference); |
278
|
|
|
} |
279
|
|
|
} |
280
|
|
|
|