1 | <?php |
||
5 | class Json implements JsonInterface |
||
6 | { |
||
7 | /** |
||
8 | * @var boolean |
||
9 | */ |
||
10 | protected static $useNativePrettyPrint = true; |
||
11 | |||
12 | /** |
||
13 | * Use the native PHP pretty print options. Set to false to use the local |
||
14 | * pretty print method in this class (not recommended). |
||
15 | * |
||
16 | * @param boolean $useNative |
||
17 | */ |
||
18 | 3 | public static function useNativePrettyPrint($useNative = true) |
|
22 | |||
23 | /** |
||
24 | * @param mixed $data |
||
25 | * @param bool $prettyPrint |
||
26 | * |
||
27 | * @return string |
||
28 | */ |
||
29 | 78 | public static function encode($data, $prettyPrint = true) |
|
30 | { |
||
31 | 78 | $options = JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT; |
|
32 | |||
33 | 78 | $useNative = self::$useNativePrettyPrint && defined('JSON_PRETTY_PRINT') && defined('JSON_UNESCAPED_SLASHES'); |
|
34 | 78 | if ($prettyPrint && $useNative) { |
|
35 | 75 | $options = $options | JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES; |
|
36 | 75 | } |
|
37 | |||
38 | 78 | $json = json_encode($data, $options); |
|
39 | 78 | if ($prettyPrint && !$useNative) { |
|
40 | 3 | $json = self::prettyPrint($json); |
|
41 | 3 | } |
|
42 | |||
43 | 78 | return $json; |
|
44 | } |
||
45 | |||
46 | /** |
||
47 | * @param string $json |
||
48 | * |
||
49 | * @return array |
||
50 | */ |
||
51 | 93 | public static function decode($json) |
|
55 | |||
56 | /** |
||
57 | * Indents a flat JSON string to make it more human-readable. |
||
58 | * |
||
59 | * The JSON_PRETTY_PRINT and JSON_UNESCAPED_SLASHES options are not |
||
60 | * available until PHP 5.4. |
||
61 | * |
||
62 | * @param string $json |
||
63 | * |
||
64 | * @return string |
||
65 | */ |
||
66 | 3 | protected static function prettyPrint($json) |
|
126 | |||
127 | /** |
||
128 | * Parses a file into a PHP array. |
||
129 | * |
||
130 | * @param string $filepath |
||
131 | * |
||
132 | * @throws \RuntimeException |
||
133 | */ |
||
134 | 24 | public static function parseFile($filepath) |
|
150 | |||
151 | /** |
||
152 | * Wrapper around file_get_contents(), useful for testing the inability to |
||
153 | * read a file. |
||
154 | * |
||
155 | * @param string $filepath |
||
156 | * |
||
157 | * @return string|false |
||
158 | */ |
||
159 | 18 | protected static function readFiledata($filepath) |
|
163 | } |
||
164 |