class RpcResponseFactory in JSON-RPC 8
Same name and namespace in other branches
- 2.x src/Shaper/RpcResponseFactory.php \Drupal\jsonrpc\Shaper\RpcResponseFactory
Creates RPC Response objects.
Hierarchy
- class \Drupal\jsonrpc\Shaper\RpcResponseFactory extends \Shaper\Transformation\TransformationBase
Expanded class hierarchy of RpcResponseFactory
1 file declares its use of RpcResponseFactory
- HttpController.php in src/
Controller/ HttpController.php
File
- src/
Shaper/ RpcResponseFactory.php, line 20
Namespace
Drupal\jsonrpc\ShaperView source
class RpcResponseFactory extends TransformationBase {
const RESPONSE_VERSION_KEY = RpcRequestFactory::REQUEST_VERSION_KEY;
const REQUEST_IS_BATCH_REQUEST = RpcRequestFactory::REQUEST_IS_BATCH_REQUEST;
/**
* The JSON Schema validator.
*
* @var \JsonSchema\Validator
*/
protected $validator;
/**
* The output validator, based on the JSON Schema.
*
* @var \Shaper\Validator\ValidateableInterface
*/
protected $outputValidator;
/**
* RpcResponseFactory constructor.
*
* @param \JsonSchema\Validator $validator
* The JSON Schema validator.
*/
public function __construct(Validator $validator) {
$this->validator = $validator;
}
/**
* {@inheritdoc}
*/
public function getInputValidator() {
return new CollectionOfValidators(new InstanceofValidator(Response::class));
}
/**
* {@inheritdoc}
*/
public function getOutputValidator() {
return $this->outputValidator ? $this->outputValidator : new AcceptValidator();
}
/**
* Sets the schema for the response output.
*
* @param array|null $result_schema
* The array of the response.
*/
public function setOutputSchema($result_schema) {
$schema = Json::decode(file_get_contents(__DIR__ . '/response-schema.json'));
$schema['properties']['result'] = $result_schema;
$this->outputValidator = new JsonSchemaValidator($schema, $this->validator, Constraint::CHECK_MODE_TYPE_CAST);
}
/**
* {@inheritdoc}
*/
protected function doTransform($data, Context $context) {
$this
->setOutputSchema($data[0]
->getResultSchema());
$output = array_map(function (Response $response) use ($context) {
try {
return $this
->doNormalize($response, $context);
} catch (\Exception $e) {
return JsonRpcException::fromPrevious($e, $response
->id(), $context[static::RESPONSE_VERSION_KEY]);
}
}, $data);
return $context[static::REQUEST_IS_BATCH_REQUEST] ? $output : reset($output);
}
/**
* Performs the actual normalization.
*
* @param \Drupal\jsonrpc\Object\Response $response
* The RPC Response object to return.
* @param \Shaper\Util\Context $context
* The context object.
*
* @return array
* The normalized response.
*/
protected function doNormalize(Response $response, Context $context) {
$normalized = [
'jsonrpc' => $context[static::RESPONSE_VERSION_KEY],
'id' => $response
->id(),
];
if ($response
->isResultResponse()) {
$normalized['result'] = $response
->getResult();
}
if ($response
->isErrorResponse()) {
$error = $response
->getError();
$normalized['error'] = [
'code' => $error
->getCode(),
'message' => $error
->getMessage(),
'data' => $error
->getData(),
];
}
return $normalized;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
RpcResponseFactory:: |
protected | property | The output validator, based on the JSON Schema. | |
RpcResponseFactory:: |
protected | property | The JSON Schema validator. | |
RpcResponseFactory:: |
protected | function | Performs the actual normalization. | |
RpcResponseFactory:: |
protected | function | ||
RpcResponseFactory:: |
public | function | ||
RpcResponseFactory:: |
public | function | ||
RpcResponseFactory:: |
constant | |||
RpcResponseFactory:: |
constant | |||
RpcResponseFactory:: |
public | function | Sets the schema for the response output. | |
RpcResponseFactory:: |
public | function | RpcResponseFactory constructor. |