Skip to content

Commit b1e20a6

Browse files
committed
replace collections with switch case
1 parent 3d3aa9f commit b1e20a6

File tree

1 file changed

+63
-59
lines changed

1 file changed

+63
-59
lines changed

src/Json.php

Lines changed: 63 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,64 @@ 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+
return [
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+
JSON_ERROR_INVALID_PROPERTY_NAME => [
152+
'message' => 'Name value given cannot be encoded',
153+
'error-code' => 9,
154+
],
155+
JSON_ERROR_UTF16 => [
156+
'message' => 'Malformed UTF-16 characters',
157+
'error-code' => 10,
158+
],
159+
'default' => [
160+
'message' => 'Unknown error',
161+
'error-code' => 999,
162+
],
163+
];
164+
}
165+
107166
/**
108167
* Check for errors.
109168
*
@@ -113,64 +172,9 @@ private static function saveFile($file, $json)
113172
*/
114173
private static function jsonLastError()
115174
{
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-
}
175+
$collections = self::jsonLastErrorCollections();
176+
$jsonLastError = json_last_error();
177+
178+
return array_key_exists($jsonLastError, $collections) ? $collections[$jsonLastError] : $collections['default'];
175179
}
176180
}

0 commit comments

Comments
 (0)