-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeyValueStore.php
More file actions
113 lines (88 loc) · 3.22 KB
/
Copy pathKeyValueStore.php
File metadata and controls
113 lines (88 loc) · 3.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
declare(strict_types=1);
namespace Echron\DataTypes;
use Echron\DataTypes\Exception\NotInCollectionException;
use Echron\DataTypes\Exception\ObjectAlreadyInCollectionException;
use Echron\Tools\Normalize\NormalizeConfig;
use Echron\Tools\Normalize\Normalizer;
class KeyValueStore
{
private array $hashMap = [];
private array $reversedHashMap = [];
private array $cachedNormalizedKeys = [];
private ?NormalizeConfig $normalizeConfig;
public function __construct(NormalizeConfig $normalizeConfig = null, bool $skipNormalizer = false)
{
if (\is_null($normalizeConfig) && !$skipNormalizer) {
$normalizeConfig = new NormalizeConfig();
}
$this->normalizeConfig = $normalizeConfig;
}
protected function normalizeKey(string $key): string
{
if (!\is_null($this->normalizeConfig)) {
if (isset($this->cachedNormalizedKeys[$key])) {
return $this->cachedNormalizedKeys[$key];
}
$normalizedKey = Normalizer::normalize($key, $this->normalizeConfig);
$this->cachedNormalizedKeys[$key] = $normalizedKey;
$key = $normalizedKey;
}
return $key;
}
public function add(string $key, mixed $value, bool $overwriteIfExist = false): void
{
$normalizedKey = $this->normalizeKey($key);
if (!$overwriteIfExist && \array_key_exists($normalizedKey, $this->hashMap)) {
throw new ObjectAlreadyInCollectionException('There is already a value with key "' . $normalizedKey . '"');
}
$this->reversedHashMap[$value] = $key;
$this->hashMap[$normalizedKey] = $value;
}
public function getValueByKey(string $key): mixed
{
$key = $this->normalizeKey($key);
//TODO: isset or key_exists?
// if (!\key_exists($key, $this->hashMap)) {
if (!isset($this->hashMap[$key])) {
throw new NotInCollectionException('Key "' . $key . '" does not exist');
}
return $this->hashMap[$key];
}
public function getKeyByValue(mixed $value): string
{
//TODO: isset or key_exists?
// if (!\key_exists($value, $this->reversedHashMap)) {
if (!isset($this->reversedHashMap[$value])) {
throw new NotInCollectionException('Value "' . $value . '" does not exist (' . \implode(', ', $this->reversedHashMap) . ')');
}
return $this->reversedHashMap[$value];
}
public function removeByKey(string $key): void
{
$key = $this->normalizeKey($key);
$value = $this->getValueByKey($key);
unset($this->hashMap[$key]);
unset($this->reversedHashMap[$value]);
}
public function removeByValue(mixed $value): void
{
$key = $this->getKeyByValue($value);
$key = $this->normalizeKey($key);
unset($this->hashMap[$key]);
unset($this->reversedHashMap[$value]);
}
/**
* @return string[]
*/
public function getKeys(): array
{
return \array_values($this->reversedHashMap);
}
public function hasKey(string $key): bool
{
$key = $this->normalizeKey($key);
return isset($this->hashMap[$key]);
// \key_exists($key, $this->hashMap);
}
}