From d73b15ae320af678fb7e73a42bcf255e7248e729 Mon Sep 17 00:00:00 2001 From: Stanimir Stoyanov Date: Mon, 20 Mar 2017 14:52:35 +0200 Subject: [PATCH] Getter and setter for the payload and ability to pass options to json_encode --- src/Exception/OAuthServerException.php | 46 ++++++++++++++++++++------ 1 file changed, 36 insertions(+), 10 deletions(-) diff --git a/src/Exception/OAuthServerException.php b/src/Exception/OAuthServerException.php index 6ffa0fb1..f0b5fef5 100644 --- a/src/Exception/OAuthServerException.php +++ b/src/Exception/OAuthServerException.php @@ -33,6 +33,11 @@ class OAuthServerException extends \Exception */ private $redirectUri; + /** + * @var array + */ + private $payload; + /** * Throw a new exception. * @@ -50,6 +55,33 @@ class OAuthServerException extends \Exception $this->errorType = $errorType; $this->hint = $hint; $this->redirectUri = $redirectUri; + $this->payload = [ + 'error' => $errorType, + 'message' => $message, + ]; + if ($hint !== null) { + $this->payload['hint'] = $hint; + } + } + + /** + * Returns the current payload. + * + * @return array + */ + public function getPayload() + { + return $this->payload; + } + + /** + * Updates the current payload. + * + * @param array $payload + */ + public function setPayload(array $payload) + { + $this->payload = $payload; } /** @@ -205,21 +237,15 @@ class OAuthServerException extends \Exception * * @param ResponseInterface $response * @param bool $useFragment True if errors should be in the URI fragment instead of query string + * @param int $jsonOptions options passed to json_encode * * @return ResponseInterface */ - public function generateHttpResponse(ResponseInterface $response, $useFragment = false) + public function generateHttpResponse(ResponseInterface $response, $useFragment = false, $jsonOptions = 0) { $headers = $this->getHttpHeaders(); - $payload = [ - 'error' => $this->getErrorType(), - 'message' => $this->getMessage(), - ]; - - if ($this->hint !== null) { - $payload['hint'] = $this->hint; - } + $payload = $this->getPayload(); if ($this->redirectUri !== null) { if ($useFragment === true) { @@ -235,7 +261,7 @@ class OAuthServerException extends \Exception $response = $response->withHeader($header, $content); } - $response->getBody()->write(json_encode($payload)); + $response->getBody()->write(json_encode($payload, $jsonOptions)); return $response->withStatus($this->getHttpStatusCode()); }