wiki.techinc.nl/includes/Settings/Source/Format/JsonFormat.php
Petr Pchelko 120ef51cbf SettingsBuilder: Add YAML file format.
If php-yaml extension in installed, use that. Otherwise
we fallback to symfony Yaml parser.

php-yaml is about 20 times faster then symfony, for default-settings.yaml
it will take PHP-yaml 6ms to load it vs 100ms for symfony. But given
that the result will be cached, it's better not to bring in
a required native dependency.

Bug: T294751
Change-Id: I3ffde926c3f264cacf39810ff7bd338c9f78823d
2021-11-29 09:27:20 -08:00

57 lines
1.1 KiB
PHP

<?php
namespace MediaWiki\Settings\Source\Format;
use UnexpectedValueException;
/**
* Decodes settings data from JSON.
*/
class JsonFormat implements SettingsFormat {
/**
* Decodes JSON.
*
* @param string $data JSON string to decode.
*
* @return array
* @throws UnexpectedValueException
*/
public function decode( string $data ): array {
$settings = json_decode( $data, true );
if ( $settings === null ) {
throw new UnexpectedValueException(
'Failed to decode JSON: ' . json_last_error_msg()
);
}
if ( !is_array( $settings ) ) {
throw new UnexpectedValueException(
'Decoded settings must be an array'
);
}
return $settings;
}
/**
* Returns true for the file extension 'json'. Case insensitive.
*
* @param string $ext File extension.
*
* @return bool
*/
public static function supportsFileExtension( string $ext ): bool {
return strtolower( $ext ) == 'json';
}
/**
* Returns the name/type of this format (JSON).
*
* @return string
*/
public function __toString(): string {
return 'JSON';
}
}