Skip to content

Commit 0a9f502

Browse files
committed
Merge branch 'peter279k-master'
2 parents 3d3aa9f + 0e2b1f0 commit 0a9f502

File tree

1 file changed

+69
-59
lines changed

1 file changed

+69
-59
lines changed

src/Json.php

Lines changed: 69 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
namespace Josantonius\Json;
1212

1313
use Josantonius\Json\Exception\JsonException;
14+
use Josantonius\Json\Exception\JsonLastErrorException;
1415

1516
/**
1617
* Json handler.
@@ -104,6 +105,70 @@ private static function saveFile($file, $json)
104105
}
105106
}
106107

108+
/**
109+
* The JSON last error collections.
110+
*
111+
* @since 1.1.3
112+
*
113+
* @return array
114+
*/
115+
private static function jsonLastErrorCollections()
116+
{
117+
$collections = [
118+
JSON_ERROR_NONE => null,
119+
JSON_ERROR_DEPTH => [
120+
'message' => 'Maximum stack depth exceeded',
121+
'error-code' => 1,
122+
],
123+
JSON_ERROR_STATE_MISMATCH => [
124+
'message' => 'Underflow or the modes mismatch',
125+
'error-code' => 2,
126+
],
127+
JSON_ERROR_CTRL_CHAR => [
128+
'message' => 'Unexpected control char found',
129+
'error-code' => 3,
130+
],
131+
JSON_ERROR_SYNTAX => [
132+
'message' => 'Syntax error, malformed JSON',
133+
'error-code' => 4,
134+
],
135+
JSON_ERROR_UTF8 => [
136+
'message' => 'Malformed UTF-8 characters',
137+
'error-code' => 5,
138+
],
139+
JSON_ERROR_RECURSION => [
140+
'message' => 'Recursion error in value to be encoded',
141+
'error-code' => 6,
142+
],
143+
JSON_ERROR_INF_OR_NAN => [
144+
'message' => 'Error NAN/INF in value to be encoded',
145+
'error-code' => 7,
146+
],
147+
JSON_ERROR_UNSUPPORTED_TYPE => [
148+
'message' => 'Type value given cannot be encoded',
149+
'error-code' => 8,
150+
],
151+
'default' => [
152+
'message' => 'Unknown error',
153+
'error-code' => 999,
154+
],
155+
];
156+
157+
if (version_compare(PHP_VERSION, '7.0.0', '>='))
158+
{
159+
$collections[JSON_ERROR_INVALID_PROPERTY_NAME] = [
160+
'message' => 'Name value given cannot be encoded',
161+
'error-code' => 9,
162+
];
163+
$collections[JSON_ERROR_UTF16] = [
164+
'message' => 'Malformed UTF-16 characters',
165+
'error-code' => 10,
166+
];
167+
}
168+
169+
return $collections;
170+
}
171+
107172
/**
108173
* Check for errors.
109174
*
@@ -113,64 +178,9 @@ private static function saveFile($file, $json)
113178
*/
114179
private static function jsonLastError()
115180
{
116-
switch (json_last_error()) {
117-
case JSON_ERROR_NONE:
118-
return null;
119-
case JSON_ERROR_DEPTH:
120-
return [
121-
'message' => 'Maximum stack depth exceeded',
122-
'error-code' => 1,
123-
];
124-
case JSON_ERROR_STATE_MISMATCH:
125-
return [
126-
'message' => 'Underflow or the modes mismatch',
127-
'error-code' => 2,
128-
];
129-
case JSON_ERROR_CTRL_CHAR:
130-
return [
131-
'message' => 'Unexpected control char found',
132-
'error-code' => 3,
133-
];
134-
case JSON_ERROR_SYNTAX:
135-
return [
136-
'message' => 'Syntax error, malformed JSON',
137-
'error-code' => 4,
138-
];
139-
case JSON_ERROR_UTF8:
140-
return [
141-
'message' => 'Malformed UTF-8 characters',
142-
'error-code' => 5,
143-
];
144-
case JSON_ERROR_RECURSION:
145-
return [
146-
'message' => 'Recursion error in value to be encoded',
147-
'error-code' => 6,
148-
];
149-
case JSON_ERROR_INF_OR_NAN:
150-
return [
151-
'message' => 'Error NAN/INF in value to be encoded',
152-
'error-code' => 7,
153-
];
154-
case JSON_ERROR_UNSUPPORTED_TYPE:
155-
return [
156-
'message' => 'Type value given cannot be encoded',
157-
'error-code' => 8,
158-
];
159-
case 9: // JSON_ERROR_INVALID_PROPERTY_NAME (PHP 7.0.0)
160-
return [
161-
'message' => 'Name value given cannot be encoded',
162-
'error-code' => 9,
163-
];
164-
case 10: //JSON_ERROR_UTF16 (PHP 7.0.0)
165-
return [
166-
'message' => 'Malformed UTF-16 characters',
167-
'error-code' => 10,
168-
];
169-
default:
170-
return [
171-
'message' => 'Unknown error',
172-
'error-code' => 999,
173-
];
174-
}
181+
$collections = self::jsonLastErrorCollections();
182+
$jsonLastError = json_last_error();
183+
184+
return isset($jsonLastError) ? $collections[$jsonLastError] : $collections['default'];
175185
}
176186
}

0 commit comments

Comments
 (0)