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