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