PHP Класс Webmozart\Json\Validation\ValidatingConverter
Pass the path to the schema to the constructor:
~~~php
$converter = ConfigFileConverter();
Decorate the converter
$converter = new ValidatingConverter($converter, __DIR__.'/schema.json');
~~~
Whenever you load or dump data as JSON, the JSON structure is validated
against the schema:
~~~php
$jsonDecoder = new JsonDecoder();
$configFile = $converter->fromJson($jsonDecoder->decode($json));
$jsonEncoder = new JsonEncoder();
$jsonEncoder->encode($converter->toJson($configFile));
~~~
If you want to dynamically determine the path to the schema file, pass a
callable instead of the string. This is especially useful when versioning
your JSON data:
~~~php
$converter = ConfigFileConverter();
Calculate the schema path based on the "version" key in the JSON object
$getSchemaPath = function ($jsonData) {
return __DIR__.'/schema-'.$jsonData->version.'.json';
}
Decorate the converter
$converter = new ValidatingConverter($converter, $getSchemaPath);
~~~
Показать файл
Открыть проект
Примеры использования класса
Открытые методы
Метод |
Описание |
|
__construct ( Webmozart\Json\Conversion\JsonConverter $innerConverter, string | callable | null $schema = null, JsonValidator $jsonValidator = null ) |
Creates the converter. |
|
fromJson ( $jsonData, array $options = [] ) |
|
|
toJson ( $data, array $options = [] ) |
|
|
Приватные методы
Описание методов
__construct()
публичный Метод
public __construct ( Webmozart\Json\Conversion\JsonConverter $innerConverter, string | callable | null $schema = null, JsonValidator $jsonValidator = null ) |
$innerConverter |
Webmozart\Json\Conversion\JsonConverter |
The decorated converter |
$schema |
string | callable | null |
The path to the schema file
or a callable for calculating
the path dynamically for a
given JSON data. If `null`,
the schema is taken from the
`$schema` property of the
JSON data |
$jsonValidator |
Webmozart\Json\JsonValidator |
The JSON validator (optional) |
fromJson()
публичный Метод