|
1
|
|
|
<?php namespace Arcanedev\Json; |
|
2
|
|
|
|
|
3
|
|
|
class JsonErrorHandler |
|
4
|
|
|
{ |
|
5
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
6
|
|
|
| Properties |
|
7
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
8
|
|
|
*/ |
|
9
|
|
|
/** |
|
10
|
|
|
* Handled errors list. |
|
11
|
|
|
* |
|
12
|
|
|
* @var array |
|
13
|
|
|
*/ |
|
14
|
|
|
protected static $errors = [ |
|
15
|
|
|
'decode' => [ |
|
16
|
|
|
'default' => Exceptions\Decode\DecodeException::class, |
|
17
|
|
|
|
|
18
|
|
|
'handled' => [ |
|
19
|
|
|
JSON_ERROR_DEPTH => Exceptions\Decode\DecodeDepthException::class, |
|
20
|
|
|
JSON_ERROR_STATE_MISMATCH => Exceptions\Decode\StateMismatchException::class, |
|
21
|
|
|
JSON_ERROR_CTRL_CHAR => Exceptions\Decode\ControlCharacterException::class, |
|
22
|
|
|
JSON_ERROR_SYNTAX => Exceptions\Decode\SyntaxException::class, |
|
23
|
|
|
JSON_ERROR_UTF8 => Exceptions\Decode\UTF8Exception::class, |
|
24
|
|
|
], |
|
25
|
|
|
], |
|
26
|
|
|
|
|
27
|
|
|
'encode' => [ |
|
28
|
|
|
'default' => Exceptions\Encode\EncodeException::class, |
|
29
|
|
|
|
|
30
|
|
|
'handled' => [ |
|
31
|
|
|
JSON_ERROR_DEPTH => Exceptions\Encode\EncodeDepthException::class, |
|
32
|
|
|
JSON_ERROR_RECURSION => Exceptions\Encode\RecursionException::class, |
|
33
|
|
|
JSON_ERROR_INF_OR_NAN => Exceptions\Encode\InfiniteOrNotANumberException::class, |
|
34
|
|
|
JSON_ERROR_UNSUPPORTED_TYPE => Exceptions\Encode\UnsupportedTypeException::class, |
|
35
|
|
|
JSON_ERROR_INVALID_PROPERTY_NAME => Exceptions\Encode\InvalidPropertyNameException::class, |
|
36
|
|
|
], |
|
37
|
|
|
], |
|
38
|
|
|
]; |
|
39
|
|
|
|
|
40
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
41
|
|
|
| Main Functions |
|
42
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
43
|
|
|
*/ |
|
44
|
|
|
/** |
|
45
|
|
|
* Handle the decode json errors. |
|
46
|
|
|
* |
|
47
|
|
|
* @param int $error |
|
48
|
|
|
* @param int $options |
|
49
|
|
|
* @param array $replaces |
|
50
|
|
|
* |
|
51
|
|
|
* @throws Exceptions\JsonException|Exceptions\Decode\DecodeException |
|
52
|
|
|
*/ |
|
53
|
33 |
|
public static function handleDecodeErrors($error, $options, array $replaces = []) |
|
54
|
|
|
{ |
|
55
|
33 |
|
unset($options); // Use the unused argument |
|
56
|
|
|
|
|
57
|
33 |
|
throw static::makeException('decode', $error, $replaces); |
|
|
|
|
|
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Handle the encode json errors. |
|
62
|
|
|
* |
|
63
|
|
|
* @param int $error |
|
64
|
|
|
* @param int $options |
|
65
|
|
|
* @param array $replaces |
|
66
|
|
|
* |
|
67
|
|
|
* @throws Exceptions\JsonException|Exceptions\Encode\EncodeException |
|
68
|
|
|
*/ |
|
69
|
30 |
|
public static function handleEncodeErrors($error, $options = 0, array $replaces = []) |
|
70
|
|
|
{ |
|
71
|
30 |
|
if (($options & JSON_PARTIAL_OUTPUT_ON_ERROR) === 0) { |
|
72
|
30 |
|
throw static::makeException('encode', $error, $replaces); |
|
|
|
|
|
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
77
|
|
|
| Other Functions |
|
78
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
79
|
|
|
*/ |
|
80
|
|
|
/** |
|
81
|
|
|
* Make an exception. |
|
82
|
|
|
* |
|
83
|
|
|
* @param string $type |
|
84
|
|
|
* @param int $error |
|
85
|
|
|
* @param array $replaces |
|
86
|
|
|
* |
|
87
|
|
|
* @return Exceptions\JsonException|Exceptions\Decode\DecodeException|Exceptions\Encode\EncodeException |
|
88
|
|
|
*/ |
|
89
|
63 |
|
private static function makeException($type, $error, array $replaces = []) |
|
90
|
|
|
{ |
|
91
|
|
|
/** @var \Arcanedev\Json\Exceptions\JsonException $ex */ |
|
92
|
63 |
|
$ex = array_key_exists($error, self::$errors[$type]['handled']) |
|
93
|
59 |
|
? new self::$errors[$type]['handled'][$error] |
|
94
|
63 |
|
: new self::$errors[$type]['default']; |
|
95
|
|
|
|
|
96
|
63 |
|
$ex->replace($replaces); |
|
97
|
|
|
|
|
98
|
63 |
|
return $ex; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
Let’s assume you have a class which uses late-static binding:
}
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClassto useselfinstead: