|
1
|
|
|
<?php |
|
2
|
|
|
namespace GenericCollections\Abstracts; |
|
3
|
|
|
|
|
4
|
|
|
use GenericCollections\Collection; |
|
5
|
|
|
use GenericCollections\Exceptions\ContainerNotUniqueMemberException; |
|
6
|
|
|
use GenericCollections\Exceptions\InvalidDefaultValueTypeException; |
|
7
|
|
|
use GenericCollections\Exceptions\InvalidKeyTypeException; |
|
8
|
|
|
use GenericCollections\Exceptions\InvalidValueTypeException; |
|
9
|
|
|
use GenericCollections\Interfaces\MapInterface; |
|
10
|
|
|
use GenericCollections\Internal\DataArray; |
|
11
|
|
|
use GenericCollections\Set; |
|
12
|
|
|
|
|
13
|
|
|
abstract class AbstractMap extends DataArray implements MapInterface, \ArrayAccess |
|
14
|
|
|
{ |
|
15
|
23 |
|
public function containsKey($key) |
|
16
|
|
|
{ |
|
17
|
23 |
|
return $this->checkKeyType($key) && array_key_exists($key, $this->data); |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
3 |
|
public function containsValue($value) |
|
21
|
|
|
{ |
|
22
|
3 |
|
return in_array($value, $this->data, $this->optionComparisonIsIdentical()); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
23 |
|
public function get($key) |
|
26
|
|
|
{ |
|
27
|
23 |
|
return $this->containsKey($key) ? $this->data[$key] : null; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
1 |
|
public function getOrDefault($key, $default) |
|
31
|
|
|
{ |
|
32
|
1 |
|
if ($this->containsKey($key)) { |
|
33
|
1 |
|
return $this->data[$key]; |
|
34
|
|
|
} |
|
35
|
1 |
|
if (null !== $default && ! $this->checkValueType($default)) { |
|
36
|
1 |
|
throw new InvalidDefaultValueTypeException('map', $this->getValueType(), get_class($this)); |
|
37
|
|
|
} |
|
38
|
1 |
|
return $default; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
2 |
|
public function keys() |
|
42
|
|
|
{ |
|
43
|
2 |
|
return array_keys($this->data); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
25 |
|
public function put($key, $value) |
|
47
|
|
|
{ |
|
48
|
25 |
|
if (! $this->checkKeyType($key)) { |
|
49
|
1 |
|
throw new InvalidKeyTypeException('map', $this->getKeyType(), get_class($this)); |
|
50
|
|
|
} |
|
51
|
24 |
|
if (! $this->checkValueType($value)) { |
|
52
|
2 |
|
throw new InvalidValueTypeException('map', $this->getValueType(), get_class($this)); |
|
53
|
|
|
} |
|
54
|
23 |
|
if ($this->optionUniqueValues() && $this->containsValue($value)) { |
|
55
|
1 |
|
throw new ContainerNotUniqueMemberException('map', get_class($this)); |
|
56
|
|
|
} |
|
57
|
23 |
|
$previous = $this->get($key); |
|
58
|
23 |
|
$this->data[$key] = $value; |
|
59
|
23 |
|
return $previous; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
28 |
|
public function putAll(array $values) |
|
63
|
|
|
{ |
|
64
|
28 |
|
foreach ($values as $key => $value) { |
|
65
|
17 |
|
$this->put($key, $value); |
|
66
|
|
|
} |
|
67
|
28 |
|
} |
|
68
|
|
|
|
|
69
|
1 |
|
public function putIfAbsent($key, $value) |
|
70
|
|
|
{ |
|
71
|
1 |
|
$current = $this->get($key); |
|
72
|
1 |
|
if (null !== $current) { |
|
73
|
1 |
|
return $current; |
|
74
|
|
|
} |
|
75
|
1 |
|
$this->put($key, $value); |
|
76
|
1 |
|
return null; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
2 |
|
public function remove($key) |
|
80
|
|
|
{ |
|
81
|
2 |
|
$previous = $this->get($key); |
|
82
|
2 |
|
if ($previous !== null) { |
|
83
|
2 |
|
unset($this->data[$key]); |
|
84
|
|
|
} |
|
85
|
2 |
|
return $previous; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
1 |
|
public function removeExact($key, $value) |
|
89
|
|
|
{ |
|
90
|
1 |
|
$changed = false; |
|
91
|
1 |
|
$previous = $this->get($key); |
|
92
|
1 |
|
$isequal = ($this->optionComparisonIsIdentical()) ? ($previous === $value) : ($previous == $value); |
|
93
|
1 |
|
if ($isequal) { |
|
94
|
1 |
|
unset($this->data[$key]); |
|
95
|
1 |
|
$changed = true; |
|
96
|
|
|
} |
|
97
|
1 |
|
return $changed; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
1 |
|
public function replace($key, $value) |
|
101
|
|
|
{ |
|
102
|
1 |
|
return ($this->containsKey($key)) ? $this->put($key, $value) : null; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
2 |
|
public function replaceExact($key, $current, $replacement) |
|
106
|
|
|
{ |
|
107
|
2 |
|
if (! $this->containsKey($key)) { |
|
108
|
1 |
|
return false; |
|
109
|
|
|
} |
|
110
|
2 |
|
$previous = $this->get($key); |
|
111
|
2 |
|
$isequal = ($this->optionComparisonIsIdentical()) ? ($previous === $current) : ($previous == $current); |
|
112
|
2 |
|
if ($isequal) { |
|
113
|
2 |
|
$this->put($key, $replacement); |
|
114
|
2 |
|
return true; |
|
115
|
|
|
} |
|
116
|
2 |
|
return false; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
1 |
|
public function keysSet() |
|
120
|
|
|
{ |
|
121
|
1 |
|
return new Set($this->getKeyType(), $this->keys()); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
1 |
|
public function valuesCollection() |
|
125
|
|
|
{ |
|
126
|
1 |
|
return new Collection($this->getValueType(), $this->toArray()); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/* |
|
130
|
|
|
* Implementations from \ArrayAccess |
|
131
|
|
|
*/ |
|
132
|
|
|
|
|
133
|
|
|
/** @inheritdoc */ |
|
134
|
3 |
|
public function offsetExists($offset) |
|
135
|
|
|
{ |
|
136
|
3 |
|
return $this->containsKey($offset); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** @inheritdoc */ |
|
140
|
1 |
|
public function offsetGet($offset) |
|
141
|
|
|
{ |
|
142
|
1 |
|
return $this->get($offset); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** @inheritdoc */ |
|
146
|
2 |
|
public function offsetSet($offset, $value) |
|
147
|
|
|
{ |
|
148
|
2 |
|
$this->put($offset, $value); |
|
149
|
1 |
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** @inheritdoc */ |
|
152
|
1 |
|
public function offsetUnset($offset) |
|
153
|
|
|
{ |
|
154
|
1 |
|
$this->remove($offset); |
|
155
|
1 |
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|