From ff7a0b36a9ef40e2cf6d9c6d250b493bed1a6a47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A1bio=20Carneiro?= Date: Fri, 26 Feb 2021 15:30:16 +0100 Subject: [PATCH 1/3] allow throwables to be used --- src/ApiProblem/ApiProblem.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/ApiProblem/ApiProblem.php b/src/ApiProblem/ApiProblem.php index 95c54f6..c84941a 100644 --- a/src/ApiProblem/ApiProblem.php +++ b/src/ApiProblem/ApiProblem.php @@ -127,6 +127,18 @@ public function __construct($status, $detail, $title = '', $type = '', array $ad */ public static function fromException(Exception $exception, $title = '', $type = '', array $additionalDetails = []) { + return self::fromThrowable($exception, $title, $type, $additionalDetails); + } + + /** + * @param Throwable $exception + * @param string $title + * @param string $type + * @param array $additionalDetails + * + * @return ApiProblem + */ + public static function fromThrowable(Throwable $throwable, $title = '', $type = '', array $additionalDetails = []) { $eCode = $exception->getCode(); $code = (!empty($eCode) && is_int($eCode)) ? $eCode : 500; From de3f2f6ba9123e9bfcf82f3d1ba15fef5b15b76c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A1bio=20Carneiro?= Date: Fri, 26 Feb 2021 15:34:30 +0100 Subject: [PATCH 2/3] allow throwables to on the response transformer --- src/ApiProblem/ApiProblemResponse.php | 34 +++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/ApiProblem/ApiProblemResponse.php b/src/ApiProblem/ApiProblemResponse.php index dfe80cf..4daab0e 100644 --- a/src/ApiProblem/ApiProblemResponse.php +++ b/src/ApiProblem/ApiProblemResponse.php @@ -72,6 +72,23 @@ public static function fromExceptionToJson( ) { return new self(new JsonPresenter(ApiProblem::fromException($exception, $title, $type, $additionalDetails))); } + + /** + * @param Throwable $throwable + * @param string $title + * @param string $type + * @param array $additionalDetails + * + * @return ApiProblemResponse + */ + public static function froThrowableToJson( + Throwable $throwable, + $title = '', + $type = '', + array $additionalDetails = [] + ) { + return new self(new JsonPresenter(ApiProblem::fromThrowable($throwable, $title, $type, $additionalDetails))); + } /** * @param $status @@ -103,4 +120,21 @@ public static function fromExceptionToXml( ) { return new self(new XmlPresenter(ApiProblem::fromException($exception, $title, $type, $additionalDetails))); } + + /** + * @param Throwable $throwable + * @param string $title + * @param string $type + * @param array $additionalDetails + * + * @return ApiProblemResponse + */ + public static function fromThroableToXml( + Throwable $throwable, + $title = '', + $type = '', + array $additionalDetails = [] + ) { + return new self(new XmlPresenter(ApiProblem::fromThrowable($throwable, $title, $type, $additionalDetails))); + } } From e30a66278463b6f214792a23ede14035d1f3aefb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A1bio=20Carneiro?= Date: Fri, 26 Feb 2021 15:36:05 +0100 Subject: [PATCH 3/3] fix variable name on docblock --- src/ApiProblem/ApiProblem.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ApiProblem/ApiProblem.php b/src/ApiProblem/ApiProblem.php index c84941a..5fee9d9 100644 --- a/src/ApiProblem/ApiProblem.php +++ b/src/ApiProblem/ApiProblem.php @@ -131,7 +131,7 @@ public static function fromException(Exception $exception, $title = '', $type = } /** - * @param Throwable $exception + * @param Throwable $throwable * @param string $title * @param string $type * @param array $additionalDetails