|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Acquia\Json; |
|
4
|
|
|
|
|
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) |
|
19
|
|
|
{ |
|
20
|
3 |
|
self::$useNativePrettyPrint = $useNative; |
|
21
|
3 |
|
} |
|
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) |
|
52
|
|
|
{ |
|
53
|
93 |
|
return json_decode($json, true); |
|
54
|
|
|
} |
|
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) |
|
67
|
|
|
{ |
|
68
|
3 |
|
$result = ''; |
|
69
|
3 |
|
$pos = 0; |
|
70
|
3 |
|
$indentation = ' '; |
|
71
|
3 |
|
$newline = "\n"; |
|
72
|
3 |
|
$previousChar = ''; |
|
73
|
3 |
|
$outOfQuotes = true; |
|
74
|
|
|
|
|
75
|
|
|
// Unescape slashes. |
|
76
|
3 |
|
if (strpos($json, '/')) { |
|
77
|
3 |
|
$json = preg_replace('#\134{1}/#', '/', $json); |
|
78
|
3 |
|
} |
|
79
|
|
|
|
|
80
|
3 |
|
$stringLength = strlen($json); |
|
81
|
|
|
|
|
82
|
3 |
|
for ($i = 0; $i <= $stringLength; $i++) { |
|
83
|
|
|
|
|
84
|
|
|
// Grab the next character in the string. |
|
85
|
3 |
|
$char = substr($json, $i, 1); |
|
86
|
|
|
|
|
87
|
3 |
|
if (':' == $previousChar && $outOfQuotes) { |
|
88
|
3 |
|
$result .= ' '; |
|
89
|
3 |
|
} |
|
90
|
|
|
|
|
91
|
|
|
// Are we inside a quoted string? |
|
92
|
3 |
|
if ('"' == $char && $previousChar != '\\') { |
|
93
|
3 |
|
$outOfQuotes = !$outOfQuotes; |
|
94
|
|
|
|
|
95
|
|
|
// If this character is the end of an element, |
|
96
|
|
|
// output a new line and indent the next line. |
|
97
|
3 |
|
} elseif (('}' == $char || ']' == $char) && $outOfQuotes) { |
|
98
|
3 |
|
$result .= $newline; |
|
99
|
3 |
|
$pos --; |
|
100
|
3 |
|
for ($j = 0; $j < $pos; $j++) { |
|
101
|
3 |
|
$result .= $indentation; |
|
102
|
3 |
|
} |
|
103
|
3 |
|
} |
|
104
|
|
|
|
|
105
|
|
|
// Add the character to the result string. |
|
106
|
3 |
|
$result .= $char; |
|
107
|
|
|
|
|
108
|
|
|
// If the last character was the beginning of an element, |
|
109
|
|
|
// output a new line and indent the next line. |
|
110
|
3 |
|
if ((',' == $char || '{' == $char || '[' == $char) && $outOfQuotes) { |
|
111
|
3 |
|
$result .= $newline; |
|
112
|
3 |
|
if ('{' == $char || '[' == $char) { |
|
113
|
3 |
|
$pos ++; |
|
114
|
3 |
|
} |
|
115
|
|
|
|
|
116
|
3 |
|
for ($j = 0; $j < $pos; $j++) { |
|
117
|
3 |
|
$result .= $indentation; |
|
118
|
3 |
|
} |
|
119
|
3 |
|
} |
|
120
|
|
|
|
|
121
|
3 |
|
$previousChar = $char; |
|
122
|
3 |
|
} |
|
123
|
|
|
|
|
124
|
3 |
|
return $result; |
|
125
|
|
|
} |
|
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) |
|
135
|
|
|
{ |
|
136
|
24 |
|
if (!is_file($filepath)) { |
|
137
|
3 |
|
throw new \RuntimeException('File not found: ' . $filepath); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
21 |
|
if (!$filedata = static::readFiledata($filepath)) { |
|
141
|
3 |
|
throw new \RuntimeException('Error reading file: ' . $filepath); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
18 |
|
if (!$json = self::decode($filedata)) { |
|
145
|
3 |
|
throw new \RuntimeException('Error parsing json: ' . $filepath); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
15 |
|
return $json; |
|
149
|
|
|
} |
|
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) |
|
160
|
|
|
{ |
|
161
|
18 |
|
return @file_get_contents($filepath); |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|