@@ -29,7 +29,8 @@ class Json {
2929 * @param string $pathfile → path to the file
3030 *
3131 * @throws JsonException → couldn't create file
32- * @return bool true → if the file is created
32+ *
33+ * @return boolean true → if the file is created
3334 */
3435 public static function arrayToFile ($ array , $ pathfile ) {
3536
@@ -42,7 +43,7 @@ public static function arrayToFile($array, $pathfile) {
4243
4344 $ json = json_encode ($ array , JSON_PRETTY_PRINT );
4445
45- self ::jsonLastError () ;
46+ $ json = self ::_jsonLastError () ? json_encode ( $ json , 128 ) : $ json ;
4647
4748 if (!$ file = fopen ($ pathfile , 'w+ ' )) {
4849
@@ -61,52 +62,92 @@ public static function arrayToFile($array, $pathfile) {
6162 *
6263 * @since 1.0.0
6364 *
64- * @param string $pathfile → path to JSON file
65+ * @param string $file → path or external url to JSON file
6566 *
66- * @return array
67+ * @return array|false
6768 */
68- public static function fileToArray ($ pathfile ) {
69+ public static function fileToArray ($ file ) {
6970
70- if (!is_file ($ pathfile )) {
71+ if (!is_file ($ file ) && ! filter_var ( $ file , FILTER_VALIDATE_URL )) {
7172
7273 self ::arrayToFile ([], $ pathFile );
7374 }
7475
75- $ jsonString = file_get_contents ($ pathfile );
76+ $ jsonString = file_get_contents ($ file );
7677
7778 $ jsonArray = json_decode ($ jsonString , true );
78-
79- self ::jsonLastError ();
80-
81- return $ jsonArray ;
79+
80+ return self ::_jsonLastError () ?: $ jsonArray ;
8281 }
8382
8483 /**
8584 * Check for errors.
8685 *
87- * @since 1.0.0
86+ * @since 1.1.3
8887 *
89- * @throws JsonException → JSON (encode-decode) error
90- * @return true
88+ * @return array|null
9189 */
92- public static function jsonLastError () {
90+ private static function _jsonLastError () {
9391
9492 switch (json_last_error ()) {
9593
96- case JSON_ERROR_NONE :
97- return true ;
98- case JSON_ERROR_UTF8 :
99- throw new JsonException ('Malformed UTF-8 characters ' , 606 );
94+ case JSON_ERROR_NONE : return null ;
95+
10096 case JSON_ERROR_DEPTH :
101- throw new JsonException ('Maximum stack depth exceeded ' , 607 );
102- case JSON_ERROR_SYNTAX :
103- throw new JsonException ('Syntax error, malformed JSON ' , 608 );
104- case JSON_ERROR_CTRL_CHAR :
105- throw new JsonException ('Unexpected control char found ' , 609 );
97+ return [
98+ 'message ' => 'Maximum stack depth exceeded ' ,
99+ 'error-code ' => 1
100+ ];
106101 case JSON_ERROR_STATE_MISMATCH :
107- throw new JsonException ('Underflow or the modes mismatch ' , 610 );
102+ return [
103+ 'message ' => 'Underflow or the modes mismatch ' ,
104+ 'error-code ' => 2
105+ ];
106+ case JSON_ERROR_CTRL_CHAR :
107+ return [
108+ 'message ' => 'Unexpected control char found ' ,
109+ 'error-code ' => 3
110+ ];
111+ case JSON_ERROR_SYNTAX :
112+ return [
113+ 'message ' => 'Syntax error, malformed JSON ' ,
114+ 'error-code ' => 4
115+ ];
116+ case JSON_ERROR_UTF8 :
117+ return [
118+ 'message ' => 'Malformed UTF-8 characters ' ,
119+ 'error-code ' => 5
120+ ];
121+ case JSON_ERROR_RECURSION :
122+ return [
123+ 'message ' => 'Recursion error in value to be encoded ' ,
124+ 'error-code ' => 6
125+ ];
126+ case JSON_ERROR_INF_OR_NAN :
127+ return [
128+ 'message ' => 'Error NAN/INF in value to be encoded ' ,
129+ 'error-code ' => 7
130+ ];
131+ case JSON_ERROR_UNSUPPORTED_TYPE :
132+ return [
133+ 'message ' => 'Type value given cannot be encoded ' ,
134+ 'error-code ' => 8
135+ ];
136+ case 9 : // JSON_ERROR_INVALID_PROPERTY_NAME (PHP 7.0.0)
137+ return [
138+ 'message ' => 'Name value given cannot be encoded ' ,
139+ 'error-code ' => 9
140+ ];
141+ case 10 : //JSON_ERROR_UTF16 (PHP 7.0.0)
142+ return [
143+ 'message ' => 'Malformed UTF-16 characters ' ,
144+ 'error-code ' => 10
145+ ];
108146 default :
109- throw new JsonException ('Unknown error ' , 995 );
147+ return [
148+ 'message ' => 'Unknown error ' ,
149+ 'error-code ' => 999
150+ ];
110151 }
111152 }
112153}
0 commit comments