diff --git a/src/ApiProblem.php b/src/ApiProblem.php index 69ec6c2..b9d5e4c 100644 --- a/src/ApiProblem.php +++ b/src/ApiProblem.php @@ -7,6 +7,7 @@ use ApiSkeletons\Laravel\ApiProblem\Exception\InvalidArgumentException; use ApiSkeletons\Laravel\ApiProblem\Exception\ProblemExceptionInterface; use Exception; +use Illuminate\Http\Response; use Throwable; use function array_key_exists; @@ -17,6 +18,7 @@ use function get_class; use function in_array; use function is_numeric; +use function response; use function sprintf; use function strtolower; use function trim; @@ -213,6 +215,16 @@ public function toArray(): array return array_merge($this->additionalDetails, $problem); } + /** + * Compose a response and return it. + */ + public function response(): Response + { + return response() + ->setStatusCode($this->getStatus()) + ->setContent($this->toArray()); + } + /** * Set the flag indicating whether an exception detail should include a * stack trace and previous exception information.