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