Completed
Push — master ( c4431a...dfa02b )
by Zoltán
02:49
created

HashMap::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
ccs 5
cts 5
cp 1
rs 9.4285
cc 3
eloc 3
nc 2
nop 1
crap 3
1
<?php namespace BuildR\Collection\Map;
2
3
use BuildR\Collection\ArrayList\ArrayList;
4
use BuildR\Collection\Collection\AbstractCollection;
5
use BuildR\Collection\Collection\FilterableCollectionTrait;
6
use BuildR\Collection\Exception\MapException;
7
use BuildR\Collection\Set\HashSet;
8
9
/**
10
 * MapInterface implementation
11
 *
12
 * BuildR PHP Framework
13
 *
14
 * @author Zoltán Borsos <[email protected]>
15
 * @package Collection
16
 * @subpackage Map
17
 *
18
 * @copyright    Copyright 2015, Zoltán Borsos.
19
 * @license      https://github.com/Zolli/BuildR/blob/master/LICENSE.md
20
 * @link         https://github.com/Zolli/BuildR
21
 */
22
class HashMap extends AbstractCollection implements MapInterface {
23
24
    use FilterableCollectionTrait;
25
26
    /**
27
     * HashMap constructor.
28
     *
29
     * @param array|null $content
30
     */
31 58
    public function __construct($content = NULL) {
32 58
        if($content !== NULL && is_array($content)) {
33 6
            $this->putAll($content);
34 6
        }
35 58
    }
36
37
    /**
38
     * {@inheritDoc}
39
     */
40 54
    public function containsKey($key) {
41 54
        $this->checkKeyType($key);
42
43 54
        return isset($this->data[$key]);
44
    }
45
46
    /**
47
     * {@inheritDoc}
48
     */
49 4
    public function containsValue($value) {
50 4
        return (array_search($value, $this->data, TRUE) === FALSE) ? FALSE : TRUE;
51
    }
52
53
    /**
54
     * {@inheritDoc}
55
     */
56 24
    public function contains($key, $value) {
57 24
        if(isset($this->data[$key]) && $this->data[$key] === $value) {
58 24
            return TRUE;
59
        }
60
61 12
        return FALSE;
62
    }
63
64
    /**
65
     * {@inheritDoc}
66
     */
67 12
    public function equals(MapInterface $map) {
68 12
        if($this->size() !== $map->size()) {
69 4
            return FALSE;
70
        }
71
72 12
        foreach($this->data as $key => $value) {
73 12
            if(!$map->contains($key, $value)) {
74 4
                return FALSE;
75
            }
76 12
        }
77
78 12
        return TRUE;
79
    }
80
81
    /**
82
     * {@inheritDoc}
83
     */
84 8
    public function each(callable $callback) {
85 8
        foreach($this->data as $key => $value) {
86 8
            call_user_func_array($callback, [$key, $value]);
87 8
        }
88 8
    }
89
90
    /**
91
     * {@inheritDoc}
92
     */
93 28 View Code Duplication
    public function get($key, $defaultValue = NULL) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
94 28
        $this->checkKeyType($key);
95
96 28
        if(!$this->containsKey($key)) {
97 4
            return $defaultValue;
98
        }
99
100 28
        return $this->data[$key];
101
    }
102
103
    /**
104
     * {@inheritDoc}
105
     */
106 4
    public function keySet() {
107 4
        return new HashSet(array_keys($this->data));
108
    }
109
110
    /**
111
     * {@inheritDoc}
112
     */
113 4
    public function valueList() {
114 4
        return new ArrayList(array_values($this->data));
115
    }
116
117
    /**
118
     * {@inheritDoc}
119
     */
120 4
    public function merge(MapInterface $map) {
121 4
        $self = $this;
122
123 4
        $map->each(function($key, $value) use($self) {
124 4
            if(!$self->containsKey($key)) {
125 4
                $self->put($key, $value);
126 4
            }
127 4
        });
128 4
    }
129
130
    /**
131
     * {@inheritDoc}
132
     */
133 54 View Code Duplication
    public function put($key, $value) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
134 54
        $this->checkKeyType($key);
135 54
        $return = NULL;
136
137 54
        if($this->containsKey($key)) {
138 4
            $return = $this->get($key);
139 4
        }
140
141 54
        $this->data[$key] = $value;
142
143 54
        return $return;
144
    }
145
146
    /**
147
     * {@inheritDoc}
148
     */
149 46
    public function putAll($map) {
150 46
        if($map instanceof MapInterface) {
151 4
            $map = $map->toArray();
152 4
        }
153
154 46
        foreach($map as $key => $value) {
155 46
            $this->put($key, $value);
156 46
        }
157 46
    }
158
159
    /**
160
     * {@inheritDoc}
161
     */
162 4 View Code Duplication
    public function putIfAbsent($key, $value) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
163 4
        $this->checkKeyType($key);
164
165 4
        if($this->containsKey($key)) {
166 4
            return $this->get($key);
167
        }
168
169 4
        $this->put($key, $value);
170
171 4
        return NULL;
172
    }
173
174
    /**
175
     * {@inheritDoc}
176
     */
177 4 View Code Duplication
    public function remove($key) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
178 4
        $this->checkKeyType($key);
179
180 4
        if($this->containsKey($key)) {
181 4
            $previousElement = $this->get($key);
182 4
            unset($this->data[$key]);
183
184 4
            return $previousElement;
185
        }
186
187 4
        return NULL;
188
    }
189
190
    /**
191
     * {@inheritDoc}
192
     */
193 4
    public function removeIf($key, $value) {
194 4
        $this->checkKeyType($key);
195
196 4
        if($this->contains($key, $value)) {
197 4
            $previousElement = $this->get($key);
198 4
            unset($this->data[$key]);
199
200 4
            return $previousElement;
201
        }
202
203 4
        return NULL;
204
    }
205
206
    /**
207
     * {@inheritDoc}
208
     */
209 4 View Code Duplication
    public function replace($key, $value) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
210 4
        $this->checkKeyType($key);
211
212 4
        if($this->containsKey($key)) {
213 4
            $previousValue = $this->get($key);
214 4
            $this->data[$key] = $value;
215
216 4
            return $previousValue;
217
        }
218
219 4
        return NULL;
220
    }
221
222
    /**
223
     * {@inheritDoc}
224
     */
225 4
    public function replaceIf($key, $oldValue, $newValue) {
226 4
        $this->checkKeyType($key);
227
228 4
        if($this->contains($key, $oldValue)) {
229 4
            $previousValue = $this->get($key);
230 4
            $this->data[$key] = $newValue;
231
232 4
            return $previousValue;
233
        }
234
235 4
        return NULL;
236
    }
237
238
    /**
239
     * Validate the given key. This map only allows scalar types
240
     * as items key. If the validation is fails, throws a MapException.
241
     *
242
     * @param mixed $key
243
     *
244
     * @return bool
245
     *
246
     * @throws \BuildR\Collection\Exception\MapException
247
     */
248 58
    protected function checkKeyType($key) {
249 58
        if(is_scalar($key)) {
250 54
            return TRUE;
251
        }
252
253 4
        throw MapException::notValidKey($key);
254
    }
255
256
}
257