1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SMW\Utils; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Convenience method to retrieved stringified error codes. |
7
|
|
|
* |
8
|
|
|
* @license GNU GPL v2+ |
9
|
|
|
* @since 2.5 |
10
|
|
|
* |
11
|
|
|
* @author mwjames |
12
|
|
|
*/ |
13
|
|
|
class ErrorCodeFormatter { |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var array |
17
|
|
|
*/ |
18
|
|
|
private static $constants = array(); |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
private static $jsonErrors = array(); |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @see http://php.net/manual/en/function.json-decode.php |
27
|
|
|
* @since 2.5 |
28
|
|
|
* |
29
|
|
|
* @param integer $errorCode |
30
|
|
|
* |
31
|
|
|
* @return string |
32
|
|
|
*/ |
33
|
|
|
public static function getStringFromJsonErrorCode( $errorCode ) { |
34
|
|
|
|
35
|
|
|
if ( self::$constants === array() ) { |
36
|
|
|
self::$constants = get_defined_constants( true ); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
if ( isset( self::$constants["json"] ) && self::$jsonErrors === array() ) { |
40
|
|
|
foreach ( self::$constants["json"] as $name => $value ) { |
41
|
|
|
if ( !strncmp( $name, "JSON_ERROR_", 11 ) ) { |
42
|
|
|
self::$jsonErrors[$value] = $name; |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return isset( self::$jsonErrors[$errorCode] ) ? self::$jsonErrors[$errorCode] : 'UNKNOWN'; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @since 2.5 |
52
|
|
|
* |
53
|
|
|
* @param integer $errorCode |
54
|
|
|
* |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
|
|
public static function getMessageFromJsonErrorCode( $errorCode ) { |
58
|
|
|
|
59
|
|
|
$errorMessages = array( |
60
|
|
|
JSON_ERROR_STATE_MISMATCH => 'Underflow or the modes mismatch, malformed JSON', |
61
|
|
|
JSON_ERROR_CTRL_CHAR => 'Unexpected control character found, possibly incorrectly encoded', |
62
|
|
|
JSON_ERROR_SYNTAX => 'Syntax error, malformed JSON', |
63
|
|
|
JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded', |
64
|
|
|
JSON_ERROR_DEPTH => 'The maximum stack depth has been exceeded' |
65
|
|
|
); |
66
|
|
|
|
67
|
|
|
if ( !isset( $errorMessages[$errorCode] ) ) { |
68
|
|
|
return self::getStringFromJsonErrorCode( $errorCode ); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return sprintf( |
72
|
|
|
"Expected a JSON compatible format but failed with '%s'", |
73
|
|
|
$errorMessages[$errorCode] |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
} |
78
|
|
|
|