1 | <?php |
||
32 | class ErrorToException |
||
33 | { |
||
34 | /** |
||
35 | * @var CaptureErrorInterface |
||
36 | */ |
||
37 | protected $callable; |
||
38 | |||
39 | /** |
||
40 | * Exception class to instantiate |
||
41 | * |
||
42 | * @var string |
||
43 | */ |
||
44 | protected $exception_class_name; |
||
45 | |||
46 | /** |
||
47 | * A new instance |
||
48 | * |
||
49 | * @param CaptureErrorInterface $callable |
||
50 | * @param string $exception_class_name The exception to be thrown |
||
51 | */ |
||
52 | public function __construct(CaptureErrorInterface $callable, $exception_class_name = 'RuntimeException') |
||
53 | { |
||
54 | $this->callable = $callable; |
||
55 | $this->setExceptionClassName($exception_class_name); |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * Set the Exception class |
||
60 | * |
||
61 | * @param string $className error reporting level |
||
62 | * |
||
63 | * @throws InvalidArgumentException If the class is not a throwable object |
||
64 | */ |
||
65 | public function setExceptionClassName($className) |
||
66 | { |
||
67 | if (!$this->isThrowable($className) && !$this->isException($className)) { |
||
68 | throw new InvalidArgumentException(sprintf( |
||
69 | 'The class name provided `%s` is neither a Throwable nor an Exception object', |
||
70 | $className |
||
71 | )); |
||
72 | } |
||
73 | |||
74 | $this->exception_class_name = $className; |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * Is the ClassName a Throwable Object (PHP7+) |
||
79 | * |
||
80 | * @param string $className |
||
81 | * |
||
82 | * @return bool |
||
83 | */ |
||
84 | protected function isThrowable($className) |
||
88 | |||
89 | /** |
||
90 | * Is the ClassName an Exception Object (PHP5+) |
||
91 | * |
||
92 | * @param string $className |
||
93 | * |
||
94 | * @return bool |
||
95 | */ |
||
96 | protected function isException($className) |
||
100 | |||
101 | /** |
||
102 | * Get the current Error Reporting Level |
||
103 | * |
||
104 | * @return string |
||
105 | */ |
||
106 | public function getExceptionClassName() |
||
110 | |||
111 | /** |
||
112 | * Process the callable |
||
113 | * |
||
114 | * @param mixed ...$args the arguments associated with the callable |
||
115 | * |
||
116 | * @return mixed |
||
117 | */ |
||
118 | public function __invoke() |
||
131 | } |
||
132 |