|
1
|
|
|
<?php |
|
2
|
|
|
namespace Wandu\Collection; |
|
3
|
|
|
|
|
4
|
|
|
use InvalidArgumentException; |
|
5
|
|
|
use Wandu\Collection\Contracts\MapInterface; |
|
6
|
|
|
|
|
7
|
|
|
class ArrayMap implements MapInterface |
|
8
|
|
|
{ |
|
9
|
|
|
/** @var array */ |
|
10
|
|
|
protected $items; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @param array $items |
|
14
|
|
|
*/ |
|
15
|
25 |
|
public function __construct(array $items = []) |
|
16
|
|
|
{ |
|
17
|
25 |
|
$this->items = $items; |
|
18
|
25 |
|
} |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* {@inheritdoc} |
|
22
|
|
|
*/ |
|
23
|
1 |
|
public function __toString() |
|
24
|
|
|
{ |
|
25
|
1 |
|
$string = static::class . " [\n"; |
|
26
|
1 |
|
foreach ($this->items as $key => $item) { |
|
27
|
1 |
|
$string .= " \"{$key}\" => "; |
|
28
|
1 |
|
if (is_string($item)) { |
|
29
|
1 |
|
$string .= "\"{$item}\",\n"; |
|
30
|
1 |
|
} elseif (is_scalar($item)) { |
|
31
|
1 |
|
$string .= "{$item},\n"; |
|
32
|
1 |
|
} elseif (is_null($item)) { |
|
33
|
1 |
|
$string .= "null,\n"; |
|
34
|
1 |
|
} elseif (is_array($item)) { |
|
35
|
1 |
|
$string .= "[array],\n"; |
|
36
|
1 |
|
} elseif (is_object($item)) { |
|
37
|
1 |
|
$string .= "[" . get_class($item) . "],\n"; |
|
38
|
|
|
} else { |
|
39
|
1 |
|
$string .= "[unknown],\n"; |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
1 |
|
return $string . ']'; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* {@inheritdoc} |
|
47
|
|
|
*/ |
|
48
|
|
|
public function toArray() |
|
49
|
|
|
{ |
|
50
|
10 |
|
return array_map(function ($item) { |
|
51
|
10 |
|
if (method_exists($item, 'toArray')) { |
|
52
|
1 |
|
return $item->toArray(); |
|
53
|
|
|
} |
|
54
|
10 |
|
return $item; |
|
55
|
10 |
|
}, $this->items); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* {@inheritdoc} |
|
60
|
|
|
*/ |
|
61
|
1 |
|
public function all() |
|
62
|
|
|
{ |
|
63
|
1 |
|
return $this->items; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* {@inheritdoc} |
|
68
|
|
|
*/ |
|
69
|
|
|
public function isEmpty() |
|
70
|
|
|
{ |
|
71
|
|
|
return $this->count() === 0; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* {@inheritdoc} |
|
76
|
|
|
*/ |
|
77
|
3 |
|
public function count() |
|
78
|
|
|
{ |
|
79
|
3 |
|
return count($this->items); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* {@inheritdoc} |
|
84
|
|
|
*/ |
|
85
|
1 |
|
function jsonSerialize() |
|
86
|
|
|
{ |
|
87
|
1 |
|
return $this->toArray(); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* {@inheritdoc} |
|
92
|
|
|
*/ |
|
93
|
2 |
|
public function offsetExists($offset) |
|
94
|
|
|
{ |
|
95
|
2 |
|
return isset($this->items[$offset]); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* {@inheritdoc} |
|
100
|
|
|
*/ |
|
101
|
4 |
|
public function offsetGet($offset) |
|
102
|
|
|
{ |
|
103
|
4 |
|
return $this->get($offset); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* {@inheritdoc} |
|
108
|
|
|
*/ |
|
109
|
2 |
|
public function offsetSet($offset, $value) |
|
110
|
|
|
{ |
|
111
|
2 |
|
$this->assertIsNotNull($offset, __METHOD__); |
|
112
|
2 |
|
$this->set($offset, $value); |
|
113
|
2 |
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* {@inheritdoc} |
|
117
|
|
|
*/ |
|
118
|
1 |
|
public function offsetUnset($offset) |
|
119
|
|
|
{ |
|
120
|
1 |
|
$this->remove($offset); |
|
121
|
1 |
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* {@inheritdoc} |
|
125
|
|
|
*/ |
|
126
|
1 |
|
public function getIterator() |
|
127
|
|
|
{ |
|
128
|
1 |
|
foreach ($this->items as $key => $value) { |
|
129
|
1 |
|
yield $key => $value; |
|
130
|
|
|
} |
|
131
|
1 |
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* {@inheritdoc} |
|
135
|
|
|
*/ |
|
136
|
1 |
|
public function serialize() |
|
137
|
|
|
{ |
|
138
|
1 |
|
return serialize($this->items); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* {@inheritdoc} |
|
143
|
|
|
*/ |
|
144
|
1 |
|
public function unserialize($serialized) |
|
145
|
|
|
{ |
|
146
|
1 |
|
$this->items = unserialize($serialized); |
|
147
|
1 |
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* {@inheritdoc} |
|
151
|
|
|
*/ |
|
152
|
|
|
public function clear() |
|
153
|
|
|
{ |
|
154
|
|
|
$this->items = []; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* {@inheritdoc} |
|
159
|
|
|
*/ |
|
160
|
1 |
|
public function contains(...$values) |
|
161
|
|
|
{ |
|
162
|
1 |
|
foreach ($values as $value) { |
|
163
|
1 |
|
if (!in_array($value, $this->items, true)) { |
|
164
|
1 |
|
return false; |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
1 |
|
return true; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* {@inheritdoc} |
|
172
|
|
|
*/ |
|
173
|
5 |
|
public function get($key, $default = null) |
|
174
|
|
|
{ |
|
175
|
5 |
|
return array_key_exists($key, $this->items) ? $this->items[$key] : $default; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* {@inheritdoc} |
|
180
|
|
|
*/ |
|
181
|
4 |
|
public function set($key, $value) |
|
182
|
|
|
{ |
|
183
|
4 |
|
$this->assertIsNotNull($key, __METHOD__); |
|
184
|
4 |
|
$this->items[$key] = $value; |
|
185
|
4 |
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* {@inheritdoc} |
|
189
|
|
|
*/ |
|
190
|
2 |
|
public function remove(...$keys) |
|
191
|
|
|
{ |
|
192
|
2 |
|
foreach ($keys as $key) { |
|
193
|
2 |
|
unset($this->items[$key]); |
|
194
|
|
|
} |
|
195
|
2 |
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* {@inheritdoc} |
|
199
|
|
|
*/ |
|
200
|
1 |
|
public function has(...$keys) |
|
201
|
|
|
{ |
|
202
|
1 |
|
foreach ($keys as $key) { |
|
203
|
1 |
|
if (!array_key_exists($key, $this->items)) { |
|
204
|
1 |
|
return false; |
|
205
|
|
|
} |
|
206
|
|
|
} |
|
207
|
1 |
|
return true; |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* {@inheritdoc} |
|
212
|
|
|
*/ |
|
213
|
1 |
|
public function keys() |
|
214
|
|
|
{ |
|
215
|
1 |
|
return new ArrayList(array_keys($this->items)); |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* {@inheritdoc} |
|
220
|
|
|
*/ |
|
221
|
1 |
|
public function values() |
|
222
|
|
|
{ |
|
223
|
1 |
|
return new ArrayList(array_values($this->items)); |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* {@inheritdoc} |
|
228
|
|
|
*/ |
|
229
|
2 |
|
public function map(callable $handler) |
|
230
|
|
|
{ |
|
231
|
2 |
|
$keys = array_keys($this->items); |
|
232
|
2 |
|
return new static(array_combine($keys, array_map($handler, $this->items, $keys))); |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
/** |
|
236
|
|
|
* {@inheritdoc} |
|
237
|
|
|
*/ |
|
238
|
1 |
|
public function reduce(callable $handler, $initial = null) |
|
239
|
|
|
{ |
|
240
|
1 |
|
foreach ($this->items as $key => $item) { |
|
241
|
1 |
|
$initial = $handler($initial, $item, $key); |
|
242
|
|
|
} |
|
243
|
1 |
|
return $initial; |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
/** |
|
247
|
|
|
* @param mixed $value |
|
248
|
|
|
* @param string $method |
|
249
|
|
|
* @param int $order |
|
250
|
|
|
*/ |
|
251
|
4 |
|
private function assertIsNotNull($value, $method, $order = 1) |
|
252
|
|
|
{ |
|
253
|
4 |
|
if (!isset($value)) { |
|
254
|
1 |
|
throw new InvalidArgumentException("Argument {$order} passed to {$method} must be not null"); |
|
255
|
|
|
} |
|
256
|
4 |
|
} |
|
257
|
|
|
} |
|
258
|
|
|
|