You are here

class Response in JSON-RPC 2.x

Same name and namespace in other branches
  1. 8 src/Object/Response.php \Drupal\jsonrpc\Object\Response

Response object to help implement JSON RPC's spec for response objects.

Hierarchy

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

... See full list

File

src/Object/Response.php, line 12

Namespace

Drupal\jsonrpc\Object
View 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

Namesort descending Modifiers Type Description Overrides
CacheableDependencyTrait::$cacheContexts protected property Cache contexts.
CacheableDependencyTrait::$cacheMaxAge protected property Cache max-age.
CacheableDependencyTrait::$cacheTags protected property Cache tags.
CacheableDependencyTrait::getCacheContexts public function 4
CacheableDependencyTrait::getCacheMaxAge public function 4
CacheableDependencyTrait::getCacheTags public function 4
CacheableDependencyTrait::setCacheability protected function Sets cacheability; useful for value object constructors.
RefinableCacheableDependencyTrait::addCacheableDependency public function 1
RefinableCacheableDependencyTrait::addCacheContexts public function
RefinableCacheableDependencyTrait::addCacheTags public function
RefinableCacheableDependencyTrait::mergeCacheMaxAge public function
Response::$error protected property The error.
Response::$headers protected property The headers for the response.
Response::$id protected property A string, number or NULL ID.
Response::$result protected property The result.
Response::$resultSchema protected property The schema for the result.
Response::$version protected property The JSON-RPC version.
Response::assertValidResponse protected function Asserts that the response is valid.
Response::getError public function Get the error of the response.
Response::getHeaders public function The headers for this response.
Response::getResult public function Get the result of the response.
Response::getResultSchema public function The schema of the output response.
Response::id public function Gets the ID.
Response::isErrorResponse public function Checks if this is an error or result response.
Response::isResultResponse public function Checks if this is an error or result response.
Response::setResultSchema public function Sets the schema for the output response.
Response::version public function Gets the version.
Response::__construct public function Response constructor.