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