-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathSchema.php
287 lines (263 loc) · 8.52 KB
/
Schema.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
<?php
/**
* © Teclib' and contributors.
*
* This file is part of GLPI inventory format.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Glpi\Inventory;
use Exception;
use RuntimeException;
use Swaggest\JsonSchema\Context;
/**
* Handle inventory JSON Schema
*
* @author Johan Cwiklinski <[email protected]>
*/
class Schema
{
/** @var array<string, array<int, string>> */
private array $patterns;
/** @var array<string, array<string, string>> */
private array $extra_properties = [];
/** @var array<string, array<string, array<string, string>>> */
private array $extra_sub_properties = [];
/** @var array<string> */
private array $extra_itemtypes = [];
/** @var bool */
private bool $strict_schema = true;
/**
* Add new supported item types
*
* @param array<string> $itemtypes
* @return $this
*/
public function setExtraItemtypes(array $itemtypes): self
{
$this->extra_itemtypes = $itemtypes;
return $this;
}
/**
* Build (extended) JSON schema
*
* @return object
*/
public function build(): object
{
$string = file_get_contents($this->getPath());
if ($string === false) {
throw new RuntimeException('Unable to read schema file');
}
$schema = json_decode($string);
$known_itemtypes = [];
preg_match('/\^\((.+)\)\$/', $schema->properties->itemtype->pattern, $known_itemtypes);
if (isset($known_itemtypes[1])) {
$known_itemtypes = explode('|', $known_itemtypes[1]);
foreach ($this->extra_itemtypes as $extra_itemtype) {
if (!in_array($extra_itemtype, $known_itemtypes)) {
$known_itemtypes[] = addslashes($extra_itemtype);
}
}
$schema->properties->itemtype->pattern = sprintf(
'^(%s)$',
implode('|', $known_itemtypes)
);
}
$properties = $schema->properties->content->properties;
foreach ($this->extra_properties as $extra_property => $extra_config) {
if (!property_exists($properties, $extra_property)) {
$properties->$extra_property = json_decode((string)json_encode($extra_config));
} else {
trigger_error(
sprintf('Property %1$s already exists in schema.', $extra_property),
E_USER_WARNING
);
}
}
foreach ($this->extra_sub_properties as $extra_sub_property => $extra_sub_config) {
if (property_exists($properties, $extra_sub_property)) {
foreach ($extra_sub_config as $subprop => $subconfig) {
$type = $properties->$extra_sub_property->type;
switch ($type) {
case 'array':
if (!property_exists($properties->$extra_sub_property->items->properties, $subprop)) {
$properties->$extra_sub_property->items->properties->$subprop =
json_decode((string)json_encode($subconfig));
} else {
trigger_error(
sprintf('Property %1$s already exists in schema.', $subprop),
E_USER_WARNING
);
}
break;
case 'object':
if (!property_exists($properties->$extra_sub_property->properties, $subprop)) {
$properties->$extra_sub_property->properties->$subprop =
json_decode((string)json_encode($subconfig));
} else {
trigger_error(
sprintf(
'Property %1$s/%2$s already exists in schema.',
$extra_sub_property,
$subprop
),
E_USER_WARNING
);
}
break;
default:
trigger_error('Unknown type ' . $type, E_USER_WARNING);
}
}
} else {
trigger_error(
sprintf('Property %1$s does not exists in schema.', $extra_sub_property),
E_USER_WARNING
);
}
}
if ($this->strict_schema === false) {
$this->buildFlexibleSchema($schema->properties->content);
}
return $schema;
}
/**
* Get path to schema
*
* @return string
*/
public function getPath(): string
{
$schema_path = realpath(__DIR__ . '/../../inventory.schema.json');
if ($schema_path === false) {
throw new RuntimeException('Schema file not found!');
}
return $schema_path;
}
/**
* Add extra properties to schema
*
* @param array<string, array<string, string>> $properties
* @return $this
*/
public function setExtraProperties(array $properties): self
{
$this->extra_properties = $properties;
return $this;
}
/**
* Add extra sub-properties to schema
*
* @param array<string, array<string, array<string, string>>> $properties
* @return $this
*/
public function setExtraSubProperties(array $properties): self
{
$this->extra_sub_properties = $properties;
return $this;
}
/**
* Load schema patterns that will be used to validate
*
* @return void
*/
public function loadPatterns(): void
{
$string = file_get_contents($this->getPath());
if ($string === false) {
throw new RuntimeException('Unable to read schema file');
}
$json = json_decode($string, true);
$this->patterns['networks_types'] = explode(
'|',
str_replace(
['^(', ')$'],
['', ''],
$json['properties']['content']['properties']['networks']['items']['properties']['type']['pattern']
)
);
}
/**
* Get available schema patterns
*
* @return array<string, array<int, string>>
*/
public function getPatterns(): array
{
return $this->patterns;
}
/**
* Set schema validation strict (no additional properties allowed anywhere)
*
* @return self
*/
public function setStrict(): self
{
$this->strict_schema = true;
return $this;
}
/**
* Set schema validation strict (no additional properties allowed anywhere)
*
* @return self
*/
public function setFlexible(): self
{
$this->strict_schema = false;
return $this;
}
/**
* Build schema flexible (remove all additionalProperties)
*
* @param mixed $schemapart
*
* @return void
*/
private function buildFlexibleSchema(&$schemapart)
{
foreach ($schemapart as $key => $value) {
if (is_object($value) || is_array($value)) {
$this->buildFlexibleSchema($value);
} else {
if ($key == 'additionalProperties') {
unset($schemapart->$key);
}
}
}
}
/**
* Do validation (against last schema only!)
*
* @param mixed $json Converted data to validate
*
* @return boolean
*/
public function validate($json): bool
{
try {
$schema = \Swaggest\JsonSchema\Schema::import($this->build());
$context = new Context();
$context->tolerateStrings = (!defined('TU_USER'));
$schema->in($json, $context);
return true;
} catch (Exception $e) {
throw new RuntimeException(
sprintf(
"JSON does not validate. Violations:\n%1\$s\n",
$e->getMessage()
)
);
}
}
/**
* Get current schema version
*
* @return float
*/
public function getVersion(): float
{
return $this->build()->version; //@phpstan-ignore-line: version does exist.
}
}