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
|
40 |
|
public function __construct(array $elements = []) |
32
|
|
|
{ |
33
|
40 |
|
$this->add($elements); |
34
|
29 |
|
} |
35
|
|
|
|
36
|
1 |
|
public function create(array $elements): Collection |
37
|
|
|
{ |
38
|
1 |
|
return new static($elements); |
39
|
|
|
} |
40
|
|
|
|
41
|
29 |
|
public function set($key, $value): void |
42
|
|
|
{ |
43
|
29 |
|
array_set_value($this->elements, $key, $value); |
44
|
29 |
|
} |
45
|
|
|
|
46
|
29 |
|
public function add(array $elements): void |
47
|
|
|
{ |
48
|
29 |
|
foreach($elements as $key => $element) { |
49
|
28 |
|
$this->set($key, $element); |
50
|
|
|
} |
51
|
29 |
|
} |
52
|
|
|
|
53
|
1 |
|
public function merge(array $elements): void |
54
|
|
|
{ |
55
|
1 |
|
$this->elements = array_merge($this->elements, $elements); |
56
|
1 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @throws PropertyNotAccessibleException |
60
|
|
|
*/ |
61
|
8 |
|
public function get($key, $default = null) |
62
|
|
|
{ |
63
|
8 |
|
return array_get_value($this->elements, $key, $default); |
64
|
|
|
} |
65
|
|
|
|
66
|
1 |
|
public function replace(array $elements): void |
67
|
|
|
{ |
68
|
1 |
|
$this->clear(); |
69
|
1 |
|
$this->add($elements); |
70
|
1 |
|
} |
71
|
|
|
|
72
|
2 |
|
public function removeKey($key) |
73
|
|
|
{ |
74
|
2 |
|
return array_remove_key($this->elements, $key, false); |
75
|
|
|
} |
76
|
|
|
|
77
|
1 |
|
public function removeValue($element) |
78
|
|
|
{ |
79
|
1 |
|
return array_remove_value($this->elements, $element); |
80
|
|
|
} |
81
|
|
|
|
82
|
1 |
|
public function slice($offset, $length = null): array |
83
|
|
|
{ |
84
|
1 |
|
return \array_slice($this->elements, $offset, $length, true); |
85
|
|
|
} |
86
|
|
|
|
87
|
2 |
|
public function clear(): void |
88
|
|
|
{ |
89
|
2 |
|
$this->elements = []; |
90
|
2 |
|
} |
91
|
|
|
|
92
|
3 |
|
public function all(): array |
93
|
|
|
{ |
94
|
3 |
|
return $this->elements; |
95
|
|
|
} |
96
|
|
|
|
97
|
1 |
|
public function filter(\Closure $function) |
98
|
|
|
{ |
99
|
1 |
|
return \array_filter($this->elements, $function, ARRAY_FILTER_USE_BOTH); |
100
|
|
|
} |
101
|
|
|
|
102
|
1 |
|
public function map(\Closure $function) |
103
|
|
|
{ |
104
|
1 |
|
return \array_map($function, $this->elements); |
105
|
|
|
} |
106
|
|
|
|
107
|
2 |
|
public function keyOrValueExists($keyOrValue): bool |
108
|
|
|
{ |
109
|
2 |
|
if ($this->keyExists($keyOrValue)) { |
110
|
1 |
|
return true; |
111
|
|
|
} |
112
|
|
|
|
113
|
2 |
|
return $this->valueExists($keyOrValue); |
114
|
|
|
} |
115
|
|
|
|
116
|
1 |
|
public function keyOrValueDoesNotExists($keyOrValue): bool |
117
|
|
|
{ |
118
|
1 |
|
return false === $this->keyOrValueExists($keyOrValue); |
119
|
|
|
} |
120
|
|
|
|
121
|
10 |
|
public function keyExists($key): bool |
122
|
|
|
{ |
123
|
10 |
|
return array_key_exists($this->elements, $key); |
124
|
|
|
} |
125
|
|
|
|
126
|
3 |
|
public function keyDoesNotExists($key): bool |
127
|
|
|
{ |
128
|
3 |
|
return false === $this->keyExists($key); |
129
|
|
|
} |
130
|
|
|
|
131
|
4 |
|
public function valueExists($value): bool |
132
|
|
|
{ |
133
|
4 |
|
return empty(array_deep_search($this->elements, $value)) ? false : true; |
134
|
|
|
} |
135
|
|
|
|
136
|
1 |
|
public function valueDoesNotExists($value): bool |
137
|
|
|
{ |
138
|
1 |
|
return false === $this->valueExists($value); |
139
|
|
|
} |
140
|
|
|
|
141
|
1 |
|
public function valueIsNull($key): bool |
142
|
|
|
{ |
143
|
1 |
|
return null === $this->get($key); |
144
|
|
|
} |
145
|
|
|
|
146
|
1 |
|
public function valueIsNotNull($key): bool |
147
|
|
|
{ |
148
|
1 |
|
return false === $this->valueIsNull($key); |
149
|
|
|
} |
150
|
|
|
|
151
|
1 |
|
public function isEmpty(): bool |
152
|
|
|
{ |
153
|
1 |
|
return empty($this->elements); |
154
|
|
|
} |
155
|
|
|
|
156
|
1 |
|
public function __toString(): string |
157
|
|
|
{ |
158
|
1 |
|
return __CLASS__ . '#' . $this->getHash(); |
159
|
|
|
} |
160
|
|
|
|
161
|
2 |
|
public function getHash(): string |
162
|
|
|
{ |
163
|
2 |
|
return md5($this->serialize()); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @return string |
168
|
|
|
*/ |
169
|
1 |
|
public function getSplHash(): string |
170
|
|
|
{ |
171
|
1 |
|
return \spl_object_hash($this); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
|
175
|
1 |
|
public function getIterator() |
176
|
|
|
{ |
177
|
1 |
|
return new \ArrayIterator($this->elements); |
178
|
|
|
} |
179
|
|
|
|
180
|
1 |
|
public function offsetExists($offset) |
181
|
|
|
{ |
182
|
1 |
|
return $this->keyExists($offset); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @throws PropertyNotAccessibleException |
187
|
|
|
*/ |
188
|
2 |
|
public function offsetGet($offset) |
189
|
|
|
{ |
190
|
2 |
|
return $this->get($offset); |
191
|
|
|
} |
192
|
|
|
|
193
|
1 |
|
public function offsetSet($offset, $value) |
194
|
|
|
{ |
195
|
1 |
|
$this->set($offset, $value); |
196
|
1 |
|
} |
197
|
|
|
|
198
|
1 |
|
public function offsetUnset($offset) |
199
|
|
|
{ |
200
|
1 |
|
$this->removeKey($offset); |
201
|
1 |
|
} |
202
|
|
|
|
203
|
1 |
|
public function count(): int |
204
|
|
|
{ |
205
|
1 |
|
return \count($this->elements); |
206
|
|
|
} |
207
|
|
|
|
208
|
3 |
|
public function serialize(): string |
209
|
|
|
{ |
210
|
3 |
|
return \serialize($this->elements); |
211
|
|
|
} |
212
|
|
|
|
213
|
1 |
|
public function unserialize($value): void |
214
|
|
|
{ |
215
|
1 |
|
$this->elements = \unserialize($value); |
216
|
1 |
|
} |
217
|
|
|
} |
218
|
|
|
|