Skip to content

Commit

Permalink
Fix, clean, rename
Browse files Browse the repository at this point in the history
  • Loading branch information
trasher committed Jan 21, 2025
1 parent d5e3994 commit 003c2ba
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 52 deletions.
45 changes: 5 additions & 40 deletions lib/php/Converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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>>
*
Expand Down Expand Up @@ -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();
}

/**
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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']);
}
}
Expand Down
32 changes: 21 additions & 11 deletions lib/php/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -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>>> */
Expand All @@ -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');
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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(
['^(', ')$'],
Expand All @@ -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;
Expand All @@ -215,7 +225,7 @@ public function setStrictSchema(): self
*
* @return self
*/
public function setFlexibleSchema(): self
public function setFlexible(): self
{
$this->strict_schema = false;
return $this;
Expand Down Expand Up @@ -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'));
Expand Down
2 changes: 1 addition & 1 deletion tests/Glpi/Inventory/tests/units/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function testSchemaPath(): void
{
$expected = realpath(TU_DIR . '/../inventory.schema.json');
$instance = new \Glpi\Inventory\Schema();
$this->assertSame($expected, $instance->getSchemaPath());
$this->assertSame($expected, $instance->getPath());
}

public function testValidateOK(): void
Expand Down

0 comments on commit 003c2ba

Please sign in to comment.