You are here

protected function RpcRequestFactory::denormalizeParams in JSON-RPC 2.x

Same name and namespace in other branches
  1. 8 src/Shaper/RpcRequestFactory.php \Drupal\jsonrpc\Shaper\RpcRequestFactory::denormalizeParams()

Denormalizes a JSON-RPC request object's parameters.

Parameters

object $data: The decoded JSON-RPC request to be denormalized.

\Shaper\Util\Context $context: The denormalized JSON-RPC request.

Return value

\Drupal\jsonrpc\Object\ParameterBag|null The denormalized parameters or NULL if none were provided.

Throws

\Drupal\jsonrpc\Exception\JsonRpcException

1 call to RpcRequestFactory::denormalizeParams()
RpcRequestFactory::denormalizeRequest in src/Shaper/RpcRequestFactory.php
Denormalizes a single JSON-RPC request object.

File

src/Shaper/RpcRequestFactory.php, line 127

Class

RpcRequestFactory
Creates RPC Request objects.

Namespace

Drupal\jsonrpc\Shaper

Code

protected function denormalizeParams($data, Context $context) {
  if (!$this->handler
    ->supportsMethod($data['method'])) {
    throw $this
      ->newException(Error::methodNotFound($data['method']), $context);
  }
  $method = $this->handler
    ->getMethod($data['method']);
  $params = $method
    ->getParams();
  if (is_null($params)) {
    if (isset($data['params'])) {
      $error = Error::invalidParams("The {$data['method']} method does not accept parameters.");
      throw $this
        ->newException($error, $context);
    }
    return NULL;
  }
  $arguments = [];
  $positional = $method
    ->areParamsPositional();
  foreach ($params as $key => $param) {
    if (isset($data['params'][$key])) {
      $arguments[$key] = $this
        ->denormalizeParam($data['params'][$key], $param);
    }
    elseif ($param
      ->isRequired()) {
      throw $this
        ->newException(Error::invalidParams("Missing required parameter: {$key}"), $context);
    }
  }
  return new ParameterBag($arguments, $positional);
}