1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
|
<?php
namespace MediaWiki\Settings\Source\Format;
use Stringable;
use UnexpectedValueException;
/**
* Decodes settings data from JSON.
*/
class JsonFormat implements Stringable, 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';
}
}
|