You are here

class RpcResponseFactory in JSON-RPC 8

Same name and namespace in other branches
  1. 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\Shaper
View 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

Namesort descending Modifiers Type Description Overrides
RpcResponseFactory::$outputValidator protected property The output validator, based on the JSON Schema.
RpcResponseFactory::$validator protected property The JSON Schema validator.
RpcResponseFactory::doNormalize protected function Performs the actual normalization.
RpcResponseFactory::doTransform protected function
RpcResponseFactory::getInputValidator public function
RpcResponseFactory::getOutputValidator public function
RpcResponseFactory::REQUEST_IS_BATCH_REQUEST constant
RpcResponseFactory::RESPONSE_VERSION_KEY constant
RpcResponseFactory::setOutputSchema public function Sets the schema for the response output.
RpcResponseFactory::__construct public function RpcResponseFactory constructor.