wotapi-8.x-1.x-dev/src/Object/Response.php
src/Object/Response.php
<?php
namespace Drupal\wotapi\Object;
use Drupal\Core\Cache\CacheableDependencyInterface;
use Drupal\Core\Cache\RefinableCacheableDependencyTrait;
/**
* Response object to help implement JSON RPC's spec for response objects.
*/
class Response implements CacheableDependencyInterface {
use RefinableCacheableDependencyTrait;
/**
* 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\wotapi\Object\Error
*/
protected $error;
/**
* Response constructor.
*
* @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\wotapi\Object\Error $error
* An error object if the response resulted in an error. Must not be
* provided if a result was provided.
*/
public function __construct($id, $result = NULL, Error $error = NULL) {
$this->id = $id;
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;
}
/**
* 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);
}
/**
* 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;
}
}
