Getter and setter for the payload and ability to pass options to json_encode

This commit is contained in:
Stanimir Stoyanov 2017-03-20 14:52:35 +02:00
parent 945624eb51
commit d73b15ae32
No known key found for this signature in database
GPG Key ID: 34DDBE1E036F7FEB

View File

@ -33,6 +33,11 @@ class OAuthServerException extends \Exception
*/ */
private $redirectUri; private $redirectUri;
/**
* @var array
*/
private $payload;
/** /**
* Throw a new exception. * Throw a new exception.
* *
@ -50,6 +55,33 @@ class OAuthServerException extends \Exception
$this->errorType = $errorType; $this->errorType = $errorType;
$this->hint = $hint; $this->hint = $hint;
$this->redirectUri = $redirectUri; $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 ResponseInterface $response
* @param bool $useFragment True if errors should be in the URI fragment instead of query string * @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 * @return ResponseInterface
*/ */
public function generateHttpResponse(ResponseInterface $response, $useFragment = false) public function generateHttpResponse(ResponseInterface $response, $useFragment = false, $jsonOptions = 0)
{ {
$headers = $this->getHttpHeaders(); $headers = $this->getHttpHeaders();
$payload = [ $payload = $this->getPayload();
'error' => $this->getErrorType(),
'message' => $this->getMessage(),
];
if ($this->hint !== null) {
$payload['hint'] = $this->hint;
}
if ($this->redirectUri !== null) { if ($this->redirectUri !== null) {
if ($useFragment === true) { if ($useFragment === true) {
@ -235,7 +261,7 @@ class OAuthServerException extends \Exception
$response = $response->withHeader($header, $content); $response = $response->withHeader($header, $content);
} }
$response->getBody()->write(json_encode($payload)); $response->getBody()->write(json_encode($payload, $jsonOptions));
return $response->withStatus($this->getHttpStatusCode()); return $response->withStatus($this->getHttpStatusCode());
} }