22
33/**
44 * @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
5- * @copyright Copyright (c) 2014 Zend Technologies USA Inc. (http://www.zend.com)
5+ * @copyright Copyright (c) 2014-2016 Zend Technologies USA Inc. (http://www.zend.com)
66 */
77
88namespace ZF \ApiProblem ;
99
10+ use Exception ;
11+ use Throwable ;
12+ use ZF \ApiProblem \Exception \InvalidArgumentException ;
13+ use ZF \ApiProblem \Exception \ProblemExceptionInterface ;
14+
1015/**
1116 * Object describing an API-Problem payload.
1217 */
@@ -34,7 +39,7 @@ class ApiProblem
3439 /**
3540 * Description of the specific problem.
3641 *
37- * @var string|\ Exception
42+ * @var string|Exception|Throwable
3843 */
3944 protected $ detail = '' ;
4045
@@ -128,14 +133,14 @@ class ApiProblem
128133 * from $problemStatusTitles as a result.
129134 *
130135 * @param int $status
131- * @param string $detail
136+ * @param string|Exception|Throwable $detail
132137 * @param string $type
133138 * @param string $title
134139 * @param array $additional
135140 */
136141 public function __construct ($ status , $ detail , $ type = null , $ title = null , array $ additional = [])
137142 {
138- if ($ detail instanceof Exception \ ProblemExceptionInterface) {
143+ if ($ detail instanceof ProblemExceptionInterface) {
139144 if (null === $ type ) {
140145 $ type = $ detail ->getType ();
141146 }
@@ -170,10 +175,8 @@ public function __construct($status, $detail, $type = null, $title = null, array
170175 * Retrieve properties.
171176 *
172177 * @param string $name
173- *
174178 * @return mixed
175- *
176- * @throws Exception\InvalidArgumentException
179+ * @throws InvalidArgumentException
177180 */
178181 public function __get ($ name )
179182 {
@@ -192,7 +195,7 @@ public function __get($name)
192195 return $ this ->additionalDetails [$ normalized ];
193196 }
194197
195- throw new Exception \ InvalidArgumentException (sprintf (
198+ throw new InvalidArgumentException (sprintf (
196199 'Invalid property name "%s" ' ,
197200 $ name
198201 ));
@@ -220,7 +223,6 @@ public function toArray()
220223 * stack trace and previous exception information.
221224 *
222225 * @param bool $flag
223- *
224226 * @return ApiProblem
225227 */
226228 public function setDetailIncludesStackTrace ($ flag )
@@ -240,7 +242,7 @@ public function setDetailIncludesStackTrace($flag)
240242 */
241243 protected function getDetail ()
242244 {
243- if ($ this ->detail instanceof \ Exception) {
245+ if ($ this ->detail instanceof Throwable || $ this -> detail instanceof Exception) {
244246 return $ this ->createDetailFromException ();
245247 }
246248
@@ -257,7 +259,7 @@ protected function getDetail()
257259 */
258260 protected function getStatus ()
259261 {
260- if ($ this ->detail instanceof \ Exception) {
262+ if ($ this ->detail instanceof Throwable || $ this -> detail instanceof Exception) {
261263 $ this ->status = $ this ->createStatusFromException ();
262264 }
263265
@@ -290,7 +292,7 @@ protected function getTitle()
290292 return $ this ->problemStatusTitles [$ this ->status ];
291293 }
292294
293- if ($ this ->detail instanceof \ Exception) {
295+ if ($ this ->detail instanceof Throwable || $ this -> detail instanceof Exception) {
294296 return get_class ($ this ->detail );
295297 }
296298
@@ -308,6 +310,7 @@ protected function getTitle()
308310 */
309311 protected function createDetailFromException ()
310312 {
313+ /** @var Exception|Throwable $e */
311314 $ e = $ this ->detail ;
312315
313316 if (!$ this ->detailIncludesStackTrace ) {
@@ -337,17 +340,18 @@ protected function createDetailFromException()
337340 /**
338341 * Create HTTP status from an exception.
339342 *
340- * @return string
343+ * @return int
341344 */
342345 protected function createStatusFromException ()
343346 {
347+ /** @var Exception|Throwable $e */
344348 $ e = $ this ->detail ;
345349 $ status = $ e ->getCode ();
346350
347- if (! empty ( $ status) ) {
351+ if ($ status ) {
348352 return $ status ;
349- } else {
350- return 500 ;
351353 }
354+
355+ return 500 ;
352356 }
353357}
0 commit comments