PHP Класс Webmozart\Json\Migration\MigratingConverter
This decorator supports JSON objects in different versions. The decorated
converter can be written for a specific version. Any other version can be
supported by supplying a {@link MigrationManager} that is able to migrate
a JSON object in that version to the version required by the decorated
converter.
You need to pass the decorated converter and the migration manager to the
constructor:
~~~php
Written for version 3.0
$converter = ConfigFileConverter();
Support older versions of the file
$migrationManager = new MigrationManager(array(
new ConfigFile10To20Migration(),
new ConfigFile20To30Migration(),
));
Decorate the converter
$converter = new MigratingConverter($converter, '3.0', $migrationManager);
~~~
You can load JSON data in any version with the method {@link fromJson()}. If
the "version" property of the JSON object is different than the version
supported by the decorated converter, the JSON object is migrated to the
required version.
~~~php
$jsonDecoder = new JsonDecoder();
$configFile = $converter->fromJson($jsonDecoder->decode($json));
~~~
You can also dump data as JSON object with {@link toJson()}:
~~~php
$jsonEncoder = new JsonEncoder();
$jsonEncoder->encode($converter->toJson($configFile));
~~~
By default, data is dumped in the current version. If you want to dump the
data in a specific version, pass the "targetVersion" option:
~~~php
$jsonEncoder->encode($converter->toJson($configFile, array(
'targetVersion' => '2.0',
)));
~~~
Показать файл
Открыть проект
Примеры использования класса
Открытые методы
Метод |
Описание |
|
__construct ( Webmozart\Json\Conversion\JsonConverter $innerConverter, string $currentVersion, MigrationManager $migrationManager ) |
Creates the converter. |
|
fromJson ( $jsonData, array $options = [] ) |
|
|
toJson ( $data, array $options = [] ) |
|
|
Приватные методы
Описание методов
__construct()
публичный Метод
public __construct ( Webmozart\Json\Conversion\JsonConverter $innerConverter, string $currentVersion, MigrationManager $migrationManager ) |
$innerConverter |
Webmozart\Json\Conversion\JsonConverter |
The decorated converter |
$currentVersion |
string |
The version that the decorated
converter is compatible with |
$migrationManager |
MigrationManager |
The manager for migrating JSON data |
fromJson()
публичный Метод