Documentation

JsonRpc
in package

Table of Contents

Methods

createResponseError()  : array{jsonrpc: string, error: array, id: ?mixed}
Create a response object (unsuccessful).
createResponseSuccess()  : array{jsonrpc: string, result: mixed, id: ?mixed}
Create a response object (successful).
run()  : string
Execute a JSON-RPC request and return a result.
handleMethodCall()  : array<string|int, mixed>

Methods

createResponseError()

Create a response object (unsuccessful).

public static createResponseError(array{jsonrpc: string, method: string, params: array, id: ?mixed} $request, Throwable $t) : array{jsonrpc: string, error: array, id: ?mixed}
Parameters
$request : array{jsonrpc: string, method: string, params: array, id: ?mixed}
$t : Throwable

The exception which caused the request to fail.

Tags
link
https://www.jsonrpc.org/specification#response_object
Return values
array{jsonrpc: string, error: array, id: ?mixed}

createResponseSuccess()

Create a response object (successful).

public static createResponseSuccess(array{jsonrpc: string, method: string, params: array, id: ?mixed} $request, mixed $result) : array{jsonrpc: string, result: mixed, id: ?mixed}
Parameters
$request : array{jsonrpc: string, method: string, params: array, id: ?mixed}
$result : mixed

The result-value of the method call.

Tags
link
https://www.jsonrpc.org/specification#response_object
Return values
array{jsonrpc: string, result: mixed, id: ?mixed}

run()

Execute a JSON-RPC request and return a result.

public static run(string $requestLine, callable $dispatcher) : string

This adapter handles decoding, encoding, and conversion of exceptions.

Parameters
$requestLine : string

JSON formatted RPC request

$dispatcher : callable

Dispatch function - given a parsed/well-formed request, compute the result. Signature: function(string $method, mixed $params): mixed

Tags
code

$input = '{"jsonrpc":"2.0","method":"greet","id":1}'; $output = JsonRpc::run($input, function(string $method, array $params) { if ($method === 'greet') return 'hello world'; else throw new \InvalidArgumentException('Method not found', -32601); }); assert $output === '{"jsonrpc":"2.0","result":"hello world","id":1}';

endCode
Return values
string

JSON formatted RPC response

handleMethodCall()

protected static handleMethodCall(mixed $request, mixed $dispatcher) : array<string|int, mixed>
Parameters
$request : mixed
$dispatcher : mixed
Return values
array<string|int, mixed>

        
On this page

Search results