-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
27 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,7 +20,7 @@ | |
* Converts old FusionInventory XML format to new JSON schema | ||
* for automatic inventory. | ||
* | ||
* @author Johan Cwiklinski <[email protected]> | ||
* @author Johan Cwiklinski <[email protected]> | ||
*/ | ||
class Converter | ||
{ | ||
|
@@ -36,24 +36,13 @@ class Converter | |
* XML a different steps. Used for debug only | ||
* @var array<int, mixed> | ||
*/ | ||
private array $steps; | ||
private array $steps; //@phpstan-ignore-line | ||
|
||
/** @var array<string, float> */ | ||
private array $mapping = [ | ||
'01' => 0.1 | ||
]; | ||
|
||
/** @var array<string, array<int, string>> */ | ||
private array $schema_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; | ||
|
||
/** | ||
* @var array<string, array<int, string>> | ||
* | ||
|
@@ -146,31 +135,7 @@ public function getSchema(): Schema | |
*/ | ||
public function getJSONSchema(): object | ||
{ | ||
return $this->schema->buildSchema(); | ||
} | ||
|
||
/** | ||
* Do validation (against last schema only!) | ||
* | ||
* @param mixed $json Converted data to validate | ||
* | ||
* @return boolean | ||
*/ | ||
public function validate($json): bool | ||
{ | ||
try { | ||
$schema = Schema::import($this->buildSchema()); | ||
|
||
$context = new Context(); | ||
$context->tolerateStrings = (!defined('TU_USER')); | ||
$schema->in($json, $context); | ||
return true; | ||
} catch (Exception $e) { | ||
$errmsg = "JSON does not validate. Violations:\n"; | ||
$errmsg .= $e->getMessage(); | ||
$errmsg .= "\n"; | ||
throw new RuntimeException($errmsg); | ||
} | ||
return $this->schema->build(); | ||
} | ||
|
||
/** | ||
|
@@ -203,7 +168,7 @@ public function convert(string $xml) | |
(string)json_encode((array)$sxml), | ||
true | ||
); | ||
$this->loadSchemaPatterns(); | ||
$this->schema->loadPatterns(); | ||
|
||
$methods = $this->getMethods(); | ||
foreach ($methods as $method) { | ||
|
@@ -427,7 +392,7 @@ private function convertTo01(array $data): array | |
if ($network['type'] == 'local') { | ||
$network['type'] = 'loopback'; | ||
} | ||
if (!in_array($network['type'], $this->schema_patterns['networks_types'])) { | ||
if (!in_array($network['type'], $this->schema->getPatterns()['networks_types'])) { | ||
unset($network['type']); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,14 +18,14 @@ | |
/** | ||
* Handle inventory JSON Schema | ||
* | ||
* @author Johan Cwiklinski <[email protected]> | ||
* @author Johan Cwiklinski <[email protected]> | ||
*/ | ||
class Schema | ||
{ | ||
public const LAST_VERSION = 0.1; | ||
|
||
/** @var array<string, array<int, string>> */ | ||
private array $schema_patterns; | ||
private array $patterns; | ||
/** @var array<string, array<string, string>> */ | ||
private array $extra_properties = []; | ||
/** @var array<string, array<string, array<string, string>>> */ | ||
|
@@ -52,9 +52,9 @@ public function setExtraItemtypes(array $itemtypes): self | |
* | ||
* @return object | ||
*/ | ||
public function buildSchema(): object | ||
public function build(): object | ||
{ | ||
$string = file_get_contents($this->getSchemaPath()); | ||
$string = file_get_contents($this->getPath()); | ||
if ($string === false) { | ||
throw new RuntimeException('Unable to read schema file'); | ||
} | ||
|
@@ -143,7 +143,7 @@ public function buildSchema(): object | |
* | ||
* @return string | ||
*/ | ||
public function getSchemaPath(): string | ||
public function getPath(): string | ||
{ | ||
$schema_path = realpath(__DIR__ . '/../../inventory.schema.json'); | ||
if ($schema_path === false) { | ||
|
@@ -181,15 +181,15 @@ public function setExtraSubProperties(array $properties): self | |
* | ||
* @return void | ||
*/ | ||
public function loadSchemaPatterns(): void | ||
public function loadPatterns(): void | ||
{ | ||
$string = file_get_contents($this->getSchemaPath()); | ||
$string = file_get_contents($this->getPath()); | ||
if ($string === false) { | ||
throw new RuntimeException('Unable to read schema file'); | ||
} | ||
$json = json_decode($string, true); | ||
|
||
$this->schema_patterns['networks_types'] = explode( | ||
$this->patterns['networks_types'] = explode( | ||
'|', | ||
str_replace( | ||
['^(', ')$'], | ||
|
@@ -199,12 +199,22 @@ public function loadSchemaPatterns(): void | |
); | ||
} | ||
|
||
/** | ||
* 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 setStrictSchema(): self | ||
public function setStrict(): self | ||
{ | ||
$this->strict_schema = true; | ||
return $this; | ||
|
@@ -215,7 +225,7 @@ public function setStrictSchema(): self | |
* | ||
* @return self | ||
*/ | ||
public function setFlexibleSchema(): self | ||
public function setFlexible(): self | ||
{ | ||
$this->strict_schema = false; | ||
return $this; | ||
|
@@ -251,7 +261,7 @@ private function buildFlexibleSchema(&$schemapart) | |
public function validate($json): bool | ||
{ | ||
try { | ||
$schema = \Swaggest\JsonSchema\Schema::import($this->buildSchema()); | ||
$schema = \Swaggest\JsonSchema\Schema::import($this->build()); | ||
|
||
$context = new Context(); | ||
$context->tolerateStrings = (!defined('TU_USER')); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters