1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AlgoWeb\ODataMetadata; |
4
|
|
|
|
5
|
|
|
class BidirectionalMap |
6
|
|
|
{ |
7
|
|
|
private $keyToValue = []; |
8
|
|
|
private $valueToKey = []; |
9
|
|
|
|
10
|
|
|
public function reset() |
11
|
|
|
{ |
12
|
|
|
$this->keyToValue = []; |
13
|
|
|
$this->valueToKey = []; |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
public function hasKey($key) |
17
|
|
|
{ |
18
|
|
|
return isset($this->keyToValue[$key]); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function hasValue($value) |
22
|
|
|
{ |
23
|
|
|
return isset($this->valueToKey[$value]); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function getKey($value) |
|
|
|
|
27
|
|
|
{ |
28
|
|
|
if ($this->hasValue($value)) { |
29
|
|
|
return $this->valueToKey[$value]; |
30
|
|
|
} |
31
|
|
|
return null; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function getValue($key) |
|
|
|
|
35
|
|
|
{ |
36
|
|
|
if ($this->hasKey($key)) { |
37
|
|
|
return $this->keyToValue[$key]; |
38
|
|
|
} |
39
|
|
|
return null; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function getAllKeys() |
43
|
|
|
{ |
44
|
|
|
if (0 !== count($this->keyToValue)) { |
45
|
|
|
return array_keys($this->keyToValue); |
46
|
|
|
} |
47
|
|
|
return $this->keyToValue; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function getAllValues() |
51
|
|
|
{ |
52
|
|
|
if (0 !== count($this->valueToKey)) { |
53
|
|
|
return array_keys($this->valueToKey); |
54
|
|
|
} |
55
|
|
|
return $this->valueToKey; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function putAll($array) |
59
|
|
|
{ |
60
|
|
|
foreach ($array as $key => $value) { |
61
|
|
|
$this->put($key, $value); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function put($key, $value) |
66
|
|
|
{ |
67
|
|
|
if ($this->hasKey($key)) { |
68
|
|
|
$this->removeKey($key); |
69
|
|
|
} |
70
|
|
|
if ($this->hasValue($value)) { |
71
|
|
|
$this->removeValue($value); |
72
|
|
|
} |
73
|
|
|
$this->keyToValue[$key] = $value; |
74
|
|
|
$this->valueToKey[$value] = $key; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function removeKey($key) |
|
|
|
|
78
|
|
|
{ |
79
|
|
|
if (!$this->hasKey($key)) { |
80
|
|
|
return null; |
81
|
|
|
} |
82
|
|
|
unset($this->valueToKey[$this->keyToValue[$key]]); |
83
|
|
|
$v = $this->keyToValue[$key]; |
|
|
|
|
84
|
|
|
unset($this->keyToValue[$key]); |
85
|
|
|
return $v; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function removeValue($value) |
|
|
|
|
89
|
|
|
{ |
90
|
|
|
if (!$this->hasValue($value)) { |
91
|
|
|
return null; |
92
|
|
|
} |
93
|
|
|
unset($this->keyToValue[$this->valueToKey[$value]]); |
94
|
|
|
$k = $this->valueToKey[$value]; |
|
|
|
|
95
|
|
|
unset($this->valueToKey[$k]); |
96
|
|
|
return $k; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@return
annotation as described here.