File: JsonFormat.php

package info (click to toggle)
mediawiki 1%3A1.43.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 417,464 kB
  • sloc: php: 1,062,949; javascript: 664,290; sql: 9,714; python: 5,458; xml: 3,489; sh: 1,131; makefile: 64
file content (58 lines) | stat: -rw-r--r-- 1,109 bytes parent folder | download
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';
	}
}