1
|
|
|
<?php namespace BuildR\Collection\Map; |
2
|
|
|
|
3
|
|
|
use BuildR\Collection\ArrayList\ArrayList; |
4
|
|
|
use BuildR\Collection\Collection\AbstractCollection; |
5
|
|
|
use BuildR\Collection\Exception\MapException; |
6
|
|
|
use BuildR\Collection\Set\Set; |
7
|
|
|
|
8
|
|
|
class HashMap extends AbstractCollection implements MapInterface { |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* {@inheritDoc} |
12
|
|
|
*/ |
13
|
|
|
public function containsKey($key) { |
14
|
|
|
$this->checkKeyType($key); |
15
|
|
|
|
16
|
|
|
return isset($this->data[$key]); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* {@inheritDoc} |
21
|
|
|
*/ |
22
|
|
|
public function containsValue($value) { |
23
|
|
|
return (array_search($value, $this->data, TRUE) === FALSE) ? FALSE : TRUE; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* {@inheritDoc} |
28
|
|
|
*/ |
29
|
|
|
public function contains($key, $value) { |
30
|
|
|
if(isset($this->data[$key]) && $this->data[$key] === $value) { |
31
|
|
|
return TRUE; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
return FALSE; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* {@inheritDoc} |
39
|
|
|
*/ |
40
|
|
|
public function equals(MapInterface $map) { |
41
|
|
|
if($this->size() !== $map->size()) { |
42
|
|
|
return FALSE; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
foreach($this->data as $key => $value) { |
46
|
|
|
if(!$map->contains($key, $value)) { |
47
|
|
|
return FALSE; |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return TRUE; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritDoc} |
56
|
|
|
*/ |
57
|
|
|
public function each(callable $callback) { |
58
|
|
|
foreach($this->data as $key => $value) { |
59
|
|
|
call_user_func_array($callback, [$key, $value]); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritDoc} |
65
|
|
|
*/ |
66
|
|
View Code Duplication |
public function get($key, $defaultValue = NULL) { |
|
|
|
|
67
|
|
|
$this->checkKeyType($key); |
68
|
|
|
|
69
|
|
|
if(!$this->containsKey($key)) { |
70
|
|
|
return $defaultValue; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $this->data[$key]; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* {@inheritDoc} |
78
|
|
|
*/ |
79
|
|
|
public function keySet() { |
80
|
|
|
return new Set(array_keys($this->data)); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* {@inheritDoc} |
85
|
|
|
*/ |
86
|
|
|
public function valueList() { |
87
|
|
|
return new ArrayList(array_values($this->data)); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* {@inheritDoc} |
92
|
|
|
*/ |
93
|
|
|
public function merge(MapInterface $map) { |
94
|
|
|
$self = $this; |
95
|
|
|
|
96
|
|
|
$map->each(function($key, $value) use($self) { |
97
|
|
|
if(!$self->containsKey($key)) { |
98
|
|
|
$self->put($key, $value); |
99
|
|
|
} |
100
|
|
|
}); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* {@inheritDoc} |
105
|
|
|
*/ |
106
|
|
View Code Duplication |
public function put($key, $value) { |
|
|
|
|
107
|
|
|
$this->checkKeyType($key); |
108
|
|
|
$return = NULL; |
109
|
|
|
|
110
|
|
|
if($this->containsKey($key)) { |
111
|
|
|
$return = $this->get($key); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$this->data[$key] = $value; |
115
|
|
|
|
116
|
|
|
return $return; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* {@inheritDoc} |
121
|
|
|
*/ |
122
|
|
|
public function putAll($map) { |
123
|
|
|
if($map instanceof MapInterface) { |
124
|
|
|
$map = $map->toArray(); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
foreach($map as $key => $value) { |
128
|
|
|
$this->put($key, $value); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* {@inheritDoc} |
134
|
|
|
*/ |
135
|
|
View Code Duplication |
public function putIfAbsent($key, $value) { |
|
|
|
|
136
|
|
|
$this->checkKeyType($key); |
137
|
|
|
|
138
|
|
|
if($this->containsKey($key)) { |
139
|
|
|
return $this->get($key); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
$this->put($key, $value); |
143
|
|
|
|
144
|
|
|
return NULL; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* {@inheritDoc} |
149
|
|
|
*/ |
150
|
|
View Code Duplication |
public function remove($key) { |
|
|
|
|
151
|
|
|
$this->checkKeyType($key); |
152
|
|
|
|
153
|
|
|
if($this->containsKey($key)) { |
154
|
|
|
$previousElement = $this->get($key); |
155
|
|
|
unset($this->data[$key]); |
156
|
|
|
|
157
|
|
|
return $previousElement; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
return NULL; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* {@inheritDoc} |
165
|
|
|
*/ |
166
|
|
|
public function removeIf($key, $value) { |
167
|
|
|
$this->checkKeyType($key); |
168
|
|
|
|
169
|
|
|
if($this->contains($key, $value)) { |
170
|
|
|
$previousElement = $this->get($key); |
171
|
|
|
unset($this->data[$key]); |
172
|
|
|
|
173
|
|
|
return $previousElement; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
return NULL; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* {@inheritDoc} |
181
|
|
|
*/ |
182
|
|
View Code Duplication |
public function replace($key, $value) { |
|
|
|
|
183
|
|
|
$this->checkKeyType($key); |
184
|
|
|
|
185
|
|
|
if($this->containsKey($key)) { |
186
|
|
|
$previousValue = $this->get($key); |
187
|
|
|
$this->data[$key] = $value; |
188
|
|
|
|
189
|
|
|
return $previousValue; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
return NULL; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* {@inheritDoc} |
197
|
|
|
*/ |
198
|
|
|
public function replaceIf($key, $oldValue, $newValue) { |
199
|
|
|
$this->checkKeyType($key); |
200
|
|
|
|
201
|
|
|
if($this->contains($key, $oldValue)) { |
202
|
|
|
$previousValue = $this->get($key); |
203
|
|
|
$this->data[$key] = $newValue; |
204
|
|
|
|
205
|
|
|
return $previousValue; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
return NULL; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Validate the given key. This map only allows scalar types |
213
|
|
|
* as items key. If the validation is fails, throws a MapException. |
214
|
|
|
* |
215
|
|
|
* @param mixed $key |
216
|
|
|
* |
217
|
|
|
* @return bool |
218
|
|
|
* |
219
|
|
|
* @throws \BuildR\Collection\Exception\MapException |
220
|
|
|
*/ |
221
|
|
|
protected function checkKeyType($key) { |
222
|
|
|
if(is_scalar($key)) { |
223
|
|
|
return TRUE; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
throw MapException::notValidKey($key); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
} |
230
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.