wiki.techinc.nl/includes/Settings/Source/Format/JsonFormat.php
Bartosz Dziewoński ccd423225f Add "implements Stringable" to every class with "function __toString()"
In PHP 8, but not in PHP 7.4, every class with a __toString() function
implicitly implements the Stringable interface. Therefore, the
behavior of checks like "instanceof Stringable" differs between these
PHP versions when such classes are involved. Make every such class
implement the interface so that the behavior will be consistent.

The PHP 7.4 fallback for the Stringable interface is provided by
symfony/polyfill-php80.

Change-Id: I3f0330c2555c7d3bf99b654ed3c0b0303e257ea1
2024-06-13 00:23:39 +00:00

58 lines
1.1 KiB
PHP

<?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';
}
}