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