/
Search.php
195 lines (177 loc) · 5.38 KB
/
Search.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
declare(strict_types = 1);
namespace Williamdes\MariaDBMySQLKBS;
use stdClass;
class Search
{
/**
* Loaded data
*
* @var stdClass
*/
public static $data;
/**
* Data is loaded
*
* @var bool
*/
public static $loaded = false;
public const ANY = -1;
public const MYSQL = 1;
public const MARIADB = 2;
public const AURORA_MYSQL = 3;
public const DS = DIRECTORY_SEPARATOR;
/**
* The directory where the data is located
*
* @var string
*/
public static $DATA_DIR = __DIR__ . self::DS . '..' . self::DS . 'dist' . self::DS;
/**
* Load data from disk
*
* @return void
* @throws KBException
*/
public static function loadData(): void
{
if (Search::$loaded === false) {
$filePath = Search::$DATA_DIR . 'merged-ultraslim.json';
if (! is_file($filePath)) {
throw new KBException($filePath . ' does not exist !');
}
$contents = file_get_contents($filePath);
if ($contents === false) {
throw new KBException($filePath . ' does not exist !');
}
$decodedData = json_decode($contents);
if ($decodedData instanceof stdClass) {
Search::$data = $decodedData;
Search::$loaded = true;
return;
}
throw new KBException($filePath . ' could not be JSON decoded !');
}
}
/**
* Load test data
*
* @param SlimData $slimData The SlimData object
* @return void
*/
public static function loadTestData(SlimData $slimData): void
{
$decodedData = json_decode((string) json_encode($slimData));
if ($decodedData instanceof stdClass) {
Search::$data = $decodedData;
Search::$loaded = true;
}
}
/**
* get the first link to doc available
*
* @param string $name Name of variable
* @param int $type (optional) Type of link Search::MYSQL/Search::MARIADB/Search::ANY
* @return string
* @throws KBException
*/
public static function getByName(string $name, int $type = Search::ANY): string
{
self::loadData();
$kbEntries = self::getVariable($name);
if (isset($kbEntries->a)) {
foreach ($kbEntries->a as $kbEntry) {
$urlEnd = isset($kbEntry->a) ? '#' . $kbEntry->a : '';
if ($type === Search::ANY) {
return Search::$data->urls[$kbEntry->u] . $urlEnd;
} elseif ($type === Search::MYSQL) {
if ($kbEntry->t === Search::MYSQL) {
return Search::$data->urls[$kbEntry->u] . $urlEnd;
}
if ($kbEntry->t === Search::AURORA_MYSQL) {
return Search::$data->urls[$kbEntry->u] . $urlEnd;
}
} elseif ($type === Search::MARIADB) {
if ($kbEntry->t === Search::MARIADB) {
return Search::$data->urls[$kbEntry->u] . $urlEnd;
}
} elseif ($type === Search::AURORA_MYSQL) {
if ($kbEntry->t === Search::AURORA_MYSQL) {
return Search::$data->urls[$kbEntry->u] . $urlEnd;
}
}
}
}
throw new KBException($name . ' does not exist for this type of documentation !');
}
/**
* Get a variable
*
* @param string $name Name of variable
* @return stdClass
* @throws KBException
*/
public static function getVariable(string $name): stdClass
{
self::loadData();
if (isset(Search::$data->vars->{$name})) {
return Search::$data->vars->{$name};
} else {
throw new KBException($name . ' does not exist !');
}
}
/**
* get the type of the variable
*
* @param string $name Name of variable
* @return string
* @throws KBException
*/
public static function getVariableType(string $name): string
{
self::loadData();
$kbEntry = self::getVariable($name);
if (isset($kbEntry->t)) {
return Search::$data->varTypes->{$kbEntry->t};
} else {
throw new KBException($name . ' does have a known type !');
}
}
/**
* Return the list of static variables
*
* @return array<int,string>
*/
public static function getStaticVariables(): array
{
return self::getVariablesWithDynamic(false);
}
/**
* Return the list of dynamic variables
*
* @return array<int,string>
*/
public static function getDynamicVariables(): array
{
return self::getVariablesWithDynamic(true);
}
/**
* Return the list of variables having dynamic = $dynamic
*
* @param bool $dynamic dynamic=true/dynamic=false
* @return array<int,string>
*/
public static function getVariablesWithDynamic(bool $dynamic): array
{
self::loadData();
$staticVars = [];
foreach (Search::$data->vars as $name => $var) {
if (isset($var->d)) {
if ($var->d === $dynamic) {
$staticVars[] = $name;
}
}
}
return $staticVars;
}
}