diff --git a/composer.json b/composer.json index 3dd708bc4..e2331ea86 100644 --- a/composer.json +++ b/composer.json @@ -22,7 +22,7 @@ "require": { "php": "^8.2", "guzzlehttp/guzzle": "^7.8", - "justinrainbow/json-schema": "^5.2", + "justinrainbow/json-schema": "^5.2 || ^6.0", "caseyamcl/guzzle_retry_middleware": "^2.9" }, "require-dev": { diff --git a/phpstan.neon b/phpstan.neon index c18d408c9..84f3d4d95 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -9,4 +9,8 @@ parameters: - identifier: match.unhandled path: src/Generated/V2/Schemas/Marketplace/*.php - identifier: instanceof.alwaysTrue - path: src/Generated \ No newline at end of file + path: src/Generated + # justinrainbow/json-schema 5.x has incorrect doc-comments on the Factory constructor + - identifier: argument.type + path: src/Validator/Validator.php + reportUnmatched: false \ No newline at end of file diff --git a/src/Validator/SchemaStorage.php b/src/Validator/SchemaStorage.php index 43adda7ea..96d579052 100644 --- a/src/Validator/SchemaStorage.php +++ b/src/Validator/SchemaStorage.php @@ -4,6 +4,7 @@ namespace Mittwald\ApiClient\Validator; +use JsonSchema\Constraints\BaseConstraint; use JsonSchema\SchemaStorageInterface; use stdClass; @@ -11,12 +12,19 @@ class SchemaStorage implements SchemaStorageInterface { private array $schemas = []; + /** + * @param mixed $schema Schema; may be passed as array, despite the interface docs + */ public function addSchema($id, $schema = null): void { + if (is_array($schema)) { + $schema = BaseConstraint::arrayToObjectRecursive($schema); + } + $this->schemas[$id] = $schema; } - public function getSchema($id): object + public function getSchema($id): object|bool { return $this->schemas[$id]; } diff --git a/src/Validator/Validator.php b/src/Validator/Validator.php index 1af2ed80d..9a24cb1a9 100644 --- a/src/Validator/Validator.php +++ b/src/Validator/Validator.php @@ -18,7 +18,7 @@ class Validator extends \JsonSchema\Validator public function __construct() { $schemaStorage = new SchemaStorage(); - $factory = new Factory($schemaStorage); // @phpstan-ignore argument.type (doc-comments are incorrect) + $factory = new Factory($schemaStorage); parent::__construct($factory); }