-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAbstractMemory.php
146 lines (135 loc) · 3.87 KB
/
AbstractMemory.php
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
namespace CompressJson\Memory;
use CompressJson\Core\Utils;
use CompressJson\Core\Value;
use CompressJson\DataTypes\JsonBoolean;
use CompressJson\DataTypes\JsonNumber;
use CompressJson\DataTypes\JsonString;
use CompressJson\Exception\UnsupportedDataTypeException;
abstract class AbstractMemory
{
// Items: key -> value
protected StoreInterface $store;
// Items: value -> key
protected CacheInterface $cache;
// key increment counter
protected int $keyCount = 0;
public function __construct(StoreInterface $store, CacheInterface $cache)
{
$this->store = $store;
$this->cache = $cache;
}
protected function getNextKeyCount(): int {
return $this->keyCount++;
}
/**
* @return string[]
*/
public function memToStringValues(): array
{
$arr = $this->store->toArray();
return array_map(
fn($el) => (string) $el,
$arr
);
}
public function getValueKey(Value $value): string
{
if ($this->cache->hasValue($value)) {
return $this->cache->getValue($value);
}
$id = $this->getNextKeyCount();
$key = JsonNumber::num_to_s($id);
$this->store->add($value);
$this->cache->setValue($value, new Value($key));
return $key;
}
/**
* @param $keys string[]
*/
public function getSchema(array $keys): Value
{
/*
???
if (config.sort_key) {
keys.sort()
}*/
$schema = implode(',', $keys);
if ($this->cache->hasSchema($schema)) {
return $this->cache->getSchema($schema);
}
$keyId = new Value($this->addValue($keys, null));
$this->cache->setSchema($schema, $keyId);
return $keyId;
}
/**
* @param $data object|string|array|bool|int|null|float
* @param $parent array|object|null
* @return Value
* @throws UnsupportedDataTypeException
*/
public function addValue(mixed $data, mixed $parent): string
{
if ($data === null) {
return '';
}
else if (is_object($data) || Utils::isAssocArray($data)) {
$dataArray = $data;
if (is_object($data)) {
$dataArray = get_object_vars($data);
}
$keys = array_keys($dataArray);
if (count($keys) === 0) {
return $this->getValueKey(
new Value('o|')
);
}
$acc = 'o';
$keyId = $this->getSchema($keys);
$acc .= '|' . $keyId;
foreach ($keys as $key) {
$item = $dataArray[$key];
$v = $this->addValue($item, $data);
$acc .= '|' . $v;
}
return $this->getValueKey(
new Value($acc)
);
}
else if (is_array($data)) {
$acc = 'a';
foreach ($data as $item) {
$key = $item === null ? '_' : $this->addValue($item, $data);
$acc .= '|' . $key;
}
if ($acc === 'a') {
$acc = 'a|';
}
return $this->getValueKey(
new Value($acc)
);
}
else if (is_bool($data)) {
return $this->getValueKey(
new Value(
JsonBoolean::encodeBoolean($data)
)
);
}
else if (is_string($data)) {
return $this->getValueKey(
new Value(
JsonString::encodeString($data)
)
);
}
else if (is_numeric($data)) {
return $this->getValueKey(
new Value(
JsonNumber::encodeNumber($data)
)
);
}
throw new UnsupportedDataTypeException($data);
}
}