@@ -20,6 +20,9 @@ class Fixer
2020 /** @var bool If current char is within a string */
2121 protected $ inStr = false ;
2222
23+ /** @var bool Whether to throw Exception on failure */
24+ protected $ silent = false ;
25+
2326 /** @var array The complementary pairs */
2427 protected $ pairs = [
2528 '{ ' => '} ' ,
@@ -36,18 +39,50 @@ class Fixer
3639 /** @var string Missing value. (Options: true, false, null) */
3740 protected $ missingValue = 'null ' ;
3841
42+ /**
43+ * Set/unset silent mode.
44+ *
45+ * @param bool $silent
46+ *
47+ * @return $this
48+ */
49+ public function silent ($ silent = true )
50+ {
51+ $ this ->silent = (bool ) $ silent ;
52+
53+ return $ this ;
54+ }
55+
56+ /**
57+ * Set missing value.
58+ *
59+ * @param mixed $value
60+ *
61+ * @return $this
62+ */
63+ public function missingValue ($ value )
64+ {
65+ if ($ value === null ) {
66+ $ value = 'null ' ;
67+ } elseif (\is_bool ($ value )) {
68+ $ value = $ value ? 'true ' : 'false ' ;
69+ }
70+
71+ $ this ->missingValue = $ value ;
72+
73+ return $ this ;
74+ }
75+
3976 /**
4077 * Fix the truncated JSON.
4178 *
42- * @param string $json The JSON string to fix.
43- * @param bool $silent If silent, doesnt throw when fixing fails.
44- * @param string $missingValue Missing value constructor. (Options: true, false, null).
79+ * @param string $json The JSON string to fix.
4580 *
4681 * @throws \RuntimeExcaption When fixing fails.
4782 *
4883 * @return string Fixed JSON. If failed with silent then original JSON.
4984 */
50- public function fix ($ json, $ silent = false , $ missingValue = ' null ' )
85+ public function fix ($ json )
5186 {
5287 list ($ head , $ json , $ tail ) = $ this ->trim ($ json );
5388
@@ -59,16 +94,17 @@ public function fix($json, $silent = false, $missingValue = 'null')
5994 return $ tmpJson ;
6095 }
6196
62- $ this ->reset ($ missingValue );
97+ $ this ->reset ();
6398
64- return $ head . $ this ->doFix (\rtrim ( $ json), $ silent ) . $ tail ;
99+ return $ head . $ this ->doFix ($ json ) . $ tail ;
65100 }
66101
67102 protected function trim ($ json )
68103 {
69104 \preg_match ('/^(\s*)([^\s]+)(\s*)$/ ' , $ json , $ match );
70105
71- $ match += ['' , '' , \trim ($ json ), '' ];
106+ $ match += ['' , '' , '' , '' ];
107+ $ match [2 ] = \trim ($ json );
72108
73109 \array_shift ($ match );
74110
@@ -95,6 +131,14 @@ protected function quickFix($json)
95131 return $ this ->padString ($ json );
96132 }
97133
134+ protected function reset ()
135+ {
136+ $ this ->stack = [];
137+ $ this ->inStr = false ;
138+ $ this ->objectPos = -1 ;
139+ $ this ->arrayPos = -1 ;
140+ }
141+
98142 protected function maybeLiteral ($ json )
99143 {
100144 if (!\in_array ($ json [0 ], ['t ' , 'f ' , 'n ' ])) {
@@ -112,18 +156,7 @@ protected function maybeLiteral($json)
112156 // @codeCoverageIgnoreEnd
113157 }
114158
115- protected function reset ($ missingValue = 'null ' )
116- {
117- $ this ->stack = [];
118- $ this ->inStr = false ;
119-
120- $ this ->objectPos = -1 ;
121- $ this ->arrayPos = -1 ;
122-
123- $ this ->missingValue = $ missingValue ;
124- }
125-
126- protected function doFix ($ json , $ silent = false )
159+ protected function doFix ($ json )
127160 {
128161 list ($ index , $ char ) = [-1 , '' ];
129162
@@ -137,7 +170,7 @@ protected function doFix($json, $silent = false)
137170 }
138171 }
139172
140- return $ this ->fixOrFail ($ json, $ silent );
173+ return $ this ->fixOrFail ($ json );
141174 }
142175
143176 protected function stack ($ prev , $ char , $ index , $ next )
@@ -208,7 +241,7 @@ protected function updatePos($char, $index)
208241 }
209242 }
210243
211- protected function fixOrFail ($ json, $ silent )
244+ protected function fixOrFail ($ json )
212245 {
213246 $ length = \strlen ($ json );
214247 $ tmpJson = $ this ->pad ($ json );
@@ -217,7 +250,7 @@ protected function fixOrFail($json, $silent)
217250 return $ tmpJson ;
218251 }
219252
220- if ($ silent ) {
253+ if ($ this -> silent ) {
221254 return $ json ;
222255 }
223256
0 commit comments