1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Webmozart JSON package. |
5
|
|
|
* |
6
|
|
|
* (c) Bernhard Schussek <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Webmozart\Json; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @since 1.0 |
16
|
|
|
* |
17
|
|
|
* @author Bernhard Schussek <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
class JsonError |
|
|
|
|
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* User-land implementation of `json_last_error_msg()` for PHP < 5.5. |
23
|
|
|
* |
24
|
|
|
* @return string The last JSON error message. |
25
|
|
|
*/ |
26
|
5 |
|
public static function getLastErrorMessage() |
27
|
|
|
{ |
28
|
5 |
|
return self::getErrorMessage(json_last_error()); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Returns the error message of a JSON error code. |
33
|
|
|
* |
34
|
|
|
* @param int $error The error code. |
35
|
|
|
* |
36
|
|
|
* @return string The error message. |
37
|
|
|
*/ |
38
|
5 |
|
public static function getErrorMessage($error) |
39
|
|
|
{ |
40
|
|
|
switch ($error) { |
41
|
5 |
|
case JSON_ERROR_NONE: |
42
|
|
|
return 'JSON_ERROR_NONE'; |
43
|
5 |
|
case JSON_ERROR_DEPTH: |
44
|
2 |
|
return 'JSON_ERROR_DEPTH'; |
45
|
3 |
|
case JSON_ERROR_STATE_MISMATCH: |
46
|
|
|
return 'JSON_ERROR_STATE_MISMATCH'; |
47
|
3 |
|
case JSON_ERROR_CTRL_CHAR: |
48
|
|
|
return 'JSON_ERROR_CTRL_CHAR'; |
49
|
3 |
|
case JSON_ERROR_SYNTAX: |
50
|
|
|
return 'JSON_ERROR_SYNTAX'; |
51
|
3 |
|
case JSON_ERROR_UTF8: |
52
|
3 |
|
return 'JSON_ERROR_UTF8'; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if (version_compare(PHP_VERSION, '5.5.0', '>=')) { |
56
|
|
|
switch ($error) { |
57
|
|
|
case JSON_ERROR_RECURSION: |
58
|
|
|
return 'JSON_ERROR_RECURSION'; |
59
|
|
|
case JSON_ERROR_INF_OR_NAN: |
60
|
|
|
return 'JSON_ERROR_INF_OR_NAN'; |
61
|
|
|
case JSON_ERROR_UNSUPPORTED_TYPE: |
62
|
|
|
return 'JSON_ERROR_UNSUPPORTED_TYPE'; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return 'JSON_ERROR_UNKNOWN'; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
private function __construct() |
70
|
|
|
{ |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|