|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Mpyw\Exceper; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class Exceper |
|
7
|
|
|
* |
|
8
|
|
|
* @method static mixed to(string $class, callable $callback, int $types = \E_ALL | \E_STRICT) |
|
9
|
|
|
* @method static mixed to(string $class, int $code, callable $callback, int $types = \E_ALL | \E_STRICT) |
|
10
|
|
|
* @method static mixed toException(callable $callback, int $types = \E_ALL | \E_STRICT) |
|
11
|
|
|
* @method static mixed toException(int $code, callable $callback, int $types = \E_ALL | \E_STRICT) |
|
12
|
|
|
* @method static mixed toErrorException(callable $callback, int $types = \E_ALL | \E_STRICT) |
|
13
|
|
|
* @method static mixed toErrorException(int $code, callable $callback, int $types = \E_ALL | \E_STRICT) |
|
14
|
|
|
* @method static mixed toError(callable $callback, int $types = \E_ALL | \E_STRICT) |
|
15
|
|
|
* @method static mixed toError(int $code, callable $callback, int $types = \E_ALL | \E_STRICT) |
|
16
|
|
|
* @method static mixed toArgumentCountError(callable $callback, int $types = \E_ALL | \E_STRICT) |
|
17
|
|
|
* @method static mixed toArgumentCountError(int $code, callable $callback, int $types = \E_ALL | \E_STRICT) |
|
18
|
|
|
* @method static mixed toArithmeticError(callable $callback, int $types = \E_ALL | \E_STRICT) |
|
19
|
|
|
* @method static mixed toArithmeticError(int $code, callable $callback, int $types = \E_ALL | \E_STRICT) |
|
20
|
|
|
* @method static mixed toAssertionError(callable $callback, int $types = \E_ALL | \E_STRICT) |
|
21
|
|
|
* @method static mixed toAssertionError(int $code, callable $callback, int $types = \E_ALL | \E_STRICT) |
|
22
|
|
|
* @method static mixed toDivisionByZeroError(callable $callback, int $types = \E_ALL | \E_STRICT) |
|
23
|
|
|
* @method static mixed toDivisionByZeroError(int $code, callable $callback, int $types = \E_ALL | \E_STRICT) |
|
24
|
|
|
* @method static mixed toParseError(callable $callback, int $types = \E_ALL | \E_STRICT) |
|
25
|
|
|
* @method static mixed toParseError(int $code, callable $callback, int $types = \E_ALL | \E_STRICT) |
|
26
|
|
|
* @method static mixed toTypeError(callable $callback, int $types = \E_ALL | \E_STRICT) |
|
27
|
|
|
* @method static mixed toTypeError(int $code, callable $callback, int $types = \E_ALL | \E_STRICT) |
|
28
|
|
|
* @method static mixed toClosedGeneratorException(callable $callback, int $types = \E_ALL | \E_STRICT) |
|
29
|
|
|
* @method static mixed toClosedGeneratorException(int $code, callable $callback, int $types = \E_ALL | \E_STRICT) |
|
30
|
|
|
* @method static mixed toDOMException(callable $callback, int $types = \E_ALL | \E_STRICT) |
|
31
|
|
|
* @method static mixed toDOMException(int $code, callable $callback, int $types = \E_ALL | \E_STRICT) |
|
32
|
|
|
* @method static mixed toLogicException(callable $callback, int $types = \E_ALL | \E_STRICT) |
|
33
|
|
|
* @method static mixed toLogicException(int $code, callable $callback, int $types = \E_ALL | \E_STRICT) |
|
34
|
|
|
* @method static mixed toBadFunctionCallException(callable $callback, int $types = \E_ALL | \E_STRICT) |
|
35
|
|
|
* @method static mixed toBadFunctionCallException(int $code, callable $callback, int $types = \E_ALL | \E_STRICT) |
|
36
|
|
|
* @method static mixed toBadMethodCallException(callable $callback, int $types = \E_ALL | \E_STRICT) |
|
37
|
|
|
* @method static mixed toBadMethodCallException(int $code, callable $callback, int $types = \E_ALL | \E_STRICT) |
|
38
|
|
|
* @method static mixed toDomainException(callable $callback, int $types = \E_ALL | \E_STRICT) |
|
39
|
|
|
* @method static mixed toDomainException(int $code, callable $callback, int $types = \E_ALL | \E_STRICT) |
|
40
|
|
|
* @method static mixed toInvalidArgumentException(callable $callback, int $types = \E_ALL | \E_STRICT) |
|
41
|
|
|
* @method static mixed toInvalidArgumentException(int $code, callable $callback, int $types = \E_ALL | \E_STRICT) |
|
42
|
|
|
* @method static mixed toLengthException(callable $callback, int $types = \E_ALL | \E_STRICT) |
|
43
|
|
|
* @method static mixed toLengthException(int $code, callable $callback, int $types = \E_ALL | \E_STRICT) |
|
44
|
|
|
* @method static mixed toOutOfRangeException(callable $callback, int $types = \E_ALL | \E_STRICT) |
|
45
|
|
|
* @method static mixed toOutOfRangeException(int $code, callable $callback, int $types = \E_ALL | \E_STRICT) |
|
46
|
|
|
* @method static mixed toRuntimeException(callable $callback, int $types = \E_ALL | \E_STRICT) |
|
47
|
|
|
* @method static mixed toRuntimeException(int $code, callable $callback, int $types = \E_ALL | \E_STRICT) |
|
48
|
|
|
* @method static mixed toOutOfBoundsException(callable $callback, int $types = \E_ALL | \E_STRICT) |
|
49
|
|
|
* @method static mixed toOutOfBoundsException(int $code, callable $callback, int $types = \E_ALL | \E_STRICT) |
|
50
|
|
|
* @method static mixed toOverflowException(callable $callback, int $types = \E_ALL | \E_STRICT) |
|
51
|
|
|
* @method static mixed toOverflowException(int $code, callable $callback, int $types = \E_ALL | \E_STRICT) |
|
52
|
|
|
* @method static mixed toRangeException(callable $callback, int $types = \E_ALL | \E_STRICT) |
|
53
|
|
|
* @method static mixed toRangeException(int $code, callable $callback, int $types = \E_ALL | \E_STRICT) |
|
54
|
|
|
* @method static mixed toUnderflowException(callable $callback, int $types = \E_ALL | \E_STRICT) |
|
55
|
|
|
* @method static mixed toUnderflowException(int $code, callable $callback, int $types = \E_ALL | \E_STRICT) |
|
56
|
|
|
* @method static mixed toUnexpectedValueException(callable $callback, int $types = \E_ALL | \E_STRICT) |
|
57
|
|
|
* @method static mixed toUnexpectedValueException(int $code, callable $callback, int $types = \E_ALL | \E_STRICT) |
|
58
|
|
|
* @method static mixed toIntlException(callable $callback, int $types = \E_ALL | \E_STRICT) |
|
59
|
|
|
* @method static mixed toIntlException(int $code, callable $callback, int $types = \E_ALL | \E_STRICT) |
|
60
|
|
|
* @method static mixed tomysqli_sql_exception(callable $callback, int $types = \E_ALL | \E_STRICT) |
|
61
|
|
|
* @method static mixed tomysqli_sql_exception(int $code, callable $callback, int $types = \E_ALL | \E_STRICT) |
|
62
|
|
|
* @method static mixed toPDOException(callable $callback, int $types = \E_ALL | \E_STRICT) |
|
63
|
|
|
* @method static mixed toPDOException(int $code, callable $callback, int $types = \E_ALL | \E_STRICT) |
|
64
|
|
|
* @method static mixed toPharException(callable $callback, int $types = \E_ALL | \E_STRICT) |
|
65
|
|
|
* @method static mixed toPharException(int $code, callable $callback, int $types = \E_ALL | \E_STRICT) |
|
66
|
|
|
* @method static mixed toReflectionException(callable $callback, int $types = \E_ALL | \E_STRICT) |
|
67
|
|
|
* @method static mixed toReflectionException(int $code, callable $callback, int $types = \E_ALL | \E_STRICT) |
|
68
|
|
|
* @method static mixed toSNMPException(callable $callback, int $types = \E_ALL | \E_STRICT) |
|
69
|
|
|
* @method static mixed toSNMPException(int $code, callable $callback, int $types = \E_ALL | \E_STRICT) |
|
70
|
|
|
* @method static mixed toSoapFault(callable $callback, int $types = \E_ALL | \E_STRICT) |
|
71
|
|
|
* @method static mixed toSoapFault(int $code, callable $callback, int $types = \E_ALL | \E_STRICT) |
|
72
|
|
|
*/ |
|
73
|
|
|
class Convert |
|
74
|
|
|
{ |
|
75
|
|
|
/** |
|
76
|
|
|
* @param string $method |
|
77
|
|
|
* @param array $args |
|
78
|
|
|
* @param int $required |
|
79
|
|
|
* @param int &$code |
|
80
|
|
|
*/ |
|
81
|
33 |
|
protected static function validateArgumentTypes($method, array $args, $required, &$code = null) |
|
82
|
|
|
{ |
|
83
|
33 |
|
$argc = \count($args); |
|
84
|
33 |
|
if ($argc < $required) { |
|
85
|
3 |
|
throw new \InvalidArgumentException(\get_called_class() . "::$method() expects at least $required parameters, $argc given"); |
|
86
|
|
|
} |
|
87
|
30 |
|
if ($required === 2 and !\is_string($args[0]) and !\is_object($args[0]) || !\method_exists($args[0], '__toString')) { |
|
88
|
2 |
|
throw new \InvalidArgumentException(\get_called_class() . "::$method() expects parameter 1 to be string, " . \gettype($args[0]) . ' given'); |
|
89
|
|
|
} |
|
90
|
28 |
|
if (isset($args[$required - 1]) && \is_numeric($args[$required - 1])) { |
|
91
|
14 |
|
$code = $args[$required++ - 1]; |
|
92
|
|
|
} |
|
93
|
28 |
|
$orInteger = $code === null ? " or integer" : ''; |
|
94
|
28 |
|
if (!\array_key_exists($required - 1, $args)) { |
|
95
|
2 |
|
throw new \InvalidArgumentException(\get_called_class() . "::$method() expects parameter $required to be callable$orInteger, none given"); |
|
96
|
|
|
} |
|
97
|
26 |
|
if (!\is_callable($args[$required - 1])) { |
|
98
|
4 |
|
throw new \InvalidArgumentException(\get_called_class() . "::$method() expects parameter $required to be callable$orInteger, " . \gettype($args[$required - 1]) . ' given'); |
|
99
|
|
|
} |
|
100
|
22 |
|
if (isset($args[$required]) && !\is_numeric($args[$required])) { |
|
101
|
4 |
|
throw new \InvalidArgumentException(\get_called_class() . "::$method() expects parameter " . ($required + 1) . " to be integer, " . \gettype($args[$required]) . ' given'); |
|
102
|
|
|
} |
|
103
|
18 |
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @param string $method |
|
107
|
|
|
* @param array $args |
|
108
|
|
|
* @return mixed |
|
109
|
|
|
*/ |
|
110
|
34 |
|
public static function __callStatic($method, array $args) |
|
111
|
|
|
{ |
|
112
|
34 |
|
if (\strcasecmp(\substr($method, 0, 2), 'to')) { |
|
113
|
1 |
|
throw new \BadMethodCallException('Call to undefined method ' . \get_called_class() . "::$method()"); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
33 |
|
$class = (string)\substr($method, 2); |
|
117
|
33 |
|
$required = $class === '' ? 2 : 1; |
|
118
|
33 |
|
self::validateArgumentTypes($method, $args, $required, $code); |
|
119
|
|
|
|
|
120
|
18 |
|
if ($class === '') { |
|
121
|
10 |
|
$class = (string)\array_shift($args); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
18 |
|
if (!\class_exists($class) and !\is_subclass_of($class, '\Exception') and \PHP_VERSION < 7 || !\is_subclass_of($class, '\Throwable')) { |
|
125
|
2 |
|
throw new \DomainException("The class \"$class\" must be an instance of Exception or Throwable"); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
return Core::handle($args[$code !== null], function ($severity, $message, $file, $line) use ($class, $code) { |
|
129
|
11 |
|
if (!(\error_reporting() & $severity)) { |
|
130
|
2 |
|
return; |
|
131
|
|
|
} |
|
132
|
9 |
|
if (\strcasecmp($class, 'ErrorException') && \strcasecmp($class, '\ErrorException')) { |
|
133
|
8 |
|
throw Core::rewriteLocation(new $class($message, $code ?: 0), $file, $line); |
|
134
|
|
|
} else { |
|
135
|
1 |
|
throw new \ErrorException($message, $code ?: 0, $severity, $file, $line); |
|
136
|
|
|
} |
|
137
|
16 |
|
}, isset($args[1 + ($code !== null)]) ? (int)$args[1 + ($code !== null)] : \E_ALL | \E_STRICT); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @param callable $callback |
|
142
|
|
|
* @param int $types |
|
143
|
|
|
* @param bool $captureExceptions |
|
144
|
|
|
* @return mixed |
|
145
|
|
|
*/ |
|
146
|
6 |
|
public static function silent($callback, $types = null, $captureExceptions = true) |
|
147
|
|
|
{ |
|
148
|
6 |
|
if (!is_callable($callback)) { |
|
149
|
1 |
|
throw new \InvalidArgumentException(get_called_class() . '::' . __METHOD__ . '() expects parameter 1 to be callable, ' . \gettype($callback) . ' given'); |
|
150
|
|
|
} |
|
151
|
5 |
|
if ($types !== null && !is_numeric($types)) { |
|
|
|
|
|
|
152
|
1 |
|
throw new \InvalidArgumentException(get_called_class() . '::' . __METHOD__ . '() expects parameter 2 to be integer, ' . \gettype($types) . ' given'); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
try { |
|
156
|
|
|
return Core::handle($callback, function ($severity, $message, $file, $line) use (&$errorException) { |
|
157
|
3 |
|
if (!(\error_reporting() & $severity)) { |
|
158
|
1 |
|
return; |
|
159
|
|
|
} |
|
160
|
3 |
|
throw $errorException = new \ErrorException($message, 0, $severity, $file, $line); |
|
161
|
4 |
|
}, (int)($types === null ? \E_ALL | \E_STRICT : $types)); |
|
162
|
4 |
|
} catch (\Exception $e) { |
|
|
|
|
|
|
163
|
1 |
|
} catch (\Throwable $e) { |
|
|
|
|
|
|
164
|
|
|
} |
|
165
|
4 |
|
if (!$captureExceptions && $e !== $errorException) { |
|
|
|
|
|
|
166
|
1 |
|
throw $e; |
|
167
|
|
|
} |
|
168
|
4 |
|
} |
|
169
|
|
|
} |
|
170
|
|
|
|