Debian Codegraph @

includes/Settings/Source/Format/JsonFormat.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Settings\Source\Format;
4
5 use Stringable;
6 use UnexpectedValueException;
7
8 /**
9 * Decodes settings data from JSON.
10 */
11 class JsonFormat implements Stringable, SettingsFormat {
12
13 /**
14 * Decodes JSON.
15 *
16 * @param string $data JSON string to decode.
17 *
18 * @return array
19 * @throws UnexpectedValueException
20 */
21 public function decode( string $data ): array {
22 $settings = json_decode( $data, true );
23
24 if ( $settings === null ) {
25 throw new UnexpectedValueException(
26 'Failed to decode JSON: ' . json_last_error_msg()
27 );
28 }
29
30 if ( !is_array( $settings ) ) {
31 throw new UnexpectedValueException(
32 'Decoded settings must be an array'
33 );
34 }
35
36 return $settings;
37 }
38
39 /**
40 * Returns true for the file extension 'json'. Case insensitive.
41 *
42 * @param string $ext File extension.
43 *
44 * @return bool
45 */
46 public static function supportsFileExtension( string $ext ): bool {
47 return strtolower( $ext ) == 'json';
48 }
49
50 /**
51 * Returns the name/type of this format (JSON).
52 *
53 * @return string
54 */
55 public function __toString(): string {
56 return 'JSON';
57 }
58 }