class Response in JSON-RPC 2.x
Same name and namespace in other branches
- 8 src/Object/Response.php \Drupal\jsonrpc\Object\Response
Response object to help implement JSON RPC's spec for response objects.
Hierarchy
- class \Drupal\jsonrpc\Object\Response implements CacheableDependencyInterface uses RefinableCacheableDependencyTrait
Expanded class hierarchy of Response
6 files declare their use of Response
- Handler.php in src/
Handler.php - HttpController.php in src/
Controller/ HttpController.php - JsonRpcException.php in src/
Exception/ JsonRpcException.php - RpcResponseFactory.php in src/
Shaper/ RpcResponseFactory.php - SecondMethod.php in tests/
modules/ jsonrpc_test/ src/ Plugin/ jsonrpc/ Method/ SecondMethod.php
File
- src/
Object/ Response.php, line 12
Namespace
Drupal\jsonrpc\ObjectView source
class Response implements CacheableDependencyInterface {
use RefinableCacheableDependencyTrait;
/**
* The JSON-RPC version.
*
* @var string
*/
protected $version;
/**
* A string, number or NULL ID.
*
* @var mixed
*/
protected $id;
/**
* The result.
*
* @var mixed
*/
protected $result;
/**
* The schema for the result.
*
* @var null|array
*/
protected $resultSchema;
/**
* The error.
*
* @var \Drupal\jsonrpc\Object\Error
*/
protected $error;
/**
* The headers for the response.
*
* @var \Symfony\Component\HttpFoundation\HeaderBag
*/
protected $headers;
/**
* Response constructor.
*
* @param string $version
* The JSON-RPC version.
* @param mixed $id
* The response ID. Must match the ID of the generating request.
* @param mixed $result
* A result value. Must not be provided if an error is to be provided.
* @param \Drupal\jsonrpc\Object\Error|null $error
* An error object if the response resulted in an error. Must not be
* provided if a result was provided.
*/
public function __construct($version, $id, $result = NULL, Error $error = NULL, HeaderBag $headers = NULL) {
$this
->assertValidResponse($version, $id, $result, $error);
$this->version = $version;
$this->id = $id;
$this->headers = $headers ?? new HeaderBag();
if (!is_null($result)) {
$this->result = $result;
}
else {
$this->error = $error;
$this
->setCacheability($error);
}
}
/**
* Gets the ID.
*
* @return mixed
* The ID.
*/
public function id() {
return $this->id;
}
/**
* Gets the version.
*
* @return string
* The version.
*/
public function version() {
return $this->version;
}
/**
* Get the result of the response.
*
* @return mixed
* The result of the response.
*/
public function getResult() {
return $this->result;
}
/**
* Get the error of the response.
*
* @return mixed
* The error of the response.
*/
public function getError() {
return $this->error;
}
/**
* Checks if this is an error or result response.
*
* @return bool
* True if it's a result response.
*/
public function isResultResponse() {
return !$this
->isErrorResponse();
}
/**
* Checks if this is an error or result response.
*
* @return bool
* True if it's an error response.
*/
public function isErrorResponse() {
return isset($this->error);
}
/**
* Asserts that the response is valid.
*/
protected function assertValidResponse($version, $id, $result, $error) {
assert(!is_null($result) xor !is_null($error), 'Either the result member or error member MUST be included, but both members MUST NOT be included.');
assert($version === "2.0", 'A String specifying the version of the JSON-RPC protocol. MUST be exactly "2.0".');
assert(is_string($id) || is_numeric($id) || is_null($id), 'An identifier established by the Client that MUST contain a String, Number, or NULL value if included.');
}
/**
* The schema of the output response.
*
* @return array|null
* The result schema.
*/
public function getResultSchema() {
return $this->resultSchema;
}
/**
* Sets the schema for the output response.
*
* @param array|null $result_schema
* The schema of the result.
*/
public function setResultSchema($result_schema) {
$this->resultSchema = $result_schema;
}
/**
* The headers for this response.
*
* @return \Symfony\Component\HttpFoundation\HeaderBag
* The header bag object.
*/
public function getHeaders() : HeaderBag {
return $this->headers;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CacheableDependencyTrait:: |
protected | property | Cache contexts. | |
CacheableDependencyTrait:: |
protected | property | Cache max-age. | |
CacheableDependencyTrait:: |
protected | property | Cache tags. | |
CacheableDependencyTrait:: |
public | function | 4 | |
CacheableDependencyTrait:: |
public | function | 4 | |
CacheableDependencyTrait:: |
public | function | 4 | |
CacheableDependencyTrait:: |
protected | function | Sets cacheability; useful for value object constructors. | |
RefinableCacheableDependencyTrait:: |
public | function | 1 | |
RefinableCacheableDependencyTrait:: |
public | function | ||
RefinableCacheableDependencyTrait:: |
public | function | ||
RefinableCacheableDependencyTrait:: |
public | function | ||
Response:: |
protected | property | The error. | |
Response:: |
protected | property | The headers for the response. | |
Response:: |
protected | property | A string, number or NULL ID. | |
Response:: |
protected | property | The result. | |
Response:: |
protected | property | The schema for the result. | |
Response:: |
protected | property | The JSON-RPC version. | |
Response:: |
protected | function | Asserts that the response is valid. | |
Response:: |
public | function | Get the error of the response. | |
Response:: |
public | function | The headers for this response. | |
Response:: |
public | function | Get the result of the response. | |
Response:: |
public | function | The schema of the output response. | |
Response:: |
public | function | Gets the ID. | |
Response:: |
public | function | Checks if this is an error or result response. | |
Response:: |
public | function | Checks if this is an error or result response. | |
Response:: |
public | function | Sets the schema for the output response. | |
Response:: |
public | function | Gets the version. | |
Response:: |
public | function | Response constructor. |