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