src/Response.php
public static function create($result) {
        if ($result instanceof Response) {
            return $result;
        } elseif ($result instanceof Exception\ResponseException) {
            /* @var Exception\ResponseException $result */
            return $result->getResponse();
        }

        $response = new Response();

        if ($result instanceof Exception\ClientException) {
            /* @var Exception\ClientException $cex */
            $cex = $result;
            $response->status($cex->getCode());
            $response->headers($cex->getHeaders());
            $response->data($cex->jsonSerialize());
        } elseif ($result instanceof \Exception) {
            /* @var \Exception $ex */
            $ex = $result;
            $response->status($ex->getCode());
            $response->data([
                'exception' => $ex->getMessage(),
                'code' => $ex->getCode()
            ]);
        } elseif (is_array($result)) {
            if (count($result) === 3 && isset($result[0], $result[1], $result[2])) {
                // This is a rack style response in the form [code, headers, body].
                $response->status($result[0]);
                $response->headers($result[1]);
                $response->data($result[2]);
            } elseif (array_key_exists('response', $result)) {
                $resultResponse = $result['response'];
                if (!$resultResponse) {
                    $response->data($result['body']);
                } else {
                    // This is a dispatched response.
                    $response = static::create($resultResponse);
                }

                // Set the rest of the result to the response context.
                unset($result['response']);
                $response->meta($result, true);
            } else {
                $response->data($result);
            }
        } else {
            $response->status(422);
            $response->data([
                'exception' => "Unknown result type for response.",
                'code' => $response->status()
            ]);
        }
        return $response;
    }
public static function create($result) {
        if ($result instanceof Response) {
            return $result;
        } elseif ($result instanceof Exception\ResponseException) {
            /* @var Exception\ResponseException $result */
            return $result->getResponse();
        }

        $response = new Response();

        if ($result instanceof Exception\ClientException) {
            /* @var Exception\ClientException $cex */
            $cex = $result;
            $response->status($cex->getCode());
            $response->headers($cex->getHeaders());
            $response->data($cex->jsonSerialize());
        } elseif ($result instanceof \Exception) {
            /* @var \Exception $ex */
            $ex = $result;
            $response->status($ex->getCode());
            $response->data([
                'exception' => $ex->getMessage(),
                'code' => $ex->getCode()
            ]);
        } elseif (is_array($result)) {
            if (count($result) === 3 && isset($result[0], $result[1], $result[2])) {
                // This is a rack style response in the form [code, headers, body].
                $response->status($result[0]);
                $response->headers($result[1]);
                $response->data($result[2]);
            } elseif (array_key_exists('response', $result)) {
                $resultResponse = $result['response'];
                if (!$resultResponse) {
                    $response->data($result['body']);
                } else {
                    // This is a dispatched response.
                    $response = static::create($resultResponse);
                }

                // Set the rest of the result to the response context.
                unset($result['response']);
                $response->meta($result, true);
            } else {
                $response->data($result);
            }
        } else {
            $response->status(422);
            $response->data([
                'exception' => "Unknown result type for response.",
                'code' => $response->status()
            ]);
        }
        return $response;
    }
Create a Response from a variety of data.

@param mixed $result The result to create the response from.
@return Response Returns a {@link Response} object.